1 name: Trigger Release and Publish
2
3 on:
4 push:
5 branches:
6 - develop
7 release:
8 types: [published]
9 workflow_dispatch:
10 inputs:
11 version:
12 type: choice
13 default: "preview"
14 description: What tag do you want to release?
15 required: true
16 options:
17 - preview
18 - next
19 - snapshot
20 - canary
21 - latest
22 branch:
23 type: string
24 default: "develop"
25 description: Which branch or commit SHA do you want to release from?
26 required: true
27 pr_number:
28 type: string
29 default: ""
30 description: Pull request to comment the published versions on (optional)
31 required: false
32 branches:
33 - develop
34 paths-ignore:
35 - "docs/**"
36 - "www/**"
37 - ".github/**"
38 issue_comment:
39 types:
40 - created
41
42 concurrency: ${{ github.workflow }}-${{ github.event_name == 'issue_comment' && github.event.comment.id || (github.event_name == 'push' || github.event.inputs.version == 'latest') && format('latest-{0}', github.ref) || github.ref }}
43
44 permissions:
45 pull-requests: write # to be able to comment on released pull requests
46 id-token: write # to enable use of OIDC for trusted publishing and npm provenance
47
48 jobs:
49 check:
50 name: Check if latest release is running
51 if: |
52 github.event_name == 'release' ||
53 (github.event_name == 'workflow_dispatch' && github.event.inputs.version != 'latest')
54 runs-on: ubuntu-latest
55 outputs:
56 release_running: ${{ steps.check_release.outputs.release_running }}
57 steps:
58 - name: Check for active latest release runs
59 id: check_release
60 uses: actions/github-script@v7
61 with:
62 github-token: ${{ secrets.GITHUB_TOKEN }}
63 script: |
64 const { data } = await github.rest.actions.listWorkflowRuns({
65 owner: context.repo.owner,
66 repo: context.repo.repo,
67 workflow_id: 'trigger-release.yml',
68 event: 'push',
69 per_page: 10,
70 })
71 const isRunning = data.workflow_runs.some(
72 run => ['in_progress', 'queued', 'waiting', 'requested', 'pending'].includes(run.status)
73 )
74 if (isRunning) {
75 core.notice('Latest release is currently running. Skipping preview release to avoid conflicts.')
76 }
77 core.setOutput('release_running', isRunning.toString())
78
79 release:
80 name: Trigger ${{ github.event.inputs.version || 'preview' }} Release
81 needs: check
82 if: needs.check.outputs.release_running != 'true'
83 runs-on: ubuntu-latest
84 steps:
85 - name: Checkout Repo
86 uses: actions/checkout@v3
87 with:
88 ref: ${{ github.event.inputs.branch || 'develop' }}
89
90 - name: Setup release env
91 uses: ./.github/actions/setup-release
92
93 - name: Version packages
94 run: yarn changeset version --snapshot ${{ github.event.inputs.version || 'preview' }}
95 env:
96 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
97
98 - name: Install Dependencies
99 run: yarn install --no-immutable
100
101 - name: Build all packages
102 run: yarn build
103
104 - name: Publish packages under next tag
105 run: |
106 set -o pipefail
107 yarn changeset publish --no-git-tags --snapshot --tag ${{ github.event.inputs.version || 'preview' }} | tee publish-output.txt
108
109 - name: Comment published versions on pull request
110 if: github.event.inputs.pr_number != ''
111 uses: actions/github-script@v7
112 env:
113 PR_NUMBER: ${{ github.event.inputs.pr_number }}
114 RELEASE_TAG: ${{ github.event.inputs.version }}
115 with:
116 script: |
117 const fs = require('fs')
118 const issueNumber = Number(process.env.PR_NUMBER)
119 if (!Number.isInteger(issueNumber) || issueNumber <= 0) {
120 core.setFailed(`Invalid pr_number: ${process.env.PR_NUMBER}`)
121 return
122 }
123
124 const output = fs.readFileSync('publish-output.txt', 'utf8')
125 const newTags = Array
126 .from(output.matchAll(/New tag:\s+(\S+)/g))
127 .map(([_, tag]) => tag)
128 if (!newTags.length) {
129 return
130 }
131
132 const version = newTags[0].slice(newTags[0].lastIndexOf('@') + 1)
133 const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`
134 const body =
135 `#### :rocket: A ${process.env.RELEASE_TAG} release has been made for this PR\n\n` +
136 `Version: \`${version}\`\n\n` +
137 '```sh\n' +
138 `yarn add @medusajs/medusa@${version}\n` +
139 '```\n\n' +
140 `<details>\n<summary>All ${newTags.length} published packages</summary>\n\n` +
141 '```sh\n' +
142 newTags.map((tag) => `yarn add ${tag}`).join('\n') +
143 '\n```\n\n</details>\n\n' +
144 `> Workflow run: ${runUrl}`
145
146 await github.rest.issues.createComment({
147 ...context.repo,
148 issue_number: issueNumber,
149 body,
150 })
151
152 - name: Post failure to Slack channel
153 if: failure()
154 id: slack
155 uses: slackapi/slack-github-action@v1.23.0
156 with:
157 channel-id: "releases"
158 payload: |
159 {
160 "blocks":[
161 {
162 "type": "header",
163 "text": {
164 "type": "plain_text",
165 "text": "Failed to release! ⛔"
166 }
167 },
168 {
169 "type": "section",
170 "text": {
171 "type": "plain_text",
172 "text": "Triggering a release for ${{ github.event.inputs.version || 'preview' }} environments failed for ${{ github.run_id }}"
173 }
174 }
175 ]
176 }
177 env:
178 SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_RELEASE }}
179 SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
180
181 latest:
182 name: Version packages PR
183 if: |
184 github.event_name == 'push' ||
185 (github.event_name == 'workflow_dispatch' && github.event.inputs.version == 'latest')
186 runs-on: ubuntu-latest
187 permissions:
188 contents: write
189 pull-requests: write
190 id-token: write
191 actions: write # required to createWorkflowDispatch the draft-release workflow
192 steps:
193 - name: Checkout Repo
194 uses: actions/checkout@v3
195 with:
196 ref: ${{ github.event.inputs.branch || github.sha }}
197
198 - name: Setup release env
199 uses: ./.github/actions/setup-release
200
201 - name: Build packages
202 run: yarn build
203
204 - name: Create Release Pull Request or Publish packages
205 id: changesets
206 uses: changesets/action@v1
207 with:
208 version: yarn version
209 commit: "Chore: Release"
210 title: "Chore: Release"
211 createGithubReleases: false
212 publish: yarn changeset publish
213 env:
214 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
215
216 - name: Trigger draft release generation
217 if: steps.changesets.outputs.published == 'true'
218 uses: actions/github-script@v7
219 with:
220 github-token: ${{ secrets.GITHUB_TOKEN }}
221 script: |
222 const packages = JSON.parse('${{ steps.changesets.outputs.publishedPackages }}')
223 const version = packages[0]?.version ?? ''
224 await github.rest.actions.createWorkflowDispatch({
225 owner: context.repo.owner,
226 repo: context.repo.repo,
227 workflow_id: 'draft-release.yml',
228 ref: 'develop',
229 inputs: {
230 version
231 }
232 })
233
234 snapshot:
235 name: Dispatch Snapshot Release
236 if: |
237 github.event.issue.pull_request &&
238 github.event.comment.body == '/snapshot-this'
239 runs-on: ubuntu-latest
240 permissions:
241 actions: write
242 pull-requests: write
243 steps:
244 - name: Validate pull request and dispatch snapshot release
245 uses: actions/github-script@v7
246 with:
247 script: |
248 await github.rest.reactions.createForIssueComment({
249 ...context.repo,
250 comment_id: context.payload.comment.id,
251 content: 'rocket',
252 })
253
254 const fail = async (message) => {
255 await github.rest.issues.createComment({
256 ...context.repo,
257 issue_number: context.issue.number,
258 body: message,
259 })
260 core.setFailed(message)
261 }
262
263 const actorPermission = (await github.rest.repos.getCollaboratorPermissionLevel({
264 ...context.repo,
265 username: context.actor,
266 })).data.permission
267 if (!['write', 'admin'].includes(actorPermission)) {
268 return fail('Only users with write permission to the respository can run /snapshot-this')
269 }
270
271 const { data: pullRequest } = await github.rest.pulls.get({
272 ...context.repo,
273 pull_number: context.issue.number,
274 })
275 if (context.payload.repository.full_name !== pullRequest.head.repo.full_name) {
276 return fail('`/snapshot-this` is not supported on pull requests from forked repositories.')
277 }
278
279 await github.rest.actions.createWorkflowDispatch({
280 ...context.repo,
281 workflow_id: 'trigger-release.yml',
282 ref: context.payload.repository.default_branch,
283 inputs: {
284 version: 'snapshot',
285 branch: pullRequest.head.ref,
286 pr_number: String(context.issue.number),
287 },
288 })
289
290 const runsUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/workflows/trigger-release.yml?query=event%3Aworkflow_dispatch`
291 await github.rest.issues.createComment({
292 ...context.repo,
293 issue_number: context.issue.number,
294 body: `#### :rocket: A snapshot release was triggered for \`${pullRequest.head.ref}\`\n\nFollow its progress [here](${runsUrl}). The published versions will be commented here once it finishes.`,
295 })