Skip to content

Commit 078bdaf

Browse files
alcaeusjyemin
authored andcommitted
Add GitHub Actions based release automation (#1400)
JAVA-5479
1 parent 247d7b4 commit 078bdaf

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

.github/workflows/bump-and-tag.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# This script assumes that release X.Y.Z will always be created from X.Y.Z-SNAPSHOT"
5+
echo "Replace snapshot version with release version ${RELEASE_VERSION} in build.gradle"
6+
sed --in-place "s/version = '.*-SNAPSHOT'/version = '${RELEASE_VERSION}'/g" build.gradle
7+
8+
echo "Create package commit for ${RELEASE_VERSION}"
9+
git commit -m "Version: bump ${RELEASE_VERSION}" build.gradle
10+
11+
echo "Create release tag for ${RELEASE_VERSION}"
12+
git tag -a -m "${RELEASE_VERSION}" r${RELEASE_VERSION}
13+
14+
echo "Bump to snapshot version for ${NEXT_VERSION}"
15+
sed --in-place "s/version = '${RELEASE_VERSION}'/version = '${NEXT_VERSION}-SNAPSHOT'/g" build.gradle
16+
17+
echo "Create commit for version bump to ${NEXT_VERSION}"
18+
git commit -m "Version: bump ${NEXT_VERSION}-SNAPSHOT" build.gradle

.github/workflows/release.yml

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
name: "Release New Version"
2+
run-name: "Release ${{ inputs.version }}"
3+
4+
on:
5+
workflow_dispatch:
6+
inputs:
7+
version:
8+
description: "The version to be released (e.g. 1.2.3)"
9+
required: true
10+
type: "string"
11+
12+
jobs:
13+
prepare-release:
14+
environment: release
15+
name: "Prepare release"
16+
runs-on: ubuntu-latest
17+
permissions:
18+
# Write permission for id-token is necessary to generate a new token for the GitHub App
19+
id-token: write
20+
# Write permission for contents is to ensure we're allowed to push to the repository
21+
contents: write
22+
23+
steps:
24+
- name: "Create release output"
25+
run: echo '🎬 Release process for version ${{ env.RELEASE_VERSION }} started by @${{ github.triggering_actor }}' >> $GITHUB_STEP_SUMMARY
26+
27+
- uses: mongodb-labs/drivers-github-tools/secure-checkout@v2
28+
with:
29+
app-id: ${{ vars.APP_ID }}
30+
private-key: ${{ secrets.APP_PRIVATE_KEY }}
31+
32+
- name: "Store version numbers in env variables"
33+
# The awk command to increase the version number was copied from
34+
# StackOverflow: https://stackoverflow.com/a/61921674/3959933
35+
run: |
36+
echo RELEASE_VERSION=${{ inputs.version }} >> $GITHUB_ENV
37+
echo NEXT_VERSION=$(echo ${{ inputs.version }} | awk -F. -v OFS=. '{$NF += 1 ; print}') >> $GITHUB_ENV
38+
echo RELEASE_BRANCH=$(echo ${{ inputs.version }} | awk -F. -v OFS=. '{$NF = "x" ; print}') >> $GITHUB_ENV
39+
40+
- name: "Ensure release tag does not already exist"
41+
run: |
42+
if [[ $(git tag -l r${RELEASE_VERSION}) == r${RELEASE_VERSION} ]]; then
43+
echo '❌ Release failed: tag for version ${{ inputs.version }} already exists' >> $GITHUB_STEP_SUMMARY
44+
exit 1
45+
fi
46+
47+
# For patch releases (A.B.C where C != 0), we expect the release to be
48+
# triggered from the A.B.x maintenance branch
49+
- name: "Fail if patch release is created from wrong release branch"
50+
if: ${{ !endsWith(inputs.version, '.0') && env.RELEASE_BRANCH != github.ref_name }}
51+
run: |
52+
echo '❌ Release failed due to branch mismatch: expected ${{ inputs.version }} to be released from ${{ env.RELEASE_BRANCH }}, got ${{ github.ref_name }}' >> $GITHUB_STEP_SUMMARY
53+
exit 1
54+
55+
# For non-patch releases (A.B.C where C == 0), we expect the release to
56+
# be triggered from master or the A.B.x maintenance branch
57+
- name: "Fail if non-patch release is created from wrong release branch"
58+
if: ${{ endsWith(inputs.version, '.0') && env.RELEASE_BRANCH != github.ref_name && github.ref_name != 'master' }}
59+
run: |
60+
echo '❌ Release failed due to branch mismatch: expected ${{ inputs.version }} to be released from ${{ env.RELEASE_BRANCH }} or master, got ${{ github.ref_name }}' >> $GITHUB_STEP_SUMMARY
61+
exit 1
62+
63+
# If a non-patch release is created from a branch other than its
64+
# maintenance branch, create that branch from the current one and push it
65+
- name: "Create new release branch for non-patch release"
66+
if: ${{ endsWith(inputs.version, '.0') && env.RELEASE_BRANCH != github.ref_name }}
67+
run: |
68+
echo '🆕 Creating new release branch ${RELEASE_BRANCH} from ${{ github.ref_name }}' >> $GITHUB_STEP_SUMMARY
69+
git checkout -b ${RELEASE_BRANCH}
70+
71+
- name: "Set git author information"
72+
run: |
73+
git config user.name "${GIT_AUTHOR_NAME}"
74+
git config user.email "${GIT_AUTHOR_EMAIL}"
75+
76+
# This step bumps version numbers in build.gradle and creates git artifacts for the release
77+
- name: "Bump version numbers and create release tag"
78+
run: .github/workflows/bump-and-tag.sh
79+
80+
- name: "Push release branch and tag"
81+
run: |
82+
git push origin ${RELEASE_BRANCH}
83+
git push origin r${{ env.RELEASE_VERSION }}
84+
85+
- name: "Create draft release with generated changelog"
86+
run: |
87+
echo "RELEASE_URL=$(\
88+
gh release create r${RELEASE_VERSION} \
89+
--target ${{ env.RELEASE_BRANCH }} \
90+
--title "Java Driver ${{ env.RELEASE_VERSION }} ($(date '+%B %d, %Y'))" \
91+
--generate-notes \
92+
--draft\
93+
)" >> "$GITHUB_ENV"
94+
95+
- name: "Set summary"
96+
run: |
97+
echo '🚀 Created tag and drafted release for version [${{ env.RELEASE_VERSION }}](${{ env.RELEASE_URL }})' >> $GITHUB_STEP_SUMMARY
98+
echo '✍️ You may now update the release notes and publish the release when ready' >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)