Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 416: fixing versioning workflow for releases and merges to master #468

Merged
merged 2 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 github-actions@github.com
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
sebhrusen marked this conversation as resolved.
Show resolved Hide resolved

if [[ "$PUSH" == "push" ]]; then
if [[ -x "$(command -v git)" ]]; then
git config user.name github-actions
git config user.email github-actions@github.com
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'
PGijsbers marked this conversation as resolved.
Show resolved Hide resolved

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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we ever want to release from master? I don't believe it's something we currently want in our workflow? If not, we should probably add a check for that here. I have in the past accidentally released from master instead of stable-vX, because master is our default branch and I forgot to change the option in the release UI.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think we should ever release from master.

Let me find what's the best way to constraint this here.

Copy link
Collaborator Author

@sebhrusen sebhrusen Jul 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be addressed with the new changes: learned much about GH workflows thanks to this one, especially to be able to bypass the default parallelism of actions and jobs

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, we also use that to first test on baselines and only then frameworks too 👍

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 github-actions@github.com
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