Skip to content

Commit ae8109d

Browse files
authored
Feature: Auto create draft PR on pushing new release or hotfix branch (#23)
2 parents df88201 + 9175012 commit ae8109d

File tree

2 files changed

+94
-5
lines changed

2 files changed

+94
-5
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: draft_release_hotfix_pr
2+
3+
on:
4+
push:
5+
branches:
6+
- "release/**"
7+
- "hotfix/**"
8+
9+
env:
10+
BRANCH_MAIN: main
11+
BRANCH_DEVELOP: develop
12+
TAG_PREFIX: v
13+
14+
jobs:
15+
create_draft_pr:
16+
name: Create a draft PR for release/hotfix
17+
runs-on: ubuntu-latest
18+
permissions:
19+
contents: write
20+
# only create draft pull requests on pushing to branches 'release/' or 'hotfix/'
21+
if: (startsWith(github.head_ref, 'release/') || startsWith(github.head_ref, 'hotfix/'))
22+
23+
steps:
24+
- name: Set GitHub token
25+
run: |
26+
echo "GH_TOKEN=${{ secrets.GH_TOKEN }}" >> $GITHUB_ENV
27+
28+
- name: Checkout repository
29+
uses: actions/checkout@v3
30+
with:
31+
ref: ${{ github.head_ref }}
32+
token: ${{ env.GH_TOKEN }}
33+
# needed by "gh pr create"
34+
fetch-depth: 0
35+
36+
- name: Set environment variables for publishing release
37+
if: startsWith(github.head_ref, 'release/')
38+
run: |
39+
BRANCH_NAME="${{ github.head_ref }}"
40+
VERSION=${BRANCH_NAME#release/}
41+
echo "RELEASE_VERSION=$VERSION" >> $GITHUB_ENV
42+
echo "RELEASE_TYPE=release" >> $GITHUB_ENV
43+
44+
- name: Set environment variables for publishing hotfix
45+
if: startsWith(github.head_ref, 'hotfix/')
46+
run: |
47+
BRANCH_NAME="${{ github.head_ref }}"
48+
VERSION=${BRANCH_NAME#hotfix/}
49+
echo "RELEASE_VERSION=$VERSION" >> $GITHUB_ENV
50+
echo "RELEASE_TYPE=hotfix" >> $GITHUB_ENV
51+
52+
- name: Check whether pre-release-version
53+
if: contains(env.RELEASE_VERSION, 'alpha') || contains(env.RELEASE_VERSION, 'beta') || contains(env.RELEASE_VERSION, 'rc')
54+
run: |
55+
echo "PRE_RELEASE=true" >> $GITHUB_ENV
56+
57+
- name: Set PR title
58+
run: |
59+
RELEASE_TYPE=${{ env.RELEASE_TYPE }}
60+
PR_PREFIX=${RELEASE_TYPE^}
61+
PR_TITLE="${PR_PREFIX} ${{ env.TAG_PREFIX }}${{ env.RELEASE_VERSION }}"
62+
echo "PR_TITLE=$PR_TITLE" >> $GITHUB_ENV
63+
64+
- name: Create PR '${{ env.PR_TITLE }}'
65+
env:
66+
PR_LABELS: "automated-pr,${{ env.RELEASE_TYPE}}-pr"
67+
run: |
68+
LABELS=${{ env.PR_LABELS }}
69+
for LABEL in $(echo $LABELS | sed "s/,/ /g"); do gh label create $LABEL --force; done
70+
[[ ${PRE_RELEASE:false} == "true" ]] && gh label create 'pre-release' --force
71+
72+
PR_BODY=$(git log ${{ env.BRANCH_MAIN }}..${{ github.head_ref }} --pretty=format:%s)
73+
74+
gh pr create \
75+
--draft \
76+
--base ${{ env.BRANCH_MAIN }} \
77+
--head ${{ github.head_ref }} \
78+
--title "${{ env.PR_TITLE }}" \
79+
--body "$PR_BODY" \
80+
--label "${{ env.PR_LABELS }}" \
81+
--fill

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,25 @@ Sample Gitflow release workflow using GitHub Actions and official [GitHub CLI](h
77

88
## Features
99

10-
- Auto create and publish a new release on merging changes from `release/**`/`hotfix/**` into `main`.
10+
- Auto create a draft PR on pushing new branch `release/**` or `hotfix/**`.
11+
- Auto add following labels for PR:
12+
- `automated-pr`,
13+
- `release-pr`: if normal release.
14+
- `hotfix-pr`: if hotfix release.
15+
- `pre-release`: if version contains `alpha`, `beta`, `rc`.
16+
- **Note:** You are responsible for preparing release materials: changelog, bump version,... using your favorite tools.
17+
- Auto create and publish a new release on merging changes from `release/**` or `hotfix/**` into `main`.
1118
- Auto create corresponding git tag with the released version.
12-
- Auto create PR and merge changes from `release/**`/`hotfix/**` back into `develop`.
19+
- Auto create PR and merge changes from `release/**` or `hotfix/**` back into `develop`.
1320

1421
## Manual
1522

1623
- Workflows
1724

18-
| Name | Description | File |
19-
| ------- | ------------------ | ------------------------------------------------ |
20-
| Release | Automate releasing | [release.yaml](./.github/workflows/release.yaml) |
25+
| Name | Description | File |
26+
| ----------------------- | ------------------------------ | -------------------------------------------------------------------------------- |
27+
| Release | Automate releasing | [release.yaml](./.github/workflows/release.yaml) |
28+
| Draft Release/Hotfix PR | Automate drafting a release PR | [draft_release_hotfix_pr.yaml](./.github/workflows/draft_release_hotfix_pr.yaml) |
2129

2230
- You have to update below env variables if necessary
2331

0 commit comments

Comments
 (0)