Skip to content
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
7 changes: 7 additions & 0 deletions .github/workflows/test-build-number.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ jobs:
run: |
echo "BUILD_NUMBER: ${BUILD_NUMBER}"
[[ "${BUILD_NUMBER}" =~ ^[0-9]+$ ]]
if [[ -n "$(git status --porcelain)" ]]; then
echo "::error title=test-build-number-generation::Workspace polluted after get-build-number:"
git status --porcelain
exit 1
fi

- uses: ./get-build-number
id: get_build_number_second_call
Expand Down Expand Up @@ -59,6 +64,7 @@ jobs:
echo -e "::error title=test-build-number-reuse::Build number '${BUILD_NUMBER}' does not match the previous job build number" \
"'${{ needs.test-build-number-generation.outputs.BUILD_NUMBER }}' despite it is the same workflow run.\n" \
"Prefer using the output from SonarSource/ci-github-actions/get-build-number instead of calling it from distinct jobs."
# exit 1 # flaky test
fi

test-build-number-reuse-from-cache-windows:
Expand All @@ -80,6 +86,7 @@ jobs:
echo -e "::error title=test-build-number-reuse-from-cache-windows::Build number '${BUILD_NUMBER}' does not match the previous" \
"job build number '${{ needs.test-build-number-generation.outputs.BUILD_NUMBER }}' despite it is the same workflow run.\n" \
"Prefer using the output from SonarSource/ci-github-actions/get-build-number instead of calling it from distinct jobs."
# exit 1 # flaky test
fi

test-build-number-reuse-from-env:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/.idea/
/coverage/
/.scannerwork/
/build_number.txt
/.shellspec-quick.log
package.json
.DS_Store
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ and set it as an environment variable named `BUILD_NUMBER`, and as a GitHub Acti

The build number is unique per workflow run ID. It is not incremented on workflow reruns.

During execution the action temporarily writes `.build_number.txt` at the repository root (for
`actions/cache`); the file is removed before the action completes. Do not track a file named
`.build_number.txt` in your repository.

### Requirements

#### Required GitHub Permissions
Expand Down
7 changes: 6 additions & 1 deletion get-build-number/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ runs:
fi
echo "ACTION_PATH_GET_BUILD_NUMBER=$ACTION_PATH_GET_BUILD_NUMBER"
echo "ACTION_PATH_GET_BUILD_NUMBER=$ACTION_PATH_GET_BUILD_NUMBER" >> "$GITHUB_ENV"
echo "BUILD_NUMBER_FILE=${RUNNER_TEMP}/build_number.txt" >> "$GITHUB_ENV"
echo "BUILD_NUMBER_FILE=.build_number.txt" >> "$GITHUB_ENV"
echo "::endgroup::"

# Reuse build number from environment if provided (e.g. from a parent workflow)
Expand Down Expand Up @@ -82,3 +82,8 @@ runs:
path: ${{ env.BUILD_NUMBER_FILE }}
key: build-number-${{ github.run_id }}
enableCrossOsArchive: true

- name: Remove build number file from workspace
if: always()
shell: bash
run: rm -f "$BUILD_NUMBER_FILE"
Comment thread
matemoln marked this conversation as resolved.
6 changes: 3 additions & 3 deletions get-build-number/get_build_number.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/bash
# Get the build number for a GitHub repository and save the incremented value to build_number.txt
# Get the build number for a GitHub repository and save the incremented value to .build_number.txt

set -euo pipefail

: "${GITHUB_REPOSITORY:?}"
GH_API_VERSION_HEADER="X-GitHub-Api-Version: 2022-11-28"
CACHE_FILE="${BUILD_NUMBER_FILE:-${RUNNER_TEMP}/build_number.txt}"
BUILD_NUMBER_FILE="${BUILD_NUMBER_FILE:-.build_number.txt}"

echo "Fetching build number from repository properties..."
PROPERTIES_API_URL="repos/${GITHUB_REPOSITORY}/properties/values"
Expand All @@ -21,4 +21,4 @@ gh api --method PATCH -H "$GH_API_VERSION_HEADER" "$PROPERTIES_API_URL" \
-f "properties[][property_name]=build_number" \
-f "properties[][value]=${BUILD_NUMBER}"
echo "Incremented 'build_number' repository property to ${BUILD_NUMBER}"
echo "${BUILD_NUMBER}" > "$CACHE_FILE"
echo "${BUILD_NUMBER}" > "$BUILD_NUMBER_FILE"
9 changes: 4 additions & 5 deletions spec/get_build_number_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
eval "$(shellspec - -c) exit 1"

export GITHUB_REPOSITORY="my org/my-repo"
export RUNNER_TEMP="${SHELLSPEC_TMPBASE:-/tmp}"
export BUILD_NUMBER_FILE="${RUNNER_TEMP}/build_number.txt"
CACHE_FILE="${BUILD_NUMBER_FILE}"
TEMP_DIR="${SHELLSPEC_TMPBASE:-/tmp}"
export BUILD_NUMBER_FILE="${TEMP_DIR}/build_number.txt"

Mock gh
echo "gh $*"
Expand All @@ -28,8 +27,8 @@ Describe 'get_build_number.sh'
The line 1 should include "Fetching build number"
The line 2 should equal "Current build number from repo: 42"
The line 3 should include "43"
The path "$CACHE_FILE" should be file
The contents of file "$CACHE_FILE" should equal "43"
The path "$BUILD_NUMBER_FILE" should be file
The contents of file "$BUILD_NUMBER_FILE" should equal "43"
# The variable BUILD_NUMBER should equal "43"
End

Expand Down
2 changes: 1 addition & 1 deletion spec/update-release-channel_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Describe 'update-release-channel/update-release-channel.sh'
It 'produces a JSON body that validates against schema/v1.json'
stdout_file=$(mktemp)
bash update-release-channel/update-release-channel.sh > "$stdout_file"
body_file=$(mktemp --suffix=.json)
body_file=$(mktemp)
extract_body "$stdout_file" > "$body_file"
When call check-jsonschema --schemafile update-release-channel/schema/v1.json "$body_file"
The status should be success
Expand Down
Loading