-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
202 lines (169 loc) · 7.3 KB
/
action.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
name: "Prevent Maven Artifact Overwrites"
description: "Prevent overwriting Maven artifacts from feature branches."
branding:
icon: 'package'
color: 'blue'
inputs:
commit-message-suffix:
description: "Text to add to the end of commit messages."
default: ""
required: false
type: string
enforce-branch-version:
required: true
type: boolean
git-user-name:
description: "GIT user name (to commit changes)"
default: "github-actions[bot]"
required: false
type: string
git-user-email:
description: "GIT user email (to commit changes)"
default: "github-actions[bot]@users.noreply.github.com"
required: false
type: string
maven-args:
description: "Additional arguments to pass to Maven commands"
default: ""
required: false
type: string
pom-file:
description: "Maven POM file"
default: "pom.xml"
required: false
type: string
push-changes:
required: true
type: boolean
outputs:
changes-made:
description: "Whether this action modified any files"
value: ${{ steps.sumarize-changes.outputs.changes-made }}
runs:
using: "composite"
steps:
- name: Check if branch version is needed
id: needs-branch-version
shell: bash
run: |
currentBranch="${GITHUB_REF_NAME}"
echo "Current branch: '$currentBranch'"
if [[ $currentBranch == "main" || $currentBranch == "master" || $currentBranch == "develop" || $currentBranch == release* ]]; then
needsBranchVersion=false
else
needsBranchVersion=true
fi
echo "Needs branch version: $needsBranchVersion"
echo "needs=$needsBranchVersion" >> $GITHUB_OUTPUT
- name: Get project version
id: get-project-version
shell: bash
run: |
projectVersion=$(mvn -B help:evaluate -Dexpression=project.version -q -DforceStdout --file ${{ inputs.pom-file }} ${{ inputs.maven-args }})
echo "Project version: $projectVersion"
echo "version=$projectVersion" >> $GITHUB_OUTPUT
- name: Set up GIT
shell: bash
run: |
git config --local user.name '${{ inputs.git-user-name }}'
git config --local user.email '${{ inputs.git-user-email }}'
- name: Enforce branch version
id: enforce-branch-version-step
if: steps.needs-branch-version.outputs.needs == 'true' # step outputs are always strings
shell: bash
run: |
# This condition needs to be inside the script (not in "if" field), because GitHub is unable to properly implement boolean inputs.
# https://github.com/actions/runner/issues/2238
if [ "${{ inputs.enforce-branch-version }}" = "false" ]; then
echo "Project version enforcement is turned off."
exit 0
fi
versionRegexp='^[0-9]+\.[0-9]+\.[0-9].*-[0-9a-zA-Z]+-SNAPSHOT$'
projectVersion=${{ steps.get-project-version.outputs.version }}
if [[ $projectVersion =~ $versionRegexp ]]; then
echo "Project does have a branch version."
echo "changes-made=false" >> $GITHUB_OUTPUT
else
branchPostfix=$(echo "${GITHUB_REF_NAME}" | tr / -)
versionWithoutSnapshot="${projectVersion%-SNAPSHOT}"
newVersion="${versionWithoutSnapshot}-${branchPostfix}-SNAPSHOT"
echo "Project does not have a branch version. I will change it automatically to ${newVersion}";
mvn -B versions:set -DnewVersion="$newVersion" -DgenerateBackupPoms=false --file ${{ inputs.pom-file }} ${{ inputs.maven-args }}
git commit -a -m "Switched to branch-specific version.${{ inputs.commit-message-suffix }}"
echo "changes-made=true" >> $GITHUB_OUTPUT
fi
- name: Remove branch version
id: remove-branch-version
if: ${{ steps.needs-branch-version.outputs.needs == 'false' }} # step outputs are always strings
shell: bash
run: |
versionRegexp='^[0-9]+\.[0-9]+\.[0-9].*-[0-9a-zA-Z]+-SNAPSHOT$'
projectVersion=${{ steps.get-project-version.outputs.version }}
if [[ $projectVersion =~ $versionRegexp ]]; then
echo "Project does have a branch version. I will remove it, since we are on main/develop/release branch."
prefix=$(echo "$projectVersion" | grep -oE "^[0-9]+\.[0-9]+\.[0-9](\-rc(\.[0-9]+)?)?")
newVersion="$prefix-SNAPSHOT"
echo "New version: $newVersion"
mvn -B versions:set -DnewVersion="$newVersion" versions:commit --file ${{ inputs.pom-file }} ${{ inputs.maven-args }}
git commit -a -m "Switched to non branch-specific version.${{ inputs.commit-message-suffix }}"
echo "changes-made=true" >> $GITHUB_OUTPUT
else
echo "changes-made=false" >> $GITHUB_OUTPUT
fi
- name: Remove dependency branch versions
id: remove-dependency-branch-versions
if: ${{ steps.needs-branch-version.outputs.needs == 'false' }} # step outputs are always strings
continue-on-error: true
shell: bash
run: |
changesMade=false
tempFile=$(mktemp)
mvn -B dependency:list -DexcludeTransitive=true -DoutputFile="$tempFile" --file ${{ inputs.pom-file }} ${{ inputs.maven-args }}
versionRegexp='^[0-9]+\.[0-9]+\.[0-9].*-[0-9a-zA-Z]+-SNAPSHOT$'
lineRegexp="^([^:]+:){4}[^:]+$"
while IFS= read -r line; do
if [[ "$line" =~ $lineRegexp ]]; then
trimmedLine="${line#"${line%%[![:space:]]*}"}" # remove leading spaces from line
echo ""
echo "Checking dependency: $trimmedLine"
IFS=':' read -ra lineParts <<< "$trimmedLine"
group="${lineParts[0]}"
artifact="${lineParts[1]}"
version="${lineParts[3]}"
if [[ $version =~ $versionRegexp ]]; then
echo "Found a branch-specific version for ${group}:${artifact}. I will remove it."
prefix=$(echo "$version" | grep -oE "^[0-9]+\.[0-9]+\.[0-9](\-rc(\.[0-9]+)?)?")
newVersion="$prefix-SNAPSHOT"
echo "New version: $newVersion"
mvn -B versions:use-dep-version -DprocessProperties=true -Dincludes=${group}:${artifact} -DdepVersion=${newVersion} -DforceVersion=true --file ${{ inputs.pom-file }} ${{ inputs.maven-args }}
changesMade=true
fi
fi
done < "$tempFile"
if [ "$changesMade" = true ]; then
git commit -a -m "Switched to non branch dependency versions.${{ inputs.commit-message-suffix }}"
echo "changes-made=true" >> $GITHUB_OUTPUT
else
echo "changes-made=false" >> $GITHUB_OUTPUT
fi
- name: Sumarize changes
id: sumarize-changes
shell: bash
run: |
if [[ "${{ steps.enforce-branch-version-step.outputs.changes-made }}" = "true" || "${{ steps.remove-branch-version.outputs.changes-made }}" = "true" || "${{ steps.remove-dependency-branch-versions.outputs.changes-made }}" = "true" ]]; then
echo "Changes have been made."
echo "changes-made=true" >> $GITHUB_OUTPUT
else
echo "No changes have been made."
echo "changes-made=false" >> $GITHUB_OUTPUT
fi
- name: Push changes
if: steps.sumarize-changes.outputs.changes-made == 'true' # step outputs are always strings
shell: bash
run: |
# This condition needs to be inside the script (not in "if" field), because GitHub is unable to properly implement boolean inputs.
# https://github.com/actions/runner/issues/2238
if [ "${{ inputs.push-changes }}" = "true" ]; then
echo "Pushing changes:"
git push
fi