Add version code generation in CI #6
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build Number | ||
on: | ||
workflow_call: | ||
outputs: | ||
build-number: | ||
description: "The build number generated from git tags on current SHA." | ||
value: ${{ jobs.build.outputs.build-number }} | ||
type: integer | ||
inputs: | ||
token: | ||
description: 'GitHub token or PAT token' | ||
required: false | ||
default: ${{ github.token }} | ||
type: string | ||
concurrency: | ||
group: ${{ github.ref }} | ||
cancel-in-progress: true | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
outputs: | ||
build_number: ${{ steps.build-number.outputs.build_number }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Generate build number | ||
id: build-number | ||
env: | ||
INPUT_GITHUB_TOKEN: ${{ inputs.token }} | ||
run: | | ||
CURRENT_BUILD_NUMBER=$(git show-ref --tags | grep "$GITHUB_SHA" | sort -nr | head -1 | grep -oE "build-number--(\d+)" | grep -oE "\d+") | ||
echo "Found build number $CURRENT_BUILD_NUMBER for current SHA." | ||
if [ -z "${CURRENT_BUILD_NUMBER}" ]; then | ||
echo "No build number found for current SHA" | ||
# Get the latest build number | ||
LATEST_BUILD_NUMBER=$(git show-ref --tags | grep -oE "build-number-(\d+)" | sort -nr | head -1 | grep -oE "\d+") | ||
if [ -z "${LATEST_BUILD_NUMBER}" ]; then | ||
echo "No build number found in tags. Please create a tag with the format 'build-number-1' on main branch." | ||
exit 1 | ||
fi | ||
CURRENT_BUILD_NUMBER="$((LATEST_BUILD_NUMBER + 1))" | ||
git tag "build-number-$CURRENT_BUILD_NUMBER" | ||
git config --local user.email "github-actions[bot]@users.noreply.github.com" | ||
git config --local user.name "github-actions[bot]" | ||
GITHUB_URL_PROTOCOL=$(echo "${GITHUB_SERVER_URL%?(:)//*}" | ||
# "$GITHUB_SERVER_URL" | sed -E "s/https?:\/\///g") | ||
REMOTE_REPO="${GITHUB_URL_PROTOCOL}//oauth2:${INPUT_GITHUB_TOKEN}@${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" | ||
# PUSH_PARAMETERS="${REMOTE_REPO} HEAD:${GITHUB_REF}" | ||
PUSH_PARAMETERS="${REMOTE_REPO}" | ||
echo "Pushing build number tag build-number-$CURRENT_BUILD_NUMBER to Github ($GITHUB_REF)." | ||
git push "$PUSH_PARAMETERS" --tags | ||
else | ||
echo "Skipping tag creation." | ||
exit 0 | ||
fi | ||
echo "BUILD_NUMBER=$CURRENT_BUILD_NUMBER" > "$GITHUB_ENV" |