-
Notifications
You must be signed in to change notification settings - Fork 0
66 lines (53 loc) · 2.32 KB
/
build-number.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
name: Build Number
on:
workflow_call:
outputs:
build-number:
description: "The build number generated from git tags on current SHA."
value: ${{ jobs.build-number.outputs.build-number }}
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-number:
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" | 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"