From 5ce0b2778f03e25d7df77ed9df548d063c6442b5 Mon Sep 17 00:00:00 2001 From: Nick Le Large Date: Wed, 20 Nov 2024 15:02:21 +0100 Subject: [PATCH 1/8] Replace the version bump action using a custom shell script --- ...create-release-and-push-docker-images.yaml | 22 +++------ .github/workflows/compute_version.sh | 48 +++++++++++++++++++ 2 files changed, 54 insertions(+), 16 deletions(-) create mode 100755 .github/workflows/compute_version.sh diff --git a/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml b/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml index 861d335c..92690454 100644 --- a/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml +++ b/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml @@ -18,23 +18,13 @@ jobs: - name: Check out the repository uses: actions/checkout@v4 - - name: Read version from file - run: | - # Read the version from the version file, only store the number (without the 'v') - INITIAL_VERSION=$(source version && echo ${VERSION#v}) - echo "Current version: $INITIAL_VERSION" - echo "INITIAL_VERSION=${INITIAL_VERSION}" >> $GITHUB_ENV - - - name: Bump version + - name: Compute new version id: bump_version - uses: anothrNick/github-tag-action@v1 - env: - DEFAULT_BUMP: minor - DRY_RUN: true - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - INITIAL_VERSION: ${{ env.INITIAL_VERSION }} - WITH_V: true - + run: | + # Run the compute_version script and pass the PR description as an argument + cd .github/workflows + new_tag=$(./compute_version.sh "${{ github.event.pull_request.body }}") + echo "new_tag=$new_tag" >> $GITHUB_OUTPUT update-version-file: needs: compute-version diff --git a/.github/workflows/compute_version.sh b/.github/workflows/compute_version.sh new file mode 100755 index 00000000..bdc933e7 --- /dev/null +++ b/.github/workflows/compute_version.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +set -e + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +VERSION_FILE="$SCRIPT_DIR/../../version" + +# Read the current version from the version file or default to v0.0.0 +if [[ -f "$VERSION_FILE" ]]; then + source "$VERSION_FILE" +else + VERSION="v0.0.0" +fi + +# Extract the current version components +major=$(echo $VERSION | cut -d'.' -f1 | cut -d'v' -f2) +minor=$(echo $VERSION | cut -d'.' -f2) +patch=$(echo $VERSION | cut -d'.' -f3) + +# Read the PR description passed as an argument +input_string="$1" + +# Determine the bump type based on PR description +if [[ "$input_string" == *"#major"* ]]; then + new_major=$((major + 1)) + new_minor=0 + new_patch=0 +elif [[ "$input_string" == *"#minor"* ]]; then + new_major=$major + new_minor=$((minor + 1)) + new_patch=0 +elif [[ "$input_string" == *"#patch"* ]]; then + new_major=$major + new_minor=$minor + new_patch=$((patch + 1)) +else + # Default to minor bump + new_major=$major + new_minor=$((minor + 1)) + new_patch=0 +fi + +# Construct the new version +new_version="v${new_major}.${new_minor}.${new_patch}" + +# Output the new version +echo "${new_version}" + From f08521a5601fc0d6bd582e124c3f95227973feab Mon Sep 17 00:00:00 2001 From: Nick Le Large Date: Thu, 21 Nov 2024 07:15:28 +0100 Subject: [PATCH 2/8] Pass initial version to script instead of reading it from file --- ...create-release-and-push-docker-images.yaml | 7 +++--- .github/workflows/compute_version.sh | 23 ++++++++----------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml b/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml index 92690454..2bfbfaf6 100644 --- a/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml +++ b/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml @@ -21,10 +21,9 @@ jobs: - name: Compute new version id: bump_version run: | - # Run the compute_version script and pass the PR description as an argument - cd .github/workflows - new_tag=$(./compute_version.sh "${{ github.event.pull_request.body }}") - echo "new_tag=$new_tag" >> $GITHUB_OUTPUT + initial_version=$(source version && echo ${VERSION#v}) + new_version=$(.github/workflows/compute_version.sh "$initial_version" "${{ github.event.pull_request.body }}") + echo "new_version=$new_version" >> $GITHUB_OUTPUT update-version-file: needs: compute-version diff --git a/.github/workflows/compute_version.sh b/.github/workflows/compute_version.sh index bdc933e7..91d04f80 100755 --- a/.github/workflows/compute_version.sh +++ b/.github/workflows/compute_version.sh @@ -2,23 +2,18 @@ set -e -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -VERSION_FILE="$SCRIPT_DIR/../../version" - -# Read the current version from the version file or default to v0.0.0 -if [[ -f "$VERSION_FILE" ]]; then - source "$VERSION_FILE" -else - VERSION="v0.0.0" +if [[ $# -ne 2 ]]; then + echo "Usage: $0 " + exit 1 fi -# Extract the current version components -major=$(echo $VERSION | cut -d'.' -f1 | cut -d'v' -f2) -minor=$(echo $VERSION | cut -d'.' -f2) -patch=$(echo $VERSION | cut -d'.' -f3) +initial_version="$1" +input_string="$2" -# Read the PR description passed as an argument -input_string="$1" +# Extract the current version components +major=$(echo "$initial_version" | cut -d'.' -f1 | cut -d'v' -f2) +minor=$(echo "$initial_version" | cut -d'.' -f2) +patch=$(echo "$initial_version" | cut -d'.' -f3) # Determine the bump type based on PR description if [[ "$input_string" == *"#major"* ]]; then From fa957711c76412b5d66c09564187cb057cbd849d Mon Sep 17 00:00:00 2001 From: Nick Le Large Date: Thu, 21 Nov 2024 07:20:26 +0100 Subject: [PATCH 3/8] Sanitize script inputs --- .github/workflows/compute_version.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/compute_version.sh b/.github/workflows/compute_version.sh index 91d04f80..0e43a9c7 100755 --- a/.github/workflows/compute_version.sh +++ b/.github/workflows/compute_version.sh @@ -7,8 +7,8 @@ if [[ $# -ne 2 ]]; then exit 1 fi -initial_version="$1" -input_string="$2" +initial_version=${1//[^0-9.v]/} +input_string=${2//[^a-zA-Z0-9#]/} # Extract the current version components major=$(echo "$initial_version" | cut -d'.' -f1 | cut -d'v' -f2) From 95bcfb3984ec5a6dc5f1f479b06056cd27060116 Mon Sep 17 00:00:00 2001 From: Nick Le Large Date: Thu, 21 Nov 2024 07:22:30 +0100 Subject: [PATCH 4/8] Small script tidy up Variable renaming to clarify which variables concern the inital version Remove unnecessary comments --- .github/workflows/compute_version.sh | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/.github/workflows/compute_version.sh b/.github/workflows/compute_version.sh index 0e43a9c7..19bd1e87 100755 --- a/.github/workflows/compute_version.sh +++ b/.github/workflows/compute_version.sh @@ -10,34 +10,30 @@ fi initial_version=${1//[^0-9.v]/} input_string=${2//[^a-zA-Z0-9#]/} -# Extract the current version components -major=$(echo "$initial_version" | cut -d'.' -f1 | cut -d'v' -f2) -minor=$(echo "$initial_version" | cut -d'.' -f2) -patch=$(echo "$initial_version" | cut -d'.' -f3) +initial_major=$(echo "$initial_version" | cut -d'.' -f1 | cut -d'v' -f2) +initial_minor=$(echo "$initial_version" | cut -d'.' -f2) +initial_patch=$(echo "$initial_version" | cut -d'.' -f3) # Determine the bump type based on PR description if [[ "$input_string" == *"#major"* ]]; then - new_major=$((major + 1)) + new_major=$((initial_major + 1)) new_minor=0 new_patch=0 elif [[ "$input_string" == *"#minor"* ]]; then - new_major=$major - new_minor=$((minor + 1)) + new_major=$initial_major + new_minor=$((initial_minor + 1)) new_patch=0 elif [[ "$input_string" == *"#patch"* ]]; then - new_major=$major - new_minor=$minor - new_patch=$((patch + 1)) + new_major=$initial_major + new_minor=$initial_minor + new_patch=$((initial_patch + 1)) else # Default to minor bump - new_major=$major - new_minor=$((minor + 1)) + new_major=$initial_major + new_minor=$((initial_minor + 1)) new_patch=0 fi -# Construct the new version new_version="v${new_major}.${new_minor}.${new_patch}" - -# Output the new version echo "${new_version}" From e7934b95ebc8e9641de81923b273397cddfc22d4 Mon Sep 17 00:00:00 2001 From: Nick Le Large Date: Thu, 21 Nov 2024 07:24:21 +0100 Subject: [PATCH 5/8] Rename new_tag to new_version in workflow definition for consistency --- ...create-release-and-push-docker-images.yaml | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml b/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml index 2bfbfaf6..0a45ee3e 100644 --- a/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml +++ b/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml @@ -13,7 +13,7 @@ jobs: permissions: contents: write outputs: - new_tag: ${{ steps.bump_version.outputs.new_tag }} + new_version: ${{ steps.bump_version.outputs.new_version }} steps: - name: Check out the repository uses: actions/checkout@v4 @@ -38,18 +38,18 @@ jobs: - name: Update version file with new version run: | - echo "New version: ${{ needs.compute-version.outputs.new_tag }}" - echo "VERSION=${{ needs.compute-version.outputs.new_tag }}" > version + echo "New version: ${{ needs.compute-version.outputs.new_version }}" + echo "VERSION=${{ needs.compute-version.outputs.new_version }}" > version git config --local user.name "github-actions[bot]" git config --local user.email "github-actions[bot]@users.noreply.github.com" git add version - git commit -m "chore: update version file to ${{ needs.compute-version.outputs.new_tag }}" + git commit -m "chore: update version file to ${{ needs.compute-version.outputs.new_version }}" git push - name: Push new tag run: | - git tag ${{ needs.compute-version.outputs.new_tag }} - git push origin ${{ needs.compute-version.outputs.new_tag }} + git tag ${{ needs.compute-version.outputs.new_version }} + git push origin ${{ needs.compute-version.outputs.new_version }} create-release: @@ -62,7 +62,7 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 - ref: ${{ needs.compute-version.outputs.new_tag }} + ref: ${{ needs.compute-version.outputs.new_version }} - name: Build release packages uses: docker/build-push-action@v6 @@ -82,7 +82,7 @@ jobs: uses: ncipollo/release-action@v1 with: artifacts: "/tmp/artifacts/release/*" - tag: ${{ needs.compute-version.outputs.new_tag }} + tag: ${{ needs.compute-version.outputs.new_version }} body: ${{ github.event.pull_request.body }} build-and-run-release-tests: @@ -93,7 +93,7 @@ jobs: uses: docker/build-push-action@v6 with: build-args: | - RELEASE_DOWNLOAD_URL=https://github.com/KIT-MRT/arbitration_graphs/releases/download/${{ needs.compute-version.outputs.new_tag }} + RELEASE_DOWNLOAD_URL=https://github.com/KIT-MRT/arbitration_graphs/releases/download/${{ needs.compute-version.outputs.new_version }} push: false tags: release_tester_core target: release_test_core @@ -106,7 +106,7 @@ jobs: uses: docker/build-push-action@v6 with: build-args: | - RELEASE_DOWNLOAD_URL=https://github.com/KIT-MRT/arbitration_graphs/releases/download/${{ needs.compute-version.outputs.new_tag }} + RELEASE_DOWNLOAD_URL=https://github.com/KIT-MRT/arbitration_graphs/releases/download/${{ needs.compute-version.outputs.new_version }} push: false tags: release_tester_gui target: release_test_gui @@ -143,32 +143,32 @@ jobs: push: true tags: | ghcr.io/kit-mrt/arbitration_graphs:latest - ghcr.io/kit-mrt/arbitration_graphs:${{ needs.compute-version.outputs.new_tag }} + ghcr.io/kit-mrt/arbitration_graphs:${{ needs.compute-version.outputs.new_version }} target: install - name: Build and push Pacman demo Docker image uses: docker/build-push-action@v6 with: build-args: | - VERSION=${{ needs.compute-version.outputs.new_tag }} + VERSION=${{ needs.compute-version.outputs.new_version }} context: demo file: demo/Dockerfile push: true tags: | ghcr.io/kit-mrt/arbitration_graphs_pacman_demo:latest - ghcr.io/kit-mrt/arbitration_graphs_pacman_demo:${{ needs.compute-version.outputs.new_tag }} + ghcr.io/kit-mrt/arbitration_graphs_pacman_demo:${{ needs.compute-version.outputs.new_version }} target: demo - name: Build and push Pacman tutorial Docker image uses: docker/build-push-action@v6 with: build-args: | - VERSION=${{ needs.compute-version.outputs.new_tag }} + VERSION=${{ needs.compute-version.outputs.new_version }} context: demo file: demo/Dockerfile push: true tags: | ghcr.io/kit-mrt/arbitration_graphs_pacman_tutorial:latest - ghcr.io/kit-mrt/arbitration_graphs_pacman_tutorial:${{ needs.compute-version.outputs.new_tag }} + ghcr.io/kit-mrt/arbitration_graphs_pacman_tutorial:${{ needs.compute-version.outputs.new_version }} target: tutorial From 228c042d837fe5e34db37d876b5371a597d954a3 Mon Sep 17 00:00:00 2001 From: Nick Le Large Date: Thu, 21 Nov 2024 07:34:02 +0100 Subject: [PATCH 6/8] Remove the "v" during string sanitation to simplify major version extraction --- ...ump-version-and-create-release-and-push-docker-images.yaml | 4 ++-- .github/workflows/compute_version.sh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml b/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml index 0a45ee3e..9d4f5461 100644 --- a/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml +++ b/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml @@ -21,8 +21,8 @@ jobs: - name: Compute new version id: bump_version run: | - initial_version=$(source version && echo ${VERSION#v}) - new_version=$(.github/workflows/compute_version.sh "$initial_version" "${{ github.event.pull_request.body }}") + source version + new_version=$(.github/workflows/compute_version.sh "$VERSION" "${{ github.event.pull_request.body }}") echo "new_version=$new_version" >> $GITHUB_OUTPUT update-version-file: diff --git a/.github/workflows/compute_version.sh b/.github/workflows/compute_version.sh index 19bd1e87..f72af2f9 100755 --- a/.github/workflows/compute_version.sh +++ b/.github/workflows/compute_version.sh @@ -7,10 +7,10 @@ if [[ $# -ne 2 ]]; then exit 1 fi -initial_version=${1//[^0-9.v]/} +initial_version=${1//[^0-9.]/} input_string=${2//[^a-zA-Z0-9#]/} -initial_major=$(echo "$initial_version" | cut -d'.' -f1 | cut -d'v' -f2) +initial_major=$(echo "$initial_version" | cut -d'.' -f1) initial_minor=$(echo "$initial_version" | cut -d'.' -f2) initial_patch=$(echo "$initial_version" | cut -d'.' -f3) From 20e87b82f896e43ec20d48a7d03bcaa7084420d9 Mon Sep 17 00:00:00 2001 From: Nick Le Large Date: Thu, 21 Nov 2024 07:58:39 +0100 Subject: [PATCH 7/8] Sanitize input in workflow already Reference: https://docs.github.com/en/actions/security-for-github-actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable --- ...ump-version-and-create-release-and-push-docker-images.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml b/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml index 9d4f5461..427e29cb 100644 --- a/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml +++ b/.github/workflows/bump-version-and-create-release-and-push-docker-images.yaml @@ -20,9 +20,11 @@ jobs: - name: Compute new version id: bump_version + env: + PR_BODY: ${{ github.event.pull_request.body }} run: | source version - new_version=$(.github/workflows/compute_version.sh "$VERSION" "${{ github.event.pull_request.body }}") + new_version=$(.github/workflows/compute_version.sh "$VERSION" "${PR_BODY//[^a-zA-Z0-9#]/}") echo "new_version=$new_version" >> $GITHUB_OUTPUT update-version-file: From 3da48b29eb642c236b701f0b23dc5c3962c88e77 Mon Sep 17 00:00:00 2001 From: ll-nick <68419636+ll-nick@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:52:12 +0100 Subject: [PATCH 8/8] Apply suggestions from code review Co-authored-by: Piotr Spieker --- .github/workflows/compute_version.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/compute_version.sh b/.github/workflows/compute_version.sh index f72af2f9..feb3ee29 100755 --- a/.github/workflows/compute_version.sh +++ b/.github/workflows/compute_version.sh @@ -14,7 +14,7 @@ initial_major=$(echo "$initial_version" | cut -d'.' -f1) initial_minor=$(echo "$initial_version" | cut -d'.' -f2) initial_patch=$(echo "$initial_version" | cut -d'.' -f3) -# Determine the bump type based on PR description +# Determine the bump type based on input string if [[ "$input_string" == *"#major"* ]]; then new_major=$((initial_major + 1)) new_minor=0