Skip to content

Commit 500a519

Browse files
committed
Add GitHub Actions Workflow to trigger releases
1 parent 330d3b1 commit 500a519

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# TODO: should this always assume 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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
env:
13+
GH_TOKEN: ${{ github.token }}
14+
# TODO: Adding the mongodb-dbx-release-automation app to the repository will allow fetching a one-time token and pushing
15+
# changes on behalf of the app. This also allows bypassing branch protection rules
16+
# When the app was added, these values can be changed to use the app's data
17+
GIT_AUTHOR_NAME: "DBX Java Release Bot"
18+
GIT_AUTHOR_EMAIL: "[email protected]"
19+
20+
jobs:
21+
prepare-release:
22+
name: "Prepare release"
23+
runs-on: ubuntu-latest
24+
permissions:
25+
# Write permission for contents is to ensure we're allowed to push to the repository
26+
contents: write
27+
28+
steps:
29+
- name: "Create release output"
30+
run: echo '🎬 Release process for version ${{ env.RELEASE_VERSION }} started by @${{ github.triggering_actor }}' >> $GITHUB_STEP_SUMMARY
31+
32+
- uses: actions/checkout@v4
33+
with:
34+
# fetch-depth 0 is required to fetch all branches and tags
35+
fetch-depth: 0
36+
37+
- name: "Store version numbers in env variables"
38+
# The awk command to increase the version number was copied from
39+
# StackOverflow: https://stackoverflow.com/a/61921674/3959933
40+
run: |
41+
echo RELEASE_VERSION=${{ inputs.version }} >> $GITHUB_ENV
42+
echo NEXT_VERSION=$(echo ${{ inputs.version }} | awk -F. -v OFS=. '{$NF += 1 ; print}') >> $GITHUB_ENV
43+
echo RELEASE_BRANCH=$(echo ${{ inputs.version }} | awk -F. -v OFS=. '{$NF = "x" ; print}') >> $GITHUB_ENV
44+
45+
- name: "Ensure release tag does not already exist"
46+
run: |
47+
if [[ $(git tag -l r${RELEASE_VERSION}) == r${RELEASE_VERSION} ]]; then
48+
echo '❌ Release failed: tag for version ${{ inputs.version }} already exists' >> $GITHUB_STEP_SUMMARY
49+
exit 1
50+
fi
51+
52+
# For patch releases (A.B.C where C != 0), we expect the release to be
53+
# triggered from the A.B.x maintenance branch
54+
- name: "Fail if patch release is created from wrong release branch"
55+
if: ${{ !endsWith(inputs.version, '.0') && env.RELEASE_BRANCH != github.ref_name }}
56+
run: |
57+
echo '❌ Release failed due to branch mismatch: expected ${{ inputs.version }} to be released from ${{ env.RELEASE_BRANCH }}, got ${{ github.ref_name }}' >> $GITHUB_STEP_SUMMARY
58+
exit 1
59+
60+
# For non-patch releases (A.B.C where C == 0), we expect the release to
61+
# be triggered from master or the A.B.x maintenance branch
62+
- name: "Fail if non-patch release is created from wrong release branch"
63+
if: ${{ endsWith(inputs.version, '.0') && env.RELEASE_BRANCH != github.ref_name && github.ref_name != 'master' }}
64+
run: |
65+
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
66+
exit 1
67+
68+
# If a non-patch release is created from a branch other than its
69+
# maintenance branch, create that branch from the current one and push it
70+
- name: "Create new release branch for non-patch release"
71+
if: ${{ endsWith(inputs.version, '.0') && env.RELEASE_BRANCH != github.ref_name }}
72+
run: |
73+
echo '🆕 Creating new release branch ${RELEASE_BRANCH} from ${{ github.ref_name }}' >> $GITHUB_STEP_SUMMARY
74+
git checkout -b ${RELEASE_BRANCH}
75+
76+
- name: "Set git author information"
77+
run: |
78+
git config user.name "${GIT_AUTHOR_NAME}"
79+
git config user.email "${GIT_AUTHOR_EMAIL}"
80+
81+
# This step bumps version numbers in build.gradle and creates git artifacts for the release
82+
# TODO: this could leverage the git-sign action with some minor changes
83+
# See https://github.com/mongodb-labs/drivers-github-tools/tree/main?tab=readme-ov-file#git-sign
84+
- name: "Bump version numbers and create release tag"
85+
run: .github/workflows/bump-and-tag.sh
86+
87+
- name: "Push release branch and tag"
88+
run: |
89+
git push origin ${RELEASE_BRANCH}
90+
git push origin r${{ env.RELEASE_VERSION }}
91+
92+
- name: "Create draft release with generated changelog"
93+
run: |
94+
echo "RELEASE_URL=$(\
95+
gh release create r${RELEASE_VERSION} \
96+
--target ${{ env.RELEASE_BRANCH }} \
97+
--title "Java Driver ${{ env.RELEASE_VERSION }} ($(date '+%B %d, %Y'))" \
98+
--generate-notes \
99+
--draft\
100+
)" >> "$GITHUB_ENV"
101+
102+
- name: "Set summary"
103+
run: |
104+
echo '🚀 Created tag and drafted release for version [${{ env.RELEASE_VERSION }}](${{ env.RELEASE_URL }})' >> $GITHUB_STEP_SUMMARY
105+
echo '✍️ You may now update the release notes and publish the release when ready' >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)