Skip to content

Commit 199ba22

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

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

.github/workflows/canary.yaml

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

0 commit comments

Comments
 (0)