Skip to content

Commit c1e1e6e

Browse files
committed
Github Actions - Update release workflow to create PR
1 parent 53ea5ef commit c1e1e6e

File tree

1 file changed

+120
-25
lines changed

1 file changed

+120
-25
lines changed

.github/workflows/release.yml

Lines changed: 120 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,25 @@ on:
1010

1111
permissions:
1212
contents: write
13+
pull-requests: write
1314

1415
jobs:
1516
release:
1617
name: Create Release
1718
runs-on: ubuntu-latest
18-
19+
1920
steps:
2021
- name: Checkout code
2122
uses: actions/checkout@v4
2223
with:
2324
fetch-depth: 0
2425
token: ${{ secrets.GITHUB_TOKEN }}
25-
26+
2627
- name: Setup git
2728
run: |
2829
git config user.name "github-actions[bot]"
2930
git config user.email "github-actions[bot]@users.noreply.github.com"
30-
31+
3132
- name: Get version
3233
id: get_version
3334
run: |
@@ -36,66 +37,160 @@ jobs:
3637
VERSION_NUMBER="${VERSION#v}"
3738
echo "version=v${VERSION_NUMBER}" >> $GITHUB_OUTPUT
3839
echo "version_number=${VERSION_NUMBER}" >> $GITHUB_OUTPUT
39-
40+
echo "branch_name=release/v${VERSION_NUMBER}" >> $GITHUB_OUTPUT
41+
42+
- name: Create release branch
43+
run: |
44+
git checkout -b ${{ steps.get_version.outputs.branch_name }}
45+
4046
- name: Update plugin.rb version
4147
run: |
4248
sed -i.bak "s/^# version: .*$/# version: ${{ steps.get_version.outputs.version_number }}/" plugin.rb
4349
rm plugin.rb.bak
44-
50+
4551
- name: Update package.json version
4652
run: |
4753
sed -i.bak "s/\"version\": \".*\"/\"version\": \"${{ steps.get_version.outputs.version_number }}\"/" package.json
4854
rm package.json.bak
49-
55+
5056
- name: Commit version updates
5157
run: |
5258
git add plugin.rb package.json
5359
git commit -m "Bump version to ${{ steps.get_version.outputs.version_number }}"
54-
git push origin ${{ github.ref_name }}
55-
60+
git push origin ${{ steps.get_version.outputs.branch_name }}
61+
62+
- name: Generate changelog
63+
id: changelog
64+
run: |
65+
# Get the previous tag
66+
PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "")
67+
68+
if [ -z "$PREV_TAG" ]; then
69+
echo "No previous tag found, generating changelog from initial commit"
70+
CHANGELOG=$(git log HEAD --pretty=format:"- %s (%h)" --reverse)
71+
else
72+
echo "Generating changelog from ${PREV_TAG} to HEAD"
73+
CHANGELOG=$(git log ${PREV_TAG}..HEAD --pretty=format:"- %s (%h)")
74+
fi
75+
76+
# Write changelog to file for multiline handling
77+
echo "$CHANGELOG" > /tmp/changelog.txt
78+
79+
# Set output using multiline format
80+
{
81+
echo "notes<<EOF"
82+
cat /tmp/changelog.txt
83+
echo "EOF"
84+
} >> $GITHUB_OUTPUT
85+
86+
- name: Create Pull Request
87+
id: create_pr
88+
uses: actions/github-script@v7
89+
with:
90+
script: |
91+
const { data: pr } = await github.rest.pulls.create({
92+
owner: context.repo.owner,
93+
repo: context.repo.repo,
94+
title: `Release ${{ steps.get_version.outputs.version }}`,
95+
head: `${{ steps.get_version.outputs.branch_name }}`,
96+
base: 'master',
97+
body: `## Changes in ${{ steps.get_version.outputs.version }}
98+
99+
${{ steps.changelog.outputs.notes }}
100+
101+
## Installation
102+
103+
Follow the [plugin installation guide](https://meta.discourse.org/t/install-a-plugin/19157) using this repository.
104+
105+
---
106+
107+
This PR was automatically created by the release workflow. After merging, the release and tag will be created automatically.`
108+
});
109+
core.setOutput('pr_number', pr.number);
110+
core.setOutput('pr_url', pr.html_url);
111+
return pr.number;
112+
113+
- name: Summary
114+
run: |
115+
echo "## Release PR Created! :rocket:" >> $GITHUB_STEP_SUMMARY
116+
echo "" >> $GITHUB_STEP_SUMMARY
117+
echo "Version: ${{ steps.get_version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
118+
echo "PR: ${{ steps.create_pr.outputs.pr_url }}" >> $GITHUB_STEP_SUMMARY
119+
echo "" >> $GITHUB_STEP_SUMMARY
120+
echo "After the PR is merged, run the 'Create Release Tag' workflow to create the GitHub release." >> $GITHUB_STEP_SUMMARY
121+
122+
create-release:
123+
name: Create Release After Merge
124+
runs-on: ubuntu-latest
125+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/heads/master')
126+
127+
steps:
128+
- name: Checkout code
129+
uses: actions/checkout@v4
130+
with:
131+
fetch-depth: 0
132+
133+
- name: Check if this is a version bump commit
134+
id: check_version_bump
135+
run: |
136+
COMMIT_MSG=$(git log -1 --pretty=%B)
137+
if [[ "$COMMIT_MSG" =~ ^Bump\ version\ to\ ([0-9]+\.[0-9]+\.[0-9]+) ]]; then
138+
VERSION="${BASH_REMATCH[1]}"
139+
echo "is_version_bump=true" >> $GITHUB_OUTPUT
140+
echo "version=v${VERSION}" >> $GITHUB_OUTPUT
141+
echo "version_number=${VERSION}" >> $GITHUB_OUTPUT
142+
else
143+
echo "is_version_bump=false" >> $GITHUB_OUTPUT
144+
fi
145+
56146
- name: Create and push tag
147+
if: steps.check_version_bump.outputs.is_version_bump == 'true'
57148
run: |
58-
git tag ${{ steps.get_version.outputs.version }}
59-
git push origin ${{ steps.get_version.outputs.version }}
60-
149+
git config user.name "github-actions[bot]"
150+
git config user.email "github-actions[bot]@users.noreply.github.com"
151+
git tag ${{ steps.check_version_bump.outputs.version }}
152+
git push origin ${{ steps.check_version_bump.outputs.version }}
153+
61154
- name: Generate changelog
155+
if: steps.check_version_bump.outputs.is_version_bump == 'true'
62156
id: changelog
63157
run: |
64158
# Get the previous tag
65-
PREV_TAG=$(git describe --tags --abbrev=0 ${{ steps.get_version.outputs.version }}^ 2>/dev/null || echo "")
66-
159+
PREV_TAG=$(git describe --tags --abbrev=0 ${{ steps.check_version_bump.outputs.version }}^ 2>/dev/null || echo "")
160+
67161
if [ -z "$PREV_TAG" ]; then
68162
echo "No previous tag found, generating changelog from initial commit"
69-
CHANGELOG=$(git log ${{ steps.get_version.outputs.version }} --pretty=format:"- %s (%h)" --reverse)
163+
CHANGELOG=$(git log ${{ steps.check_version_bump.outputs.version }} --pretty=format:"- %s (%h)" --reverse)
70164
else
71-
echo "Generating changelog from ${PREV_TAG} to ${{ steps.get_version.outputs.version }}"
72-
CHANGELOG=$(git log ${PREV_TAG}..${{ steps.get_version.outputs.version }} --pretty=format:"- %s (%h)")
165+
echo "Generating changelog from ${PREV_TAG} to ${{ steps.check_version_bump.outputs.version }}"
166+
CHANGELOG=$(git log ${PREV_TAG}..${{ steps.check_version_bump.outputs.version }} --pretty=format:"- %s (%h)")
73167
fi
74-
168+
75169
# Write changelog to file for multiline handling
76170
echo "$CHANGELOG" > /tmp/changelog.txt
77-
171+
78172
# Set output using multiline format
79173
{
80174
echo "notes<<EOF"
81175
cat /tmp/changelog.txt
82176
echo "EOF"
83177
} >> $GITHUB_OUTPUT
84-
178+
85179
- name: Create Release
180+
if: steps.check_version_bump.outputs.is_version_bump == 'true'
86181
uses: actions/create-release@v1
87182
env:
88183
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
89184
with:
90-
tag_name: ${{ steps.get_version.outputs.version }}
91-
release_name: Release ${{ steps.get_version.outputs.version }}
185+
tag_name: ${{ steps.check_version_bump.outputs.version }}
186+
release_name: Release ${{ steps.check_version_bump.outputs.version }}
92187
body: |
93-
## Changes in ${{ steps.get_version.outputs.version }}
94-
188+
## Changes in ${{ steps.check_version_bump.outputs.version }}
189+
95190
${{ steps.changelog.outputs.notes }}
96-
191+
97192
## Installation
98-
193+
99194
Follow the [plugin installation guide](https://meta.discourse.org/t/install-a-plugin/19157) using this repository.
100195
draft: false
101196
prerelease: false

0 commit comments

Comments
 (0)