-
Notifications
You must be signed in to change notification settings - Fork 15
161 lines (140 loc) · 6.16 KB
/
version_bump.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
name: "Version Increment"
on:
# Run this action on any Pull Request raised against ARM
pull_request:
# Ensure changes re-run the version script (i.e. title change)
types: [opened, edited, synchronize]
# Don't run on changes to the below paths
paths-ignore:
- 'arm_wiki/**'
- '.github/**'
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
permissions:
contents: write
pull-requests: write
jobs:
version:
runs-on: ubuntu-latest
steps:
- name: Get branch name
id: branch-name
uses: tj-actions/branch-names@v8
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Fetch main branch
run: |
git fetch origin main:main
- name: Check for version bump
id: version-check
run: |
if ! git diff main --name-only | grep -q 'VERSION'; then
echo "VERSION_UPDATED=false" >> "$GITHUB_ENV"
echo "No Version update found in commit"
else
echo "VERSION_UPDATED=true" >> "$GITHUB_ENV"
echo "Version update found in commit"
fi
- name: Determine new version
if: env.VERSION_UPDATED == 'false'
id: determine-version
env:
# Safely get title (avoid injection attacks)
PR_TITLE: ${{ github.event.pull_request.title }}
run: |
# Set Title environment flag to false
TITLE_FLAG="false"
echo "PR Title: $PR_TITLE"
if [[ "$PR_TITLE" == *"[FEATURE]"* ]]; then
VERSION_TYPE="minor"
echo "VERSION_TYPE=$VERSION_TYPE" >> $GITHUB_ENV
echo "PR set to FEATURE updating minor version"
TITLE_FLAG="true"
elif [[ "$PR_TITLE" == *"[BUGFIX]"* ]]; then
VERSION_TYPE="patch"
echo "VERSION_TYPE=$VERSION_TYPE" >> $GITHUB_ENV
echo "PR set to BUGFIX updating patch version"
TITLE_FLAG="true"
else
echo "No version bump flag found in PR title. Exiting."
echo "Edit your PR title to include either FEATURE or BUGFIX"
fi
# If Feature or Bugfix update the version
if [[ "$TITLE_FLAG" == "true" ]]; then
CURRENT_VERSION=$(cat VERSION)
IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION"
if [[ "$VERSION_TYPE" == "minor" ]]; then
VERSION_PARTS[1]=$((VERSION_PARTS[1]+1))
VERSION_PARTS[2]=0
elif [[ "$VERSION_TYPE" == "patch" ]]; then
VERSION_PARTS[2]=$((VERSION_PARTS[2]+1))
fi
NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.${VERSION_PARTS[2]}"
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
echo "CURRENT_VERSION=$CURRENT_VERSION" >> $GITHUB_ENV
echo "New Version: " $NEW_VERSION
fi
# Set variables to global environment variables for later
echo "TITLE_FLAG=$TITLE_FLAG" >> $GITHUB_ENV
echo "PR_TITLE=$PR_TITLE" >> $GITHUB_ENV
echo "VERSION_TYPE=$VERSION_TYPE" >> $GITHUB_ENV
- name: Configure Git user
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
- name: Pull latest changes from remote
if: env.VERSION_UPDATED == 'false' && env.TITLE_FLAG == 'true'
run: |
git pull --rebase origin ${{ github.head_ref }}
- name: Update VERSION file and Push Changes
if: env.VERSION_UPDATED == 'false' && env.TITLE_FLAG == 'true'
run: |
echo "$NEW_VERSION" > VERSION
git add VERSION
git commit -m "[Automated] Release: ${VERSION_TYPE} - Version from $CURRENT_VERSION to $NEW_VERSION"
# Use the pull request's branch name to push changes
BRANCH="${{ github.head_ref }}"
echo "Pushing changes to branch: $BRANCH"
# Push changes to the PR branch
git push origin HEAD:$BRANCH
- name: Debug Environment Variables
run: |
echo "PR_TITLE=${{ env.PR_TITLE }}"
echo "NEW_VERSION=${{ env.NEW_VERSION }}"
echo "CURRENT_VERSION=${{ env.CURRENT_VERSION }}"
echo "VERSION_TYPE=${{ env.VERSION_TYPE }}"
- name: Comment on PR
if: env.VERSION_UPDATED == 'false'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// Retrieve variables from the environment
const titleFlag = process.env.TITLE_FLAG;
const prNumber = context.issue.number;
const prTitle = process.env.PR_TITLE;
const newVersion = process.env.NEW_VERSION;
const currentVersion = process.env.CURRENT_VERSION;
const versionType = process.env.VERSION_TYPE;
// Prepare the message based on the title flag
let prBody;
if (titleFlag === "false") {
prBody = `Version Bot\n **PR title:** ${prTitle}\n **No valid version flag found**. PR title must include either [FEATURE] or [BUGFIX] to auto-increment the version.\n **Please update the PR title** and re-run the workflow.`;
} else {
prBody = `Version Bot:\n **PR title:** ${prTitle}\n Updated version: **${currentVersion}** to **${newVersion}**\n Release version: **${versionType}**`;
}
// Generate PR comment
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: prBody
})
- name: Set tag for non-default branch
if: steps.branch-name.outputs.is_default == 'false' && steps.branch-name.outputs.default_branch != '' && env.TITLE_FLAG == 'true'
run: |
echo "Branch name is ${{ steps.branch-name.outputs.ref_branch }}"
echo "Main name is ${{ steps.branch-name.outputs.default_branch }}"
echo "TAG=${{ steps.branch-name.outputs.current_branch }}" >> $GITHUB_ENV