|
7 | 7 | - completed
|
8 | 8 | env:
|
9 | 9 | NODE_VERSION_USED_FOR_DEVELOPMENT: 17
|
| 10 | + CI_WORKFLOW_ID: ${{github.event.workflow_run.id}} |
10 | 11 | jobs:
|
11 | 12 | publish-canary:
|
12 | 13 | runs-on: ubuntu-latest
|
13 | 14 | name: Publish Canary
|
14 | 15 | if: ${{ github.event.workflow_run.event == 'pull_request' }}
|
15 | 16 | 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 |
18 | 73 | 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 | + }) |
0 commit comments