|
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 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 |
18 | 62 | 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 | + }) |
0 commit comments