Skip to content

Commit 4c54cbb

Browse files
committed
Add workflow for automated version increments in pull-requests
1 parent dbaf85a commit 4c54cbb

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed

.github/workflows/checkVersions.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Check for version increments and apply them if necessary
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
botName:
7+
description: The name of the bot that adds the necessary version increment changes
8+
type: string
9+
required: true
10+
botMail:
11+
description: The name of the bot that adds the necessary version increment changes
12+
type: string
13+
required: true
14+
15+
permissions: read-all
16+
17+
jobs:
18+
versions-check-and-increment:
19+
name: Check and increment service versions
20+
runs-on: ubuntu-latest
21+
steps:
22+
23+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
24+
with:
25+
fetch-depth: 0 # required for jgit timestamp provider to work
26+
27+
- name: Set up Java
28+
uses: actions/setup-java@6a0805fcefea3d4657a47ac4c165951e33482018 # v4.2.2
29+
with:
30+
java-version: 17
31+
distribution: 'temurin'
32+
cache: maven
33+
34+
- name: Set up Maven
35+
uses: stCarolas/setup-maven@d6af6abeda15e98926a57b5aa970a96bb37f97d1 # v5
36+
with:
37+
maven-version: 3.9.9
38+
39+
- name: Check and increment versions
40+
uses: Wandalen/wretry.action@6feedb7dedadeb826de0f45ff482b53b379a7844 # master
41+
with:
42+
attempt_delay: 200
43+
attempt_limit: 10
44+
command: >
45+
mvn generate-sources -Pfast-version-check
46+
org.eclipse.tycho:tycho-versions-plugin:bump-versions -Dtycho.bump-versions.increment=100
47+
--threads 1C --fail-at-end --batch-mode --no-transfer-progress --show-version
48+
49+
- name: Commit version increments, if any
50+
run: |
51+
set -x
52+
if [[ $(git status --ignore-submodules --porcelain) != '' ]]; then
53+
# Workspace is not clean, i.e. some version were changed
54+
55+
# Read 'releaseNumberSDK' property as stream version
56+
streamVersion=$(mvn help:evaluate -Dexpression=releaseNumberSDK --quiet -DforceStdout)
57+
58+
git config --global user.email '${{ inputs.botMail }}'
59+
git config --global user.name '${{ inputs.botName }}'
60+
git add --all
61+
git status
62+
git commit -m "Bump version(s) for ${streamVersion} stream"
63+
64+
git format-patch -1 HEAD --no-stat --output 'version_increments.patch'
65+
66+
echo '${{ github.event.pull_request.number }}' > 'github_pull_request_number.txt'
67+
68+
else
69+
echo 'No version increments required'
70+
fi
71+
72+
- uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
73+
with:
74+
name: versions-git-patch
75+
path: |
76+
version_increments.patch
77+
github_pull_request_number.txt
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
name: Publish Version Check Results
2+
on:
3+
workflow_call:
4+
secrets:
5+
githubBotPAT:
6+
description: The personal access token (with scope 'public_repo') of the bot to push a required change to a PR branch in a fork.
7+
required: false
8+
9+
permissions: read-all
10+
11+
jobs:
12+
versions-check-result:
13+
name: Publish Version Check Results
14+
runs-on: ubuntu-latest
15+
if: github.event.workflow_run.conclusion != 'skipped'
16+
17+
steps:
18+
- name: Dump GitHub context
19+
run: echo '${{ toJSON(github) }}'
20+
21+
- name: Search version increment git patch
22+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
23+
id: search-patch
24+
with:
25+
script: |
26+
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
27+
run_id: context.payload.workflow_run.id,
28+
...context.repo
29+
})
30+
let artifact = allArtifacts.data.artifacts.find(artifact => artifact.name == 'versions-git-patch')
31+
return artifact?.id
32+
33+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
34+
if: steps.search-patch.outputs.result
35+
with:
36+
ref: '${{ github.event.workflow_run.head_sha }}'
37+
persist-credentials: false #Opt out from persisting the default Github-token authentication in order to enable use of the bot's PAT when pushing below
38+
39+
- name: Download version increment git patch
40+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
41+
id: fetch-patch
42+
if: steps.search-patch.outputs.result
43+
with:
44+
script: |
45+
let download = await github.rest.actions.downloadArtifact({
46+
artifact_id: ${{ steps.search-patch.outputs.result }},
47+
archive_format: 'zip',
48+
...context.repo
49+
})
50+
let fs = require('fs')
51+
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/patch.zip`, Buffer.from(download.data))
52+
await exec.exec('unzip', ['patch.zip'])
53+
let pr_number = Number(fs.readFileSync('github_pull_request_number.txt'))
54+
core.setOutput('pull_request_number', pr_number)
55+
await io.rmRF('patch.zip')
56+
await io.rmRF('github_pull_request_number.txt')
57+
58+
- name: Apply and push version increment
59+
id: git-commit
60+
if: steps.search-patch.outputs.result
61+
run: |
62+
set -x
63+
# Set initial placeholder name/mail and read it from the patch later
64+
git config --global user.email 'foo@bar'
65+
git config --global user.name 'Foo Bar'
66+
67+
git am version_increments.patch
68+
69+
# Read the author's name+mail from the just applied patch and recommit it with both set as committer
70+
botMail=$(git log -1 --pretty=format:'%ae')
71+
botName=$(git log -1 --pretty=format:'%an')
72+
git config --global user.email "${botMail}"
73+
git config --global user.name "${botName}"
74+
git commit --amend --no-edit
75+
76+
fileList=$(git diff-tree --no-commit-id --name-only HEAD -r)
77+
echo "file-list<<EOF" >> $GITHUB_OUTPUT
78+
echo "$fileList" >> $GITHUB_OUTPUT
79+
echo "EOF" >> $GITHUB_OUTPUT
80+
81+
git push \
82+
"https://oauth2:${BOT_PA_TOKEN}@github.com/${{ github.event.workflow_run.head_repository.full_name }}.git" \
83+
'HEAD:refs/heads/${{ github.event.workflow_run.head_branch }}'
84+
env:
85+
BOT_PA_TOKEN: ${{ secrets.githubBotPAT }}
86+
87+
88+
- name: Add information comment
89+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
90+
if: steps.search-patch.outputs.result
91+
with:
92+
github-token: ${{ secrets.githubBotPAT }}
93+
script: |
94+
let prNumber = ${{ steps.fetch-patch.outputs.pull_request_number }}
95+
const fs = require('fs')
96+
const fileList = `${{ steps.git-commit.outputs.file-list }}`
97+
if (fileList) { // if list is empty, no versions were changed
98+
github.rest.issues.createComment({
99+
issue_number: prNumber, ...context.repo, body: `
100+
This pull request changes some projects for the first time in this development.
101+
Therefore the following files need a version increment:
102+
\`\`\`
103+
${fileList}
104+
\`\`\`
105+
An additional commit containing all the necessary changes was pushed to the top of this PR's branch. To obtain these changes (for example if you want to push more changes) either fetch from your fork or apply the _git patch_.
106+
<details>
107+
<summary>Git patch</summary>
108+
109+
\`\`\`
110+
${ fs.readFileSync( process.env.GITHUB_WORKSPACE + '/version_increments.patch', {encoding: 'utf8'}).trim() }
111+
\`\`\`
112+
</details>
113+
`.trim()
114+
})
115+
}
116+
117+
118+
# TODO: Add an FAQ about version bumps?
119+
# - Why are the version bumps necessary at all?
120+
# According to the rules of semantic versioning at least the service version of a bundle should be increased if anything changes between releases.
121+
# - Why is the service version bumped by +100
122+
# Allows service version increments on maintanance branches that stay semnatically smaller than the next releases version
123+
# - Why a separate commit?
124+
# Easier to identify in Git history and the actual change is easier to revert (version must not be reverted)
125+
# TODO: make comment updatable?

0 commit comments

Comments
 (0)