Skip to content

Commit

Permalink
Change log ack from all logs to merge commit log
Browse files Browse the repository at this point in the history
  • Loading branch information
sbe-arg authored Oct 4, 2022
2 parents d915778 + 46487ca commit c15fbc2
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 52 deletions.
1 change: 0 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:
- edited
- reopened
- synchronize
workflow_dispatch:

permissions:
pull-requests: write
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
name: Bump version

on:
push:
pull_request:
types:
- closed
branches:
- master
paths-ignore:
- '.github/**'

jobs:
build:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: '0'

- name: version-tag
id: tag
uses: anothrNick/github-tag-action@1.47.0 # if we use 1 there is a too-be-fixed bug https://github.com/anothrNick/github-tag-action/actions/runs/3139501775/jobs/5099976842#step:1:35
uses: anothrNick/github-tag-action@1.51.0 # if we use 1 there is a too-be-fixed bug https://github.com/anothrNick/github-tag-action/actions/runs/3139501775/jobs/5099976842#step:1:35
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand Down
64 changes: 45 additions & 19 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ on:
- edited
- reopened
- synchronize
workflow_dispatch:

permissions:
pull-requests: write
Expand All @@ -22,15 +21,27 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: '0'

# Use the action to generate a tag for itself
- name: Test action
id: test
- name: Test action main
id: test_main
uses: ./
env:
DRY_RUN: true
WITH_V: true
VERBOSE: true
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# Use the action to generate a tag for itself
- name: Test action pre-release
id: test_pre
uses: ./
env:
DRY_RUN: true
WITH_V: true
PRERELEASE: true
PRERELEASE_SUFFIX: test
VERBOSE: true
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -39,25 +50,40 @@ jobs:
- name: Check if the tag would have been created
shell: bash
run: |
OUTPUT_TAG=${{ steps.test.outputs.tag }}
OUTPUT_NEWTAG=${{ steps.test.outputs.new_tag }}
OUTPUT_PART=${{ steps.test.outputs.part }}
set -x
MAIN_OUTPUT_TAG=${{ steps.test_main.outputs.tag }}
MAIN_OUTPUT_NEWTAG=${{ steps.test_main.outputs.new_tag }}
MAIN_OUTPUT_PART=${{ steps.test_main.outputs.part }}
PRE_OUTPUT_TAG=${{ steps.test_pre.outputs.tag }}
PRE_OUTPUT_NEWTAG=${{ steps.test_pre.outputs.new_tag }}
PRE_OUTPUT_PART=${{ steps.test_pre.outputs.part }}
echo "Outputs from running the action:" >> $GITHUB_STEP_SUMMARY
echo "Tag: $OUTPUT_TAG" >> $GITHUB_STEP_SUMMARY
echo "New tag: $OUTPUT_NEWTAG" >> $GITHUB_STEP_SUMMARY
echo "Part: $OUTPUT_PART" >> $GITHUB_STEP_SUMMARY
# Work out what the new tag should be
a=( ${OUTPUT_TAG//./ } )
((a[1]++))
a[2]=0
CORRECT_TAG="v${a[0]}.${a[1]}.${a[2]}-test.0"
if [[ $OUTPUT_NEWTAG == $CORRECT_TAG ]]; then
echo "The tag was created correctly" >> $GITHUB_STEP_SUMMARY
echo "MAIN Tag: $MAIN_OUTPUT_TAG" >> $GITHUB_STEP_SUMMARY
echo "MAIN New tag: $MAIN_OUTPUT_NEWTAG" >> $GITHUB_STEP_SUMMARY
echo "MAIN Part: $MAIN_OUTPUT_PART" >> $GITHUB_STEP_SUMMARY
echo "PRE Tag: $PRE_OUTPUT_TAG" >> $GITHUB_STEP_SUMMARY
echo "PRE New tag: $PRE_OUTPUT_NEWTAG" >> $GITHUB_STEP_SUMMARY
echo "PRE Part: $PRE_OUTPUT_PART" >> $GITHUB_STEP_SUMMARY
# check that the original tag got bumped either major, minor, patch
verlte() {
[ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}
verlt() {
[ "$1" = "$2" ] && return 1 || verlte $1 $2
}
main="$(verlt $MAIN_OUTPUT_TAG $MAIN_OUTPUT_NEWTAG && true || false)"
pre="$(verlt $PRE_OUTPUT_TAG $PRE_OUTPUT_NEWTAG && true || false)"
if $main && $pre
then
echo "The tags were created correctly" >> $GITHUB_STEP_SUMMARY
else
echo "The tag was not created correctly, expected $CORRECT_TAG got $OUTPUT_NEWTAG" >> $GITHUB_STEP_SUMMARY
echo "Tags not created correctly" >> $GITHUB_STEP_SUMMARY
exit 1
fi
# todo add test for #none bump
42 changes: 36 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,55 @@ A Github Action to automatically bump and tag master, on merge, with the latest

### Usage

```Dockerfile
```yaml
# example 1: on push to master
name: Bump version
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
with:
fetch-depth: '0'

- name: Bump version and push tag
uses: anothrNick/github-tag-action@1.40.0
uses: anothrNick/github-tag-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
WITH_V: true
```
```yaml
# example 2: on merge to master
name: Bump version
on:
pull_request:
types:
- closed
branches:
- master
jobs:
build:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: '0'

- name: Bump version and push tag
uses: anothrNick/github-tag-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
WITH_V: true
```
**Depending if you choose example 1 or example 2 is how crafted version bumps operate when reading the commit log. Is recommended to use on `pull_request` instead of on commit to master/main.**

_NOTE: set the fetch-depth for `actions/checkout@v2` or newer to be sure you retrieve all commits to look for the semver commit message._

#### Options
Expand All @@ -47,6 +76,7 @@ _NOTE: set the fetch-depth for `actions/checkout@v2` or newer to be sure you ret
- **DRY_RUN** _(optional)_ - Determine the next version without tagging the branch. The workflow can use the outputs `new_tag` and `tag` in subsequent steps. Possible values are `true` and `false` (default).
- **INITIAL_VERSION** _(optional)_ - Set initial version before bump. Default `0.0.0`.
- **TAG_CONTEXT** _(optional)_ - Set the context of the previous tag. Possible values are `repo` (default) or `branch`.
- **PRERELEASE** _(optional)_ - Define if workflow runs in prerelease mode, `false` by default. Note this will be overwritten if using complex suffix release branches.
- **PRERELEASE_SUFFIX** _(optional)_ - Suffix for your prerelease versions, `beta` by default. Note this will only be used if a prerelease branch.
- **VERBOSE** _(optional)_ - Print git logs. For some projects these logs may be very large. Possible values are `true` (default) and `false`.
- **MAJOR_STRING_TOKEN** _(optional)_ - Change the default `#major` commit message string tag.
Expand All @@ -65,9 +95,9 @@ _NOTE: set the fetch-depth for `actions/checkout@v2` or newer to be sure you ret
### Bumping

**Manual Bumping:** Any commit message that includes `#major`, `#minor`, `#patch`, or `#none` will trigger the respective version bump. If two or more are present, the highest-ranking one will take precedence.
If `#none` is contained in the commit message, it will skip bumping regardless `DEFAULT_BUMP`.
If `#none` is contained in the merge commit message, it will skip bumping regardless `DEFAULT_BUMP`.

**Automatic Bumping:** If no `#major`, `#minor` or `#patch` tag is contained in the commit messages, it will bump whichever `DEFAULT_BUMP` is set to (which is `minor` by default). Disable this by setting `DEFAULT_BUMP` to `none`.
**Automatic Bumping:** If no `#major`, `#minor` or `#patch` tag is contained in the merge commit message, it will bump whichever `DEFAULT_BUMP` is set to (which is `minor` by default). Disable this by setting `DEFAULT_BUMP` to `none`.

> **_Note:_** This action **will not** bump the tag if the `HEAD` commit has already been tagged.

Expand All @@ -78,7 +108,7 @@ If `#none` is contained in the commit message, it will skip bumping regardless `
- Either push to master or open a PR
- On push (or merge), the action will:
- Get latest tag
- Bump tag with minor version unless any commit message contains `#major` or `#patch`
- Bump tag with minor version unless the merge commit message contains `#major` or `#patch`
- Pushes tag to github
- If triggered on your repo's default branch (`master` or `main` if unchanged), the bump version will be a release tag.
- If triggered on any other branch, a prerelease will be generated, depending on the bump, starting with `*-<PRERELEASE_SUFFIX>.1`, `*-<PRERELEASE_SUFFIX>.2`, ...
Expand Down
44 changes: 22 additions & 22 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ source=${SOURCE:-.}
dryrun=${DRY_RUN:-false}
initial_version=${INITIAL_VERSION:-0.0.0}
tag_context=${TAG_CONTEXT:-repo}
prerelease=${PRERELEASE:-false}
suffix=${PRERELEASE_SUFFIX:-beta}
verbose=${VERBOSE:-true}
verbose=${VERBOSE:-true}
major_string_token=${MAJOR_STRING_TOKEN:-#major}
minor_string_token=${MINOR_STRING_TOKEN:-#minor}
patch_string_token=${PATCH_STRING_TOKEN:-#patch}
Expand All @@ -32,16 +32,23 @@ echo -e "\tSOURCE: ${source}"
echo -e "\tDRY_RUN: ${dryrun}"
echo -e "\tINITIAL_VERSION: ${initial_version}"
echo -e "\tTAG_CONTEXT: ${tag_context}"
echo -e "\tPRERELEASE: ${prerelease}"
echo -e "\tPRERELEASE_SUFFIX: ${suffix}"
echo -e "\tVERBOSE: ${verbose}"
echo -e "\tMAJOR_STRING_TOKEN: ${major_string_token}"
echo -e "\tMINOR_STRING_TOKEN: ${minor_string_token}"
echo -e "\tPATCH_STRING_TOKEN: ${patch_string_token}"
echo -e "\tNONE_STRING_TOKEN: ${none_string_token}"

# verbose, show everything
if $verbose
then
set -x
fi

current_branch=$(git rev-parse --abbrev-ref HEAD)

pre_release="true"
pre_release="$prerelease"
IFS=',' read -ra branch <<< "$release_branches"
for b in "${branch[@]}"; do
# check if ${current_branch} is in ${release_branches} | exact branch match
Expand Down Expand Up @@ -77,10 +84,9 @@ case "$tag_context" in
exit 1;;
esac

# if there are none, start tags at INITIAL_VERSION which defaults to 0.0.0
# if there are none, start tags at INITIAL_VERSION
if [ -z "$tag" ]
then
log=$(git log --pretty='%B' --)
if $with_v
then
tag="v$initial_version"
Expand All @@ -96,8 +102,6 @@ then
pre_tag="$initial_version"
fi
fi
else
log=$(git log "$tag"..HEAD --pretty='%B' --)
fi

# get current commit hash for tag
Expand All @@ -114,11 +118,9 @@ then
exit 0
fi

# echo log if verbose is wanted
if $verbose
then
echo "$log"
fi
# get the merge commit message looking for #bumps
log=$(git show -s --format=%s)
echo "Last commit message: $log"

case "$log" in
*$major_string_token* ) new=$(semver -i major "$tag"); part="major";;
Expand All @@ -145,7 +147,7 @@ esac

if $pre_release
then
# Already a prerelease available, bump it
# already a pre-release available, bump it
if [[ "$pre_tag" =~ $new ]] && [[ "$pre_tag" =~ $suffix ]]
then
if $with_v
Expand All @@ -162,15 +164,15 @@ then
else
new="$new-$suffix.0"
fi
echo -e "Setting ${suffix} pre-tag ${pre_tag}. With pre-tag ${new}"
echo -e "Setting ${suffix} pre-tag ${pre_tag} - With pre-tag ${new}"
fi
part="pre-$part"
else
if $with_v
then
new="v$new"
fi
echo -e "Bumping tag ${tag}. New tag ${new}"
echo -e "Bumping tag ${tag} - New tag ${new}"
fi

# as defined in readme if CUSTOM_TAG is used any semver calculations are irrelevant.
Expand All @@ -182,15 +184,13 @@ fi
# set outputs
echo "::set-output name=new_tag::$new"
echo "::set-output name=part::$part"
echo "::set-output name=tag::$tag"

# use dry run to determine the next tag
# dry run exit without real changes
if $dryrun
then
echo "::set-output name=tag::$tag"
exit 0
fi

echo "::set-output name=tag::$new"
fi

# create local git tag
git tag "$new"
Expand Down Expand Up @@ -219,8 +219,8 @@ git_ref_posted=$( echo "${git_refs_response}" | jq .ref | tr -d '"' )
echo "::debug::${git_refs_response}"
if [ "${git_ref_posted}" = "refs/tags/${new}" ]
then
exit 0
exit 0
else
echo "::error::Tag was not created properly."
exit 1
echo "::error::Tag was not created properly."
exit 1
fi

0 comments on commit c15fbc2

Please sign in to comment.