Skip to content

Commit c79748c

Browse files
committed
ci: implement canary releases on PRs
1 parent 101cb45 commit c79748c

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

.github/workflows/canary.yaml

+73-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,83 @@ 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 saved_github_event.json
26+
run: gh run download "$CI_WORKFLOW_ID" -n saved_github_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 assert = require('assert');
36+
const { readFileSync, writeFileSync } = require('fs');
37+
38+
const prNumber = payload.workflow_run.number;
39+
const prSHA = context.sha;
40+
41+
const packageJSONPath = './npmDist/package.json';
42+
const packageJSON = JSON.parse(readFileSync(packageJSONPath, 'utf-8'));
43+
44+
assert(packageJSON.scripts == null, 'No scripts allowed for security reasons!');
45+
46+
let { version } = packageJSON;
47+
assert(!version.includes('+'), 'Can not append after metadata');
48+
version += packageJSON.version.includes('-') ? '.' : '-';
49+
version += `canary.pr.${prNumber}.${prSHA}`;
50+
51+
const tag = `canary-pr-${prNumber}`;
52+
53+
packageJSON.version = version;
54+
packageJSON.publishConfig.tag = `canary-pr-${prNumber}`;
55+
writeFileSync(packageJSONPath, JSON.stringify(packageJSON, null, 2), 'utf-8');
56+
57+
core.exportVariable('NPM_VERSION', version);
58+
core.exportVariable('NPM_TAG', tag);
59+
60+
- name: Publish NPM package
61+
run: npm publish ./npmDist
1862
env:
19-
GITHUB_CONTEXT: ${{ toJson(github) }}
63+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
64+
65+
- name: Add deprecate message on NPM package
66+
run: |
67+
npm deprecate "graphql@$NPM_VERSION" \
68+
"You are using canary version build from $PR_URL, no gurantees provided so please use your own discretion."
69+
env:
70+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
71+
PR_URL: ${{github.event.pull_request.url}}
72+
73+
- name: Add comment on PR
74+
uses: actions/github-script@v5
75+
with:
76+
github-token: ${{secrets.GITHUB_TOKEN}}
77+
script: |
78+
const npmTag = process.env.NPM_TAG;
79+
const npmVersion = process.env.NPM_VERSION;
80+
const npmURL = 'https://www.npmjs.com/package/graphql/v/' + npmVersion;
81+
github.rest.issues.createComment({
82+
issue_number: context.issue.number,
83+
owner: context.repo.owner,
84+
repo: context.repo.repo,
85+
body:
86+
`The latest changes of this PR are available as ['graphql@${npmVersion}'](${npmURL}) on NPM.\n` +
87+
'**Note: no gurantees provided so please use your own discretion.**\n\n' +
88+
`Also you can depend on latest version built from this PR: \`npm install --save graphql@${npmTag}\`.`,
89+
})

.github/workflows/ci.yml

+10
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 github_event.json
11+
uses: actions/upload-artifact@v2
12+
with:
13+
name: github_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)