Skip to content

Commit 205f090

Browse files
algobarbexcalq
andauthored
CICD: Add create-release-pr workflow (#593)
* add create-release-pr workflow * fix comment * move the bump_version script and fix update changelog step, remove reviewers * Update scripts/bump_version.py Co-authored-by: Arthur Kepler <[email protected]> * generalize variable name --------- Co-authored-by: Arthur Kepler <[email protected]>
1 parent adbda95 commit 205f090

File tree

2 files changed

+268
-0
lines changed

2 files changed

+268
-0
lines changed
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
name: Create Release PR
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
release_version:
7+
description: 'The release_version used for the release branch name, e.g. release/x.x.x'
8+
default: 'x.x.x'
9+
required: true
10+
type: string
11+
pre_release_version:
12+
description: "Pre-Release version, e.g. 'beta-1'"
13+
required: false
14+
type: string
15+
16+
env:
17+
RELEASE_VERSION: ${{ inputs.release_version }}
18+
PRE_RELEASE_VERSION: ${{ inputs.pre_release_version }}
19+
RELEASE_BRANCH: release/${{ inputs.release_version }}
20+
21+
jobs:
22+
create-release-pr:
23+
runs-on: ubuntu-latest
24+
25+
steps:
26+
- name: Set Release Version and Branch to Check Out
27+
id: set-release
28+
run: |
29+
if [[ $RELEASE_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
30+
if [[ $PRE_RELEASE_VERSION =~ ^[-a-z0-9]+$ ]]; then
31+
echo "release-tag: $RELEASE_VERSION-$PRE_RELEASE_VERSION"
32+
echo "release-tag=$RELEASE_VERSION-$PRE_RELEASE_VERSION" >> $GITHUB_OUTPUT
33+
elif [[ -n $PRE_RELEASE_VERSION ]]; then
34+
echo "Input pre_release_version is not empty, but does not match the regex pattern ^[-a-z0-9]+$"
35+
exit 1
36+
else
37+
echo "release-tag: $RELEASE_VERSION"
38+
echo "release-tag=$RELEASE_VERSION" >> $GITHUB_OUTPUT
39+
fi
40+
else
41+
echo "Version input doesn't match the regex pattern ^[0-9]+\.[0-9]+\.[0-9]+$"
42+
exit 1
43+
fi
44+
45+
- name: Checkout
46+
uses: actions/checkout@v3
47+
with:
48+
fetch-depth: 0
49+
50+
- name: Create Release Branch if it does not exist
51+
run: |
52+
if ! git show-ref --verify --quiet "refs/remotes/origin/$RELEASE_BRANCH"; then
53+
git checkout -b $RELEASE_BRANCH
54+
git push --set-upstream origin $RELEASE_BRANCH
55+
elif [[ $(git rev-parse --abbrev-ref HEAD) != "$RELEASE_BRANCH" ]]; then
56+
echo "Current Branch: $(git rev-parse --abbrev-ref HEAD)"
57+
echo "Release branch exists, make sure you're using the workflow from the release branch or delete the existing release branch."
58+
exit 1
59+
else
60+
echo "Release branch exists and used as workflow ref."
61+
fi
62+
63+
- name: Get Latest Release
64+
id: get-release
65+
run: |
66+
if [[ -n $PRE_RELEASE_VERSION ]]; then
67+
echo "Get the latest release"
68+
tag=$(curl -L \
69+
--header "Accept: application/vnd.github.v3+json" \
70+
"https://api.github.com/repos/${{ github.repository }}/releases" | jq -r '.[0].tag_name')
71+
echo "latest-tag=$tag" >> $GITHUB_OUTPUT
72+
else
73+
echo "Get the latest stable release"
74+
tag=$(curl -L \
75+
--header "Accept: application/vnd.github.v3+json" \
76+
"https://api.github.com/repos/${{ github.repository }}/releases/latest" | jq -r '.tag_name')
77+
echo "latest-tag=$tag" >> $GITHUB_OUTPUT
78+
fi
79+
80+
- name: Build Changelog
81+
uses: mikepenz/[email protected]
82+
id: build-changelog
83+
env:
84+
PREVIOUS_VERSION: ${{ steps.get-release.outputs.latest-tag }}
85+
with:
86+
fromTag: ${{ env.PREVIOUS_VERSION }}
87+
toTag: ${{ env.RELEASE_BRANCH }}
88+
failOnError: true
89+
configurationJson: |
90+
{
91+
"categories": [
92+
{
93+
"title": "## New Features",
94+
"labels": [
95+
"New Feature"
96+
]
97+
},
98+
{
99+
"title": "## Enhancements",
100+
"labels": [
101+
"Enhancement"
102+
]
103+
},
104+
{
105+
"title": "## Bug Fixes",
106+
"labels": [
107+
"Bug-Fix"
108+
]
109+
},
110+
{
111+
"title": "## Not Yet Enabled",
112+
"labels": [
113+
"Not-Yet-Enabled"
114+
]
115+
}
116+
],
117+
"ignore_labels": [
118+
"Skip-Release-Notes"
119+
],
120+
"sort": {
121+
"order": "ASC",
122+
"on_property": "mergedAt"
123+
},
124+
"template": "#{{CHANGELOG}}",
125+
"pr_template": "- #{{TITLE}} by @#{{AUTHOR}} in ##{{NUMBER}}"
126+
}
127+
128+
- name: Update Changelog
129+
if: ${{ env.PRE_RELEASE_VERSION == '' }}
130+
env:
131+
CHANGELOG_CONTENT: ${{ steps.build-changelog.outputs.changelog }}
132+
PREVIOUS_VERSION: ${{ steps.get-release.outputs.latest-tag }}
133+
run: |
134+
echo -e "# ${RELEASE_VERSION}\n\n${CHANGELOG_CONTENT}**Full Changelog**: https://github.com/${{ github.repository }}/compare/${PREVIOUS_VERSION}...${RELEASE_VERSION}\n" | cat - CHANGELOG.md > temp && mv temp CHANGELOG.md
135+
136+
- name: Update Version References in Source
137+
env:
138+
RELEASE_TAG: ${{ steps.set-release.outputs.release-tag }}
139+
run: |
140+
python3 scripts/bump_version.py ${RELEASE_TAG}
141+
142+
- name: Commit Changes
143+
uses: EndBug/[email protected]
144+
env:
145+
RELEASE_TAG: ${{ steps.set-release.outputs.release-tag }}
146+
with:
147+
message: "bump up version to ${{ env.RELEASE_TAG }}"
148+
149+
- name: Create Pull Request to Master
150+
env:
151+
CHANGELOG_CONTENT: ${{ steps.build-changelog.outputs.changelog }}
152+
PREVIOUS_VERSION: ${{ steps.get-release.outputs.latest-tag }}
153+
GH_TOKEN: ${{ github.token }}
154+
RELEASE_TAG: ${{ steps.set-release.outputs.release-tag }}
155+
run: |
156+
echo -e "# What's Changed\n\n${CHANGELOG_CONTENT}**Full Changelog**: https://github.com/${{ github.repository }}/compare/${PREVIOUS_VERSION}...${RELEASE_TAG}" > tmp_msg_body.txt
157+
export msg_body=$(cat tmp_msg_body.txt)
158+
rm tmp_msg_body.txt
159+
# Note: There's an issue adding teams as reviewers, see https://github.com/cli/cli/issues/6395
160+
PULL_REQUEST_URL=$(gh pr create --base "master" \
161+
--title "FOR REVIEW ONLY: ${{ github.event.repository.name }} $RELEASE_TAG" \
162+
--label "Skip-Release-Notes" \
163+
--label "Team Hyper Flow" \
164+
--body "$msg_body" | tail -n 1)
165+
if [[ $PULL_REQUEST_URL =~ ^https://github.com/${{ github.repository }}/pull/[0-9]+$ ]]; then
166+
PULL_REQUEST_NUM=$(echo $PULL_REQUEST_URL | sed 's:.*/::')
167+
echo "pull-request-master=$PULL_REQUEST_URL" >> $GITHUB_ENV
168+
echo "pull-request-master-num=$PULL_REQUEST_NUM" >> $GITHUB_ENV
169+
echo "Pull request to Master created: $PULL_REQUEST_URL"
170+
else
171+
echo "There was an issue creating the pull request to master branch."
172+
exit 1
173+
fi
174+
175+
- name: Create Pull Request to Develop
176+
if: ${{ env.PRE_RELEASE_VERSION == '' }}
177+
env:
178+
GH_TOKEN: ${{ github.token }}
179+
RELEASE_TAG: ${{ steps.set-release.outputs.release-tag }}
180+
run: |
181+
# Note: There's an issue adding teams as reviewers, see https://github.com/cli/cli/issues/6395
182+
PULL_REQUEST_URL=$(gh pr create --base "develop" \
183+
--title "FOR REVIEW ONLY: Merge back ${{ github.event.repository.name }} $RELEASE_TAG to develop" \
184+
--label "Skip-Release-Notes" \
185+
--label "Team Hyper Flow" \
186+
--body "Merge back version changes to develop." | tail -n 1)
187+
if [[ $PULL_REQUEST_URL =~ ^https://github.com/${{ github.repository }}/pull/[0-9]+$ ]]; then
188+
echo "Pull request to Develop created: $PULL_REQUEST_URL"
189+
DEVELOP_PR_MESSAGE="\nPull Request to develop: $PULL_REQUEST_URL"
190+
echo "pull-request-develop-message=$DEVELOP_PR_MESSAGE" >> $GITHUB_ENV
191+
else
192+
echo "There was an issue creating the pull request to develop branch."
193+
exit 1
194+
fi
195+
196+
- name: Send Slack Message
197+
id: slack
198+
uses: slackapi/[email protected]
199+
env:
200+
RELEASE_TAG: ${{ steps.set-release.outputs.release-tag }}
201+
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
202+
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
203+
SDK_DEPLOYMENT_URL: ${{ secrets.SDK_DEPLOYMENT_URL }}
204+
with:
205+
payload: |
206+
{
207+
"blocks": [
208+
{
209+
"type": "header",
210+
"text": {
211+
"type": "plain_text",
212+
"text": "${{ github.event.repository.name }} Release PR for ${{ env.RELEASE_TAG }}"
213+
}
214+
},
215+
{
216+
"type": "section",
217+
"text": {
218+
"type": "mrkdwn",
219+
"text": "*Approvals needed for*:\nPull Request to master: ${{ env.pull-request-master}}${{ env.pull-request-develop-message }}"
220+
}
221+
},
222+
{
223+
"type": "section",
224+
"text": {
225+
"type": "mrkdwn",
226+
"text": "*After approvals*\nDeploy SDK using the <${{ env.SDK_DEPLOYMENT_URL }}|Deployment Pipeline> with the following parameters:\n*SDK*: ${{ github.event.repository.name }}\n*RELEASE_PR_NUM*: ${{ env.pull-request-master-num }}\n*RELEASE_VERSION*: ${{ env.RELEASE_VERSION }}\n*PRE_RELEASE_VERSION*: ${{ env.PRE_RELEASE_VERSION }}"
227+
}
228+
}
229+
]
230+
}

scripts/bump_version.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This script bumps up the version in `pom.xml` and `README.md` for new releases.
2+
# Usage: python bump_version.py {new_version} (--read_me <path-to-README.md> --pom_xml <path-to-pom-xml>)
3+
4+
import argparse
5+
import re
6+
7+
def bump_version(new_version, file_path):
8+
with open(file_path, "r") as file:
9+
content = file.read()
10+
11+
# Replace first instance of <version></version>
12+
new_content = re.sub(
13+
'<version>[0-9]+\.[0-9]+\.[-a-z0-9]+</version>',
14+
f'<version>{new_version}</version>',
15+
content, 1
16+
)
17+
18+
with open(file_path, "w") as file:
19+
file.write(new_content)
20+
21+
if __name__ == "__main__":
22+
parser = argparse.ArgumentParser(
23+
description="updates the version for a release",
24+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
25+
)
26+
parser.add_argument("new_version", help="New Version as major.minor.patch")
27+
parser.add_argument(
28+
"--read_me", default="README.md", help="path to README.md"
29+
)
30+
parser.add_argument(
31+
"--pom_xml", default="pom.xml", help="path to pom.xml"
32+
)
33+
34+
args = parser.parse_args()
35+
36+
bump_version(args.new_version, args.read_me)
37+
bump_version(args.new_version, args.pom_xml)
38+

0 commit comments

Comments
 (0)