From 3f928b20cd4943612df4dd19777fe00492b2c183 Mon Sep 17 00:00:00 2001 From: Niall Byrne <9848926+niall-byrne@users.noreply.github.com> Date: Sat, 25 Feb 2023 10:33:54 -0500 Subject: [PATCH] ci(GITHUB): prefer pure semantic versioning --- .github/workflows/self-test.yml | 32 +++++++++++++---- hooks/post_gen_project.sh | 2 +- .../.github/scripts/branch_filter.sh | 25 +++++++++++++ .../.github/scripts/changelog.sh | 10 +++++- .../.github/scripts/pushed_commit_range.sh | 2 +- .../.github/scripts/version.sh | 21 +++++++++++ .../.github/workflows/push.yml | 36 ++++++++++++++----- {{cookiecutter.project_slug}}/pyproject.toml | 2 +- 8 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 {{cookiecutter.project_slug}}/.github/scripts/branch_filter.sh create mode 100644 {{cookiecutter.project_slug}}/.github/scripts/version.sh diff --git a/.github/workflows/self-test.yml b/.github/workflows/self-test.yml index 22961009..294921c5 100644 --- a/.github/workflows/self-test.yml +++ b/.github/workflows/self-test.yml @@ -99,26 +99,44 @@ jobs: steps: - name: Create Release -- Checkout Repository - if: contains(github.ref, '/tags/v') + uses: actions/checkout@v3 + + - name: Create Release -- Branch Filter + id: branch_filter + run: | + source ./{{cookiecutter.project_slug}}/.github/scripts/branch_filter.sh "${{ github.event.ref }}" + + - name: Create Release -- Checkout Repository (All Commits) + if: steps.branch_filter.outputs.match == 'TRUE' uses: actions/checkout@v3 with: fetch-depth: 0 - name: Create Release -- Setup Environment - if: contains(github.ref, '/tags/v') + if: steps.branch_filter.outputs.match == 'TRUE' run: | source ./{{cookiecutter.project_slug}}/.github/scripts/setup.sh echo "{}" > package.json env: WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} + - name: Create Release -- Install Poetry + if: steps.branch_filter.outputs.match == 'TRUE' + run: | + source ./{{cookiecutter.project_slug}}/.github/scripts/poetry.sh "install-poetry" + + - name: Create Release -- Check 'pyproject.toml' Matches Tag + if: steps.branch_filter.outputs.match == 'TRUE' + run: | + source ./{{cookiecutter.project_slug}}/.github/scripts/version.sh + - name: Create Release -- Generate Changelog - if: contains(github.ref, '/tags/v') + if: steps.branch_filter.outputs.match == 'TRUE' run: source ./{{cookiecutter.project_slug}}/.github/scripts/changelog.sh - name: Create Release -- Create Github Release - if: contains(github.ref, '/tags/v') + if: steps.branch_filter.outputs.match == 'TRUE' uses: actions/create-release@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -134,7 +152,7 @@ jobs: prerelease: false - name: Create Release -- Report Job Status on Success - if: contains(github.ref, '/tags/v') + if: steps.branch_filter.outputs.match == 'TRUE' run: | ./{{cookiecutter.project_slug}}/.github/scripts/notifications.sh "${NOTIFICATION}" ":white_check_mark: automated release has been created!\nhttps://github.com/${USERNAME}/${PROJECT_NAME}/releases" @@ -315,8 +333,8 @@ jobs: run: | cd ${TEMPLATED_NAME} git checkout master - git tag --delete v0.0.0 # Don't Repush - git tag v1.0.0 + git tag --delete 0.0.0 # Don't Repush + git tag 0.1.0 - name: Push Test -- Push To Test Repository (master) uses: ad-m/github-push-action@v0.6.0 diff --git a/hooks/post_gen_project.sh b/hooks/post_gen_project.sh index 2526f85d..4e9e10a7 100644 --- a/hooks/post_gen_project.sh +++ b/hooks/post_gen_project.sh @@ -20,7 +20,7 @@ initialize_git() { git stage . git commit -m "build(COOKIECUTTER): initial generation" git symbolic-ref HEAD "refs/heads/${ANSIBLE_WORKBENCH_BRANCH_NAME_BASE}" - git tag v0.0.0 + git tag 0.0.0 git checkout -b "${ANSIBLE_WORKBENCH_BRANCH_NAME_DEVELOPMENT}" mkdir -p files templates diff --git a/{{cookiecutter.project_slug}}/.github/scripts/branch_filter.sh b/{{cookiecutter.project_slug}}/.github/scripts/branch_filter.sh new file mode 100644 index 00000000..e41dfc5b --- /dev/null +++ b/{{cookiecutter.project_slug}}/.github/scripts/branch_filter.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# .github/scripts/branch_filter.sh +# Evaluates if the current git reference is a release candidate. + +# 1: The git reference that created the workflow flow. + +# CI only script. + +set -eo pipefail + +MATCH="FALSE" + +main() { + + if [[ "${1}" =~ ^refs/tags/[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$ ]] && + [[ "${1}" != "refs/tags/0.0.0" ]]; then + MATCH="TRUE" + fi + + echo "match=${MATCH}" >> "${GITHUB_OUTPUT}" + +} + +main "$@" diff --git a/{{cookiecutter.project_slug}}/.github/scripts/changelog.sh b/{{cookiecutter.project_slug}}/.github/scripts/changelog.sh index 5c76aae5..b4594f09 100644 --- a/{{cookiecutter.project_slug}}/.github/scripts/changelog.sh +++ b/{{cookiecutter.project_slug}}/.github/scripts/changelog.sh @@ -1,11 +1,19 @@ #!/bin/bash +# .github/scripts/changelog.sh +# Generates a changelog based off the diff between $TAG and the previous sequential tag. + +# TAG: The new git tag the changelog is being generated for. + +# CI only script + set -eo pipefail main() { echo "{}" > package.json - TAG="$(git tag | sort --version-sort | tail -n 2 | head -n 1)" + + TAG="$(git tag -l --sort=refname | grep -E "[0-9]+\.[0-9]+\.[0-9]+" | tail -n 2 | head -n 1)" CHANGE_LOG_CONTENT="$(npx -q generate-changelog -f - -t "${TAG}")" { diff --git a/{{cookiecutter.project_slug}}/.github/scripts/pushed_commit_range.sh b/{{cookiecutter.project_slug}}/.github/scripts/pushed_commit_range.sh index 2abadb50..3b878ef9 100644 --- a/{{cookiecutter.project_slug}}/.github/scripts/pushed_commit_range.sh +++ b/{{cookiecutter.project_slug}}/.github/scripts/pushed_commit_range.sh @@ -31,7 +31,7 @@ main() { echo "PUSHED_COMMIT_RANGE<> "$GITHUB_ENV" + } >> "${GITHUB_ENV}" } diff --git a/{{cookiecutter.project_slug}}/.github/scripts/version.sh b/{{cookiecutter.project_slug}}/.github/scripts/version.sh new file mode 100644 index 00000000..0991ebc5 --- /dev/null +++ b/{{cookiecutter.project_slug}}/.github/scripts/version.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# .github/scripts/version.sh +# Ensures the 'pyproject.toml' version matches the current Git Tag. + +# BRANCH_OR_TAG: The name of the current Git Branch or Tag. + +# CI only script. + +set -eo pipefail + +main() { + + if [[ "$(poetry version -s)" != "${BRANCH_OR_TAG}" ]]; then + echo "The 'pyproject.toml' file does not match the version tag!" + exit 127 + fi + +} + +main "$@" diff --git a/{{cookiecutter.project_slug}}/.github/workflows/push.yml b/{{cookiecutter.project_slug}}/.github/workflows/push.yml index c3e0ef3c..10f3603d 100644 --- a/{{cookiecutter.project_slug}}/.github/workflows/push.yml +++ b/{{cookiecutter.project_slug}}/.github/workflows/push.yml @@ -81,26 +81,44 @@ jobs: runs-on: ubuntu-latest steps: - - name: Release -- Checkout Repository - if: contains(github.ref, '/tags/v') + - name: Create Release -- Checkout Repository + uses: {% endraw %}{{ cookiecutter._GITHUB_ACTION_CHECKOUT }}{% raw %} + + - name: Create Release -- Branch Filter + id: branch_filter + run: | + source .github/scripts/branch_filter.sh "${{ github.event.ref }}" + + - name: Create Release -- Checkout Repository (All Commits) + if: steps.branch_filter.outputs.match == 'TRUE' uses: {% endraw %}{{ cookiecutter._GITHUB_ACTION_CHECKOUT }}{% raw %} with: fetch-depth: 0 - - name: Release -- Setup Environment - if: contains(github.ref, '/tags/v') + - name: Create Release -- Setup Environment + if: steps.branch_filter.outputs.match == 'TRUE' run: | source ./.github/scripts/setup.sh env: WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} + - name: Create Release -- Install Poetry + if: steps.branch_filter.outputs.match == 'TRUE' + run: | + source ./.github/scripts/poetry.sh "install-poetry" + + - name: Create Release -- Check 'pyproject.toml' Matches Tag + if: steps.branch_filter.outputs.match == 'TRUE' + run: | + source ./.github/scripts/version.sh + - name: Create Release -- Generate Changelog - if: contains(github.ref, '/tags/v') + if: steps.branch_filter.outputs.match == 'TRUE' run: source ./.github/scripts/changelog.sh - name: Create Release -- Create Github Release - if: contains(github.ref, '/tags/v') + if: steps.branch_filter.outputs.match == 'TRUE' uses: actions/create-release@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -113,12 +131,12 @@ jobs: prerelease: false - name: Create Release -- Report Job Status (Success) - if: contains(github.ref, '/tags/v') + if: steps.branch_filter.outputs.match == 'TRUE' run: | ./.github/scripts/notifications.sh "${NOTIFICATION}" ":white_check_mark: automated release has been created:\nhttps://github.com/${USERNAME}/${PROJECT_NAME}/releases" - - name: Release -- Report Job Status (Failure) - if: failure() && contains(github.ref, '/tags/v') + - name: Create Release -- Report Job Status (Failure) + if: failure() run: | ./.github/scripts/notifications.sh "${NOTIFICATION}" ":x: automated release generation failed!" diff --git a/{{cookiecutter.project_slug}}/pyproject.toml b/{{cookiecutter.project_slug}}/pyproject.toml index 48d3cd74..46f7a204 100644 --- a/{{cookiecutter.project_slug}}/pyproject.toml +++ b/{{cookiecutter.project_slug}}/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "{{cookiecutter.project_slug}}" -version = "0.0.1" +version = "0.1.0" description = "{{cookiecutter.description}}" authors = ["{{cookiecutter.author}} <{{cookiecutter.email}}>"]