Skip to content

Commit f6369c1

Browse files
authored
Add release-pr workflow (#490)
* Add `release-pr` workflow * Add comments * fix quotes
1 parent e82f42c commit f6369c1

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

.github/workflows/release-pr.yml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
name: Release PR
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
jobs:
8+
release_check:
9+
if: github.repository == 'changesets/action' && github.event.issue.pull_request && startsWith(github.event.comment.body, '/release-pr')
10+
runs-on: ubuntu-latest
11+
steps:
12+
- id: report_in_progress
13+
run: |
14+
echo "in_progress_reaction_id=$(gh api /repos/${{github.repository}}/issues/comments/${{github.event.comment.id}}/reactions -f content='eyes' --jq '.id')" >> "$GITHUB_OUTPUT"
15+
env:
16+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17+
18+
- id: check_authorization
19+
run: |
20+
if [[ $AUTHOR_ASSOCIATION == 'MEMBER' || $AUTHOR_ASSOCIATION == 'OWNER' || $AUTHOR_ASSOCIATION == 'COLLABORATOR' ]]
21+
then
22+
echo "User is authorized to release"
23+
else
24+
echo "User is not authorized to release"
25+
exit 1
26+
fi
27+
env:
28+
AUTHOR_ASSOCIATION: ${{ github.event.comment.author_association }}
29+
30+
outputs:
31+
in_progress_reaction_id: ${{ steps.report_in_progress.outputs.in_progress_reaction_id }}
32+
33+
release:
34+
if: github.repository == 'changesets/action'
35+
timeout-minutes: 20
36+
runs-on: ubuntu-latest
37+
needs: release_check
38+
steps:
39+
- uses: actions/checkout@v4
40+
41+
- name: Checkout pull request
42+
run: gh pr checkout ${{ github.event.issue.number }}
43+
env:
44+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45+
46+
- name: Check if Version Packages PR
47+
id: check_version_packages
48+
run: |
49+
echo "version_packages=$(gh pr view ${{ github.event.issue.number }} --json headRefName --jq '.headRefName|startswith("changeset-release/")')" >> "$GITHUB_OUTPUT"
50+
env:
51+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52+
53+
- name: Reset Version Packages PR
54+
if: steps.check_version_packages.outputs.version_packages == 'true'
55+
run: git reset --hard HEAD~1
56+
57+
- uses: actions/setup-node@v4
58+
with:
59+
node-version: "18"
60+
cache: "yarn"
61+
62+
- name: Install Dependencies
63+
run: yarn --frozen-lockfile
64+
65+
- run: yarn changeset version --snapshot pr${{ github.event.issue.number }}
66+
env:
67+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
68+
69+
- name: Build
70+
run: yarn build
71+
72+
- name: Release snapshot version
73+
run: yarn run release:pr
74+
75+
- run: gh api /repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions -f content='rocket'
76+
env:
77+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
78+
79+
- run: gh api -X DELETE /repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions/${{ needs.release_check.outputs.in_progress_reaction_id }}
80+
env:
81+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
82+
83+
- run: gh pr comment ${{ github.event.issue.number }} --body "The [${{ github.repository }}@$(git rev-parse HEAD)](https://github.com/${{ github.repository }}/commit/$(git rev-parse HEAD)) release triggered by [this comment](${{ github.event.comment.url }}) has [succeeded](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})."
84+
env:
85+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
86+
87+
report-failure-if-needed:
88+
needs: [release_check, release]
89+
timeout-minutes: 2
90+
runs-on: ubuntu-latest
91+
if: failure() && github.repository == 'changesets/action' && (needs.release_check.result == 'failure' || needs.release.result == 'failure')
92+
steps:
93+
- run: gh api /repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions -f content='-1'
94+
env:
95+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
96+
97+
- run: gh api -X DELETE /repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions/${{ needs.release_check.outputs.in_progress_reaction_id }}
98+
env:
99+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
100+
101+
- run: gh pr comment ${{ github.event.issue.number }} --body "The release triggered by [this comment](${{ github.event.comment.url }}) has [failed](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})."
102+
env:
103+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"typecheck": "tsc",
2525
"changeset": "changeset",
2626
"bump": "node ./scripts/bump.js",
27-
"release": "node ./scripts/release.js"
27+
"release": "node ./scripts/release.js",
28+
"release:pr": "node ./scripts/release-pr.js"
2829
},
2930
"engines": {
3031
"node": ">= 20"

scripts/release-pr.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const path = require("path");
2+
const { exec, getExecOutput } = require("@actions/exec");
3+
4+
const { version } = require("../package.json");
5+
const tag = `v${version}`;
6+
const releaseLine = "pr-release";
7+
8+
process.chdir(path.join(__dirname, ".."));
9+
10+
(async () => {
11+
await exec("git", ["checkout", "--detach"]);
12+
await exec("git", ["add", "--force", "dist"]);
13+
await exec("git", ["commit", "-m", tag]);
14+
15+
await exec("git", [
16+
"push",
17+
"--force",
18+
"origin",
19+
`HEAD:refs/heads/${releaseLine}`,
20+
]);
21+
})();

0 commit comments

Comments
 (0)