Skip to content

Commit 8fd5809

Browse files
feat(ci): add GitHub release workflows
1 parent 9360b5d commit 8fd5809

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: 'Release: Deploy PR'
2+
3+
on:
4+
pull_request:
5+
branches: [ 'releases/v*' ]
6+
types: [ closed ]
7+
8+
jobs:
9+
release-deploy:
10+
11+
if: github.event.pull_request.merged == true # only run on PR merge
12+
runs-on: ubuntu-latest
13+
steps:
14+
15+
- name: Configure release
16+
run: |
17+
PR_TITLE=$(jq -r ".pull_request.title" $GITHUB_EVENT_PATH)
18+
PR_BODY=$(jq -r ".pull_request.body" $GITHUB_EVENT_PATH)
19+
RELEASE_TAG=$(echo "${PR_TITLE}" | grep -oP "(?<=^Release: )v\d+\.\d+\.\d+(-rc\.\d+)?$")
20+
21+
if [[ "${RELEASE_TAG}" =~ -rc\.[0-9]+$ ]]; then
22+
RELEASE_PRERELEASE=true
23+
else
24+
RELEASE_PRERELEASE=false
25+
fi
26+
27+
echo "PR_TITLE=${PR_TITLE}" >> $GITHUB_ENV
28+
echo "RELEASE_TAG=${RELEASE_TAG}" >> $GITHUB_ENV
29+
echo "RELEASE_PRERELEASE=${RELEASE_PRERELEASE}" >> $GITHUB_ENV
30+
31+
echo 'PR_BODY<<END_OF_PR_BODY' >> $GITHUB_ENV
32+
echo "${PR_BODY}" >> $GITHUB_ENV
33+
echo 'END_OF_PR_BODY' >> $GITHUB_ENV
34+
35+
- name: Create release
36+
uses: ncipollo/release-action@v1
37+
with:
38+
token: ${{ secrets.BOT_GITHUB_TOKEN }}
39+
commit: '${{ github.sha }}'
40+
tag: '${{ env.RELEASE_TAG }}'
41+
body: '${{ env.PR_BODY }}'
42+
draft: false
43+
prerelease: ${{ env.RELEASE_PRERELEASE }}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
name: 'Release: Prepare PR'
2+
3+
on:
4+
push:
5+
branches: [ develop ]
6+
7+
env:
8+
GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }}
9+
RELEASE_BRANCH: releases/v1
10+
11+
jobs:
12+
release-prepare:
13+
14+
runs-on: ubuntu-latest
15+
steps:
16+
17+
- uses: actions/checkout@v2
18+
with:
19+
fetch-depth: 0
20+
21+
# - uses: mxschmitt/action-tmate@v3
22+
23+
- name: Create/update pull request
24+
run: |
25+
# get latest release tag
26+
latest_release=$(git describe --tags --abbrev=0 "origin/${RELEASE_BRANCH}")
27+
latest_release_bumped=$(echo $latest_release | awk -F. -v OFS=. '{$NF++;print}')
28+
29+
30+
# create or update PR
31+
pr_body="$(cat <<EOF
32+
Release: ${latest_release_bumped}
33+
34+
## Improvements
35+
36+
## Technical
37+
38+
EOF
39+
)"
40+
41+
pr_number=$(hub pr list -h develop -f '%I')
42+
43+
if [ -n "${pr_number}" ]; then
44+
echo "Updating PR #${pr_number}"
45+
existing_comment_id=$(hub api "/repos/${GITHUB_REPOSITORY}/issues/${pr_number}/comments" | jq '.[] | select(.user.login=="jarvus-bot") | .id')
46+
else
47+
echo "Opening PR"
48+
hub pull-request -b "${RELEASE_BRANCH}" -h develop -F <(echo "${pr_body}") > /tmp/pr.json
49+
pr_number=$(hub pr list -h develop -f '%I')
50+
echo "Opened PR #${pr_number}"
51+
fi
52+
53+
54+
# build changelog
55+
commits=$(
56+
git log \
57+
--first-parent \
58+
--reverse \
59+
--format="%H" \
60+
"origin/${RELEASE_BRANCH}..develop"
61+
)
62+
63+
changelog=()
64+
65+
while read -r commit; do
66+
subject="$(git show -s --format=%s "${commit}")"
67+
line=""
68+
69+
if [[ "${subject}" =~ Merge\ pull\ request\ \#([0-9]+) ]]; then
70+
line="$(hub pr show -f '%t [%i] @%au' "${BASH_REMATCH[1]}" || true)"
71+
fi
72+
73+
if [ -z "${line}" ]; then
74+
author="$(hub api "/repos/${GITHUB_REPOSITORY}/commits/${commit}" -H Accept:application/vnd.github.v3+json | jq -r '.author.login')"
75+
if [ -n "${author}" ]; then
76+
author="@${author}"
77+
else
78+
author="$(git show -s --format=%ae "${commit}")"
79+
fi
80+
81+
line="${subject} ${author}"
82+
fi
83+
84+
# move ticket number prefix into to existing square brackets at end
85+
line="$(echo "${line}" | perl -pe 's/^([A-Z]+-[0-9]+):?\s*(.*?)\s*\[([^]]+)\]\s*(\S+)$/\2 [\3, \1] \4/')"
86+
87+
# move ticket number prefix into to new square brackets at end
88+
line="$(echo "${line}" | perl -pe 's/^([A-Z]+-[0-9]+):?\s*(.*?)\s*(\S+)$/\2 [\1] \3/')"
89+
90+
# combine doubled square brackets at the end
91+
line="$(echo "${line}" | perl -pe 's/^\s*(.*?)\s*\[([A-Z]+-[0-9]+)\]\s*\[([^]]+)\]\s*(\S+)$/\1 [\3, \2] \4/')"
92+
93+
changelog+=("- ${line}")
94+
done <<< "${commits}"
95+
96+
97+
# create or update comment
98+
comment_body="$(cat <<EOF
99+
## Changelog
100+
101+
\`\`\`markdown
102+
$(IFS=$'\n'; echo "${changelog[*]}")
103+
\`\`\`
104+
EOF
105+
)"
106+
107+
if [ -n "${existing_comment_id}" ]; then
108+
echo "Updating comment #${existing_comment_id}"
109+
hub api "/repos/${GITHUB_REPOSITORY}/issues/comments/${existing_comment_id}" -f body="${comment_body}"
110+
else
111+
echo "Creating comment"
112+
hub api "/repos/${GITHUB_REPOSITORY}/issues/${pr_number}/comments" -f body="${comment_body}"
113+
fi
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: 'Release: Validate PR'
2+
3+
on:
4+
pull_request:
5+
branches: [ 'releases/v*' ]
6+
types: [ opened, edited, reopened, synchronize ]
7+
8+
env:
9+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
10+
11+
jobs:
12+
release-validate:
13+
14+
runs-on: ubuntu-latest
15+
steps:
16+
17+
- name: Validate PR title
18+
run: |
19+
PR_TITLE=$(jq -r ".pull_request.title" $GITHUB_EVENT_PATH)
20+
21+
# check title format and extract tag
22+
if [[ "${PR_TITLE}" =~ ^Release:\ v[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?$ ]]; then
23+
RELEASE_TAG="${PR_TITLE:9}"
24+
echo "RELEASE_TAG=${RELEASE_TAG}" >> $GITHUB_ENV
25+
else
26+
echo 'PR title must match format "Release: vX.Y.Z(-rc.#)?"'
27+
exit 1
28+
fi
29+
30+
# check that tag doesn't exist
31+
if git ls-remote --exit-code "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}" "refs/tags/${RELEASE_TAG}"; then
32+
echo "The PR title's version exists already"
33+
exit 1
34+
fi

0 commit comments

Comments
 (0)