|
| 1 | +name: Canary Release |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +env: |
| 9 | + NODE_VERSION_USED_FOR_DEVELOPMENT: 17 |
| 10 | +jobs: |
| 11 | + publish-canary: |
| 12 | + name: Publish Canary |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Checkout Main |
| 16 | + uses: actions/checkout@v2 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + persist-credentials: false |
| 20 | + |
| 21 | + - name: Setup Node.js |
| 22 | + uses: actions/setup-node@v2 |
| 23 | + with: |
| 24 | + cache: npm |
| 25 | + node-version: ${{ env.NODE_VERSION_USED_FOR_DEVELOPMENT }} |
| 26 | + # 'registry-url' is required for 'npm publish' |
| 27 | + registry-url: 'https://registry.npmjs.org' |
| 28 | + |
| 29 | + - name: Install Dependencies |
| 30 | + run: npm ci --ignore-scripts |
| 31 | + |
| 32 | + - name: Build NPM package |
| 33 | + run: npm run build:npm |
| 34 | + |
| 35 | + - name: Modify NPM package to be canary release |
| 36 | + uses: actions/github-script@v5 |
| 37 | + with: |
| 38 | + script: | |
| 39 | + const assert = require('assert'); |
| 40 | + const { readFileSync, writeFileSync } = require('fs'); |
| 41 | +
|
| 42 | + const prNumber = context.issue.number; |
| 43 | + const prSHA = context.sha; |
| 44 | +
|
| 45 | + const packageJSONPath = './npmDist/package.json'; |
| 46 | + const packageJSON = JSON.parse(readFileSync(packageJSONPath, 'utf-8')); |
| 47 | +
|
| 48 | + assert(packageJSON.scripts != null, 'No scripts allowed for security reasons!'); |
| 49 | +
|
| 50 | + let { version } = packageJSON; |
| 51 | + assert(!version.includes('+'), 'Can not append after metadata'); |
| 52 | + version += packageJSON.version.includes('-') ? '.' : '-'; |
| 53 | + version += `canary.pr.${prNumber}.${prSHA}`; |
| 54 | +
|
| 55 | + const tag = `canary-pr-${prNumber}`; |
| 56 | +
|
| 57 | + packageJSON.version = version; |
| 58 | + packageJSON.publishConfig.tag = `canary-pr-${prNumber}`; |
| 59 | + writeFileSync(packageJSONPath, JSON.stringify(packageJSON, null, 2), 'utf-8'); |
| 60 | +
|
| 61 | + core.exportVariable('NPM_VERSION', version); |
| 62 | + core.exportVariable('NPM_TAG', tag); |
| 63 | +
|
| 64 | + - name: Publish NPM package |
| 65 | + run: npm publish ./npmDist |
| 66 | + env: |
| 67 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 68 | + |
| 69 | + - name: Add deprecate message on NPM package |
| 70 | + run: | |
| 71 | + npm deprecate "graphql@$NPM_VERSION" \ |
| 72 | + "You are using canary version build from $PR_URL, no gurantees provided so please use your own discretion." |
| 73 | + env: |
| 74 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 75 | + PR_URL: ${{github.event.pull_request.url}} |
| 76 | + |
| 77 | + - name: Add comment on PR |
| 78 | + uses: actions/github-script@v5 |
| 79 | + with: |
| 80 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 81 | + script: | |
| 82 | + const npmTag = process.env.NPM_TAG; |
| 83 | + const npmVersion = process.env.NPM_VERSION; |
| 84 | + const npmURL = 'https://www.npmjs.com/package/graphql/v/' + npmVersion; |
| 85 | + github.rest.issues.createComment({ |
| 86 | + issue_number: context.issue.number, |
| 87 | + owner: context.repo.owner, |
| 88 | + repo: context.repo.repo, |
| 89 | + body: |
| 90 | + `The latest changes of this PR are available as ['graphql@${npmVersion}'](${npmURL}) on NPM.\n` + |
| 91 | + '**Note: no gurantees provided so please use your own discretion.**\n\n' + |
| 92 | + `Also you can depend on latest version built from this PR: \`npm install --save graphql@${npmTag}\`.`, |
| 93 | + }) |
0 commit comments