Skip to content

Commit cd7f957

Browse files
ci: implement canary releases on PRs (#3470)
1 parent 101cb45 commit cd7f957

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

.github/workflows/canary.yaml

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,93 @@ on:
77
- completed
88
env:
99
NODE_VERSION_USED_FOR_DEVELOPMENT: 17
10+
CI_WORKFLOW_ID: ${{github.event.workflow_run.id}}
1011
jobs:
1112
publish-canary:
1213
runs-on: ubuntu-latest
1314
name: Publish Canary
1415
if: ${{ github.event.workflow_run.event == 'pull_request' }}
1516
steps:
16-
- name: Dump GitHub context
17-
run: echo "$GITHUB_CONTEXT"
17+
- name: Setup Node.js
18+
uses: actions/setup-node@v2
19+
with:
20+
cache: npm
21+
node-version: ${{ env.NODE_VERSION_USED_FOR_DEVELOPMENT }}
22+
# 'registry-url' is required for 'npm publish'
23+
registry-url: 'https://registry.npmjs.org'
24+
25+
- name: Download event.json
26+
run: gh run download "$CI_WORKFLOW_ID" -n event.json
27+
28+
- name: Download NPM package artifact
29+
run: gh run download "$CI_WORKFLOW_ID" -n npmDist -D npmDist
30+
31+
- name: Modify NPM package to be canary release
32+
uses: actions/github-script@v5
33+
with:
34+
script: |
35+
const fs = require('fs');
36+
const assert = require('assert');
37+
38+
const eventJSON = JSON.parse(
39+
fs.readFileSync('./event.json', 'utf-8')
40+
);
41+
42+
core.exportVariable('PR_URL', eventJSON.pull_request.url);
43+
44+
const prSHA = event.sha;
45+
const prNumber = event.pull_request.number;
46+
const packageJSONPath = './npmDist/package.json';
47+
const packageJSON = JSON.parse(
48+
fs.readFileSync(packageJSONPath, 'utf-8')
49+
);
50+
51+
const tag = `canary-pr-${prNumber}`;
52+
// Override entire 'publishConfig' since it can contain untrusted data.
53+
packageJSON.publishConfig = { tag };
54+
core.exportVariable('NPM_TAG', `canary-pr-${prNumber}`);
55+
56+
let { version } = packageJSON;
57+
assert(!version.includes('+'), 'Can not append after metadata');
58+
version += packageJSON.version.includes('-') ? '.' : '-';
59+
version += `canary.pr.${prNumber}.${prSHA}`;
60+
61+
packageJSON.version = version;
62+
core.exportVariable('NPM_VERSION', version);
63+
64+
assert(packageJSON.scripts == null, 'No scripts allowed for security reasons!');
65+
fs.writeFileSync(
66+
packageJSONPath,
67+
JSON.stringify(packageJSON, null, 2),
68+
'utf-8',
69+
);
70+
71+
- name: Publish NPM package
72+
run: npm publish ./npmDist
1873
env:
19-
GITHUB_CONTEXT: ${{ toJson(github) }}
74+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
75+
76+
- name: Add deprecate message on NPM package
77+
run: |
78+
npm deprecate "graphql@$NPM_VERSION" \
79+
"You are using canary version build from $PR_URL, no gurantees provided so please use your own discretion."
80+
env:
81+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
82+
83+
- name: Add comment on PR
84+
uses: actions/github-script@v5
85+
with:
86+
github-token: ${{secrets.GITHUB_TOKEN}}
87+
script: |
88+
const npmTag = process.env.NPM_TAG;
89+
const npmVersion = process.env.NPM_VERSION;
90+
const npmURL = 'https://www.npmjs.com/package/graphql/v/' + npmVersion;
91+
github.rest.issues.createComment({
92+
issue_number: context.issue.number,
93+
owner: context.repo.owner,
94+
repo: context.repo.repo,
95+
body:
96+
`The latest changes of this PR are available as ['graphql@${npmVersion}'](${npmURL}) on NPM.\n` +
97+
'**Note: no gurantees provided so please use your own discretion.**\n\n' +
98+
`Also you can depend on latest version built from this PR: \`npm install --save graphql@${npmTag}\`.`,
99+
})

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ on: [push, pull_request]
33
env:
44
NODE_VERSION_USED_FOR_DEVELOPMENT: 17
55
jobs:
6+
save-github-event:
7+
name: "Save `github.event` as an artifact to use in subsequent 'workflow_run' actions"
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Upload event.json
11+
uses: actions/upload-artifact@v2
12+
with:
13+
name: event.json
14+
path: ${{ github.event_path }}
15+
616
lint:
717
name: Lint source files
818
runs-on: ubuntu-latest

0 commit comments

Comments
 (0)