Skip to content

Commit 2330a1a

Browse files
authored
ref(ci): Open a PR before releasing a new version (#318)
1 parent 76839d1 commit 2330a1a

File tree

3 files changed

+91
-21
lines changed

3 files changed

+91
-21
lines changed

.github/workflows/docker-build.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ on:
77
description: "Docker image (e.g. docker.io/<org>/<repo>)"
88
type: string
99
required: true
10+
checkout_ref:
11+
description: "Git ref (branch, SHA, or tag) to check out"
12+
type: string
13+
required: false
1014
file:
1115
description: "Path to Dockerfile relative to the repository root"
1216
type: string
@@ -36,7 +40,14 @@ jobs:
3640
name: Build Docker Image
3741
runs-on: ubuntu-latest
3842
steps:
39-
- name: Checkout
43+
- name: Checkout (specific ref)
44+
if: inputs.checkout_ref != ''
45+
uses: actions/checkout@v4
46+
with:
47+
ref: ${{ inputs.checkout_ref }}
48+
49+
- name: Checkout (default)
50+
if: inputs.checkout_ref == ''
4051
uses: actions/checkout@v4
4152

4253
- name: Set up Docker Buildx

.github/workflows/prepare-release.yml

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ on:
1818

1919
permissions:
2020
contents: write
21+
pull-requests: write
2122

2223
jobs:
2324
prepare-release:
@@ -65,16 +66,21 @@ jobs:
6566
# and prepend it to the existing changelog.
6667
git cliff --config cliff.toml --tag "v${NEXT_VERSION}" --unreleased --prepend CHANGELOG.md
6768
68-
- name: Commit and Tag Release
69-
env:
70-
NEXT_VERSION: ${{ steps.ver.outputs.next }}
71-
run: |
72-
set -euo pipefail
73-
git config user.name "github-actions[bot]"
74-
git config user.email "github-actions[bot]@users.noreply.github.com"
75-
git add .
76-
git commit -m "chore(release): v${NEXT_VERSION}"
77-
git tag -a "v${NEXT_VERSION}" -m "Release v${NEXT_VERSION}"
78-
# Push to main and tags; assume release is performed from main.
79-
git push origin HEAD:main
80-
git push origin --tags
69+
- name: Create Release Pull Request
70+
uses: peter-evans/create-pull-request@v6
71+
with:
72+
commit-message: chore(release): v${{ steps.ver.outputs.next }}
73+
title: chore(release): v${{ steps.ver.outputs.next }}
74+
body: |
75+
This PR prepares release v${{ steps.ver.outputs.next }}.
76+
77+
- Bumps workspace versions.
78+
- Updates CHANGELOG.md with unreleased changes.
79+
80+
Merging this PR into `main` will trigger the release workflow which tags the commit, publishes Docker images, and creates the GitHub Release.
81+
branch: release/v${{ steps.ver.outputs.next }}
82+
base: main
83+
labels: |
84+
release
85+
automated
86+
delete-branch: true

.github/workflows/release.yml

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ concurrency:
55
cancel-in-progress: false
66

77
on:
8-
push:
9-
tags:
10-
- 'v*'
8+
pull_request:
9+
types: [closed]
10+
branches: [main]
1111
workflow_dispatch:
1212
inputs:
1313
version:
@@ -22,9 +22,17 @@ jobs:
2222
version:
2323
name: Determine Version
2424
runs-on: ubuntu-latest
25+
# Gate: manual dispatch OR merged PRs labeled 'release'.
26+
if: |
27+
github.event_name == 'workflow_dispatch' || (
28+
github.event_name == 'pull_request' &&
29+
github.event.pull_request.merged == true &&
30+
contains(github.event.pull_request.labels.*.name, 'release')
31+
)
2532
outputs:
2633
tag: ${{ steps.ver.outputs.tag }}
2734
version: ${{ steps.ver.outputs.version }}
35+
is_pr_merge: ${{ steps.ver.outputs.is_pr_merge }}
2836
steps:
2937
- name: Extract Tag and Version
3038
id: ver
@@ -34,9 +42,18 @@ jobs:
3442
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
3543
RAW="${{ inputs.version }}"
3644
TAG="v${RAW#v}"
45+
PR_MERGE=false
3746
else
38-
TAG="${GITHUB_REF_NAME}"
39-
RAW="${TAG#v}"
47+
# pull_request closed event (merged into main)
48+
HEAD_REF="${{ github.event.pull_request.head.ref }}"
49+
if [[ "$HEAD_REF" =~ ^release/v([0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
50+
RAW="${BASH_REMATCH[1]}"
51+
else
52+
echo "Failed to extract version from PR branch name: $HEAD_REF (expected release/vX.Y.Z)" >&2
53+
exit 1
54+
fi
55+
TAG="v${RAW}"
56+
PR_MERGE="${{ github.event.pull_request.merged }}"
4057
fi
4158
CLEANED="${RAW#v}"
4259
if [[ ! "$CLEANED" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
@@ -45,10 +62,45 @@ jobs:
4562
fi
4663
echo "tag=${TAG}" >> "$GITHUB_OUTPUT"
4764
echo "version=${CLEANED}" >> "$GITHUB_OUTPUT"
65+
echo "is_pr_merge=${PR_MERGE}" >> "$GITHUB_OUTPUT"
66+
67+
create-tag:
68+
name: Create Tag
69+
needs: version
70+
if: needs.version.outputs.is_pr_merge == 'true' || github.event_name == 'workflow_dispatch'
71+
runs-on: ubuntu-latest
72+
permissions:
73+
contents: write
74+
steps:
75+
- name: Checkout merge commit (PR merge)
76+
if: github.event_name == 'pull_request'
77+
uses: actions/checkout@v4
78+
with:
79+
ref: ${{ github.event.pull_request.merge_commit_sha }}
80+
81+
- name: Checkout main (manual dispatch)
82+
if: github.event_name == 'workflow_dispatch'
83+
uses: actions/checkout@v4
84+
85+
- name: Create and Push Tag
86+
shell: bash
87+
env:
88+
TAG: ${{ needs.version.outputs.tag }}
89+
run: |
90+
set -euo pipefail
91+
git fetch --tags
92+
if git rev-parse "$TAG" >/dev/null 2>&1; then
93+
echo "Tag $TAG already exists; skipping creation."
94+
exit 0
95+
fi
96+
git config user.name "github-actions[bot]"
97+
git config user.email "github-actions[bot]@users.noreply.github.com"
98+
git tag -a "$TAG" -m "Release $TAG"
99+
git push origin "$TAG"
48100
49101
build-and-push:
50102
name: Build and Push Docker Images
51-
needs: version
103+
needs: [version, create-tag]
52104
strategy:
53105
matrix:
54106
image: [etl-api, etl-replicator]
@@ -60,11 +112,12 @@ jobs:
60112
push: true
61113
tag_with_version: true
62114
version: ${{ needs.version.outputs.version }}
115+
checkout_ref: refs/tags/${{ needs.version.outputs.tag }}
63116
secrets: inherit
64117

65118
github-release:
66119
name: Create GitHub Release
67-
needs: [version, build-and-push]
120+
needs: [version, create-tag, build-and-push]
68121
runs-on: ubuntu-latest
69122
steps:
70123
- name: Generate Release Notes

0 commit comments

Comments
 (0)