diff --git a/.github/scripts/tag-set.sh b/.github/scripts/tag-set.sh new file mode 100755 index 000000000..d2803a5a2 --- /dev/null +++ b/.github/scripts/tag-set.sh @@ -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 diff --git a/.github/scripts/version-set.sh b/.github/scripts/version-set.sh new file mode 100755 index 000000000..734f64144 --- /dev/null +++ b/.github/scripts/version-set.sh @@ -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 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 diff --git a/.github/workflows/stable-tag.yml b/.github/workflows/stable-tag.yml deleted file mode 100644 index b13f9c18e..000000000 --- a/.github/workflows/stable-tag.yml +++ /dev/null @@ -1,24 +0,0 @@ -name: Tag with Stable - -on: - push: - tags: - - 'v*' - -jobs: - tag-if-latest-stable: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Register git credentials - run: | - git config user.name github-actions - git config user.email github-actions@github.com - - name: Update stable if latest - run: | - MOST_RECENT_STABLE=$(git ls-remote --tags origin | tail -n 1 | sed -E 's/.*\s//') - echo "New: '""${GITHUB_REF}""', most recent: '""${MOST_RECENT_STABLE}""'" - if [ "$GITHUB_REF" == "$MOST_RECENT_STABLE" ]; then - git tag stable - git push --tags -f - fi diff --git a/.github/workflows/versioning-reset.yml b/.github/workflows/versioning-reset.yml new file mode 100644 index 000000000..63fbf6424 --- /dev/null +++ b/.github/workflows/versioning-reset.yml @@ -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 diff --git a/.github/workflows/versioning.yml b/.github/workflows/versioning.yml index b377caadc..fc05eb540 100644 --- a/.github/workflows/versioning.yml +++ b/.github/workflows/versioning.yml @@ -3,7 +3,7 @@ name: Version Bump on: release: types: - - created + - published workflow_dispatch: @@ -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