Skip to content

Commit

Permalink
Issue 416: fixing versioning workflow for releases and merges to mast…
Browse files Browse the repository at this point in the history
…er (#468)

* change workflow to correctly modify the app version on releases and when forcing merged version back to master

* protect main branch from accidental releases
  • Loading branch information
seb authored Jul 12, 2022
1 parent a328310 commit b6b9b8f
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 46 deletions.
29 changes: 29 additions & 0 deletions .github/scripts/tag-set.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
TAG="$1"
PUSH=${2:-"no-push"}

function extract_version()
{
local version=$(echo "$1" | sed -E 's/^(refs\/(heads|tags)\/)?(.*)/\3/')
echo "$version"
}

NEW_TAG=$(extract_version $TAG)

git config user.name github-actions
git config user.email [email protected]
git tag -f $NEW_TAG

HIGHEST_vTAG=$(extract_version $(git ls-remote --tags --sort=-v:refname origin | grep -P "/v\d.*" | head -n 1 | awk '{print $2}'))
#git fetch --all --tags
#LATEST_vTAG=$(git describe --contains `git rev-list --tags="v*" --max-count=1`)
#HIGHEST_vTAG=$(git tag --list 'v*' --sort=-v:refname | grep -P "v\d.*" | head -n 1)

echo "New tag: '$NEW_TAG', Highest version tag: '$HIGHEST_vTAG'"
if [ "$NEW_TAG" == "$HIGHEST_vTAG" ]; then
echo "Additionally setting 'stable' tag"
git tag -f stable
fi
if [[ "$PUSH" == "push" ]]; then
git push --tags -f
fi
45 changes: 45 additions & 0 deletions .github/scripts/version-set.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash
VERSION="$1"
PUSH=${2:-"no-push"}
ADD_COMMIT=${3:-"add-commit"}

VERSION_FILE='app/__version__.py'
VERSION_PLACEHOLDER='__version__ =\s+"(.*)"$'
DEV_PLACEHOLDER='_dev_version =\s+"(.*)"$'

function get_version()
{
local version=$(grep -P "$2" "$1" | sed -E "s/$2/\1/")
echo "$version"
}

function set_version()
{
sed -i -E "s/$2/__version__ = \"$3\"/g" "$1"
echo $(get_version "$1" "$2")
}

OLD_VERSION=$(get_version "$VERSION_FILE" "$VERSION_PLACEHOLDER")
DEV_VERSION=$(get_version "$VERSION_FILE" "$DEV_PLACEHOLDER")
NEW_VERSION=$(echo ${VERSION:-"$DEV_VERSION"} | sed -E 's/^v([0-9].*)/\1/')
echo "setting version from \"$OLD_VERSION\" to \"$NEW_VERSION\""
FINAL_VERSION=$(set_version "$VERSION_FILE" "$VERSION_PLACEHOLDER" "$NEW_VERSION")
echo "version changed to \"$FINAL_VERSION\""
echo "VERSION=$FINAL_VERSION" >> $GITHUB_ENV

if [[ "$PUSH" == "push" ]]; then
if [[ -x "$(command -v git)" ]]; then
git config user.name github-actions
git config user.email [email protected]
if [[ "$ADD_COMMIT" == "add-commit" ]]; then
git commit -am "Update version to $FINAL_VERSION"
git push
else
git commit -a --amend --no-edit
git push -f
fi
else
echo "Can not push the version changes as git is not available."
exit 1
fi
fi
24 changes: 0 additions & 24 deletions .github/workflows/stable-tag.yml

This file was deleted.

27 changes: 27 additions & 0 deletions .github/workflows/versioning-reset.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Version reset to dev
# reset the version if merging a release branch into main

on:
push:
branches:
- main
- master
paths:
- 'app/__version__.py'

workflow_dispatch:

env:
BRANCH: ${{ github.event.release.target_commitish }}

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
ref: ${{ env.BRANCH }}
fetch-depth: 2 # we need the previous commit as we amend the last one
- name: Reset version to dev
run: ./.github/scripts/version-set.sh "" push no-add-commit
50 changes: 28 additions & 22 deletions .github/workflows/versioning.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Version Bump
on:
release:
types:
- created
- published

workflow_dispatch:

Expand All @@ -12,30 +12,36 @@ env:
TAG: ${{ github.event.release.tag_name }}

jobs:
deploy:
validation:
runs-on: ubuntu-latest
steps:
- name: Release only from stable-v* branch
if: ${{ !startsWith(env.BRANCH, 'stable-v') }}
uses: actions/github-script@v6
with:
script: core.setFailed("Releases can only be made from 'stable-v*' branches")
rollback:
needs: validation
if: ${{ failure() }}
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Rollback if invalid release
uses: author/action-rollback@stable
with:
tag: ${{ env.TAG }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
versioning:
needs: validation
if: ${{ success() }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: ${{ env.BRANCH }}
- name: Bump version from tag
run: |
VERSION=$(echo $TAG | sed 's/^v//')
PLACEHOLDER='__version__ = _dev_version'
VERSION_FILE='amlb/__version__.py'
grep "$PLACEHOLDER" "$VERSION_FILE"
sed -i "s/$PLACEHOLDER/__version__ = \"${VERSION}\"/g" "$VERSION_FILE"
echo "VERSION=$VERSION" >> $GITHUB_ENV
shell: bash
- name: Commit version changes
run: |
git config user.name github-actions
git config user.email [email protected]
git commit -am "Update version to $VERSION"
run: ./.github/scripts/version-set.sh $TAG push
- name: Update tag
run: |
git tag $TAG
git push
git push --tags -f
run: ./.github/scripts/tag-set.sh $TAG push
- name: Restore dev version
run: ./.github/scripts/version-set.sh "" push

0 comments on commit b6b9b8f

Please sign in to comment.