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