Skip to content

ci: add canary releases on PRs #3469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions .github/workflows/canary.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Canary Release
on:
workflow_run:
workflows:
- CI
types:
- completed
env:
NODE_VERSION_USED_FOR_DEVELOPMENT: 17
jobs:
publish-canary:
runs-on: ubuntu-latest
name: Publish Canary
if: ${{ github.event.workflow_run.event == 'pull_request' }}
steps:
- name: Dump GitHub context
run: echo "$GITHUB_CONTEXT"
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
- name: Setup Node.js
uses: actions/setup-node@v2
with:
cache: npm
node-version: ${{ env.NODE_VERSION_USED_FOR_DEVELOPMENT }}
# 'registry-url' is required for 'npm publish'
registry-url: 'https://registry.npmjs.org'

- name: Download NPM package artifact
run: gh run download "$CI_WORKFLOW_ID" -n npmDist -D npmDist
env:
CI_WORKFLOW_ID: ${{github.event.workflow_run.id}}

- name: Modify NPM package to be canary release
uses: actions/github-script@v5
with:
script: |
const assert = require('assert');
const { readFileSync, writeFileSync } = require('fs');

const prNumber = payload.workflow_run.number;
const prSHA = context.sha;

const packageJSONPath = './npmDist/package.json';
const packageJSON = JSON.parse(readFileSync(packageJSONPath, 'utf-8'));

assert(packageJSON.scripts == null, 'No scripts allowed for security reasons!');

let { version } = packageJSON;
assert(!version.includes('+'), 'Can not append after metadata');
version += packageJSON.version.includes('-') ? '.' : '-';
version += `canary.pr.${prNumber}.${prSHA}`;

const tag = `canary-pr-${prNumber}`;

packageJSON.version = version;
packageJSON.publishConfig.tag = `canary-pr-${prNumber}`;
writeFileSync(packageJSONPath, JSON.stringify(packageJSON, null, 2), 'utf-8');

core.exportVariable('NPM_VERSION', version);
core.exportVariable('NPM_TAG', tag);

- name: Publish NPM package
run: npm publish ./npmDist
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Add deprecate message on NPM package
run: |
npm deprecate "graphql@$NPM_VERSION" \
"You are using canary version build from $PR_URL, no gurantees provided so please use your own discretion."
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
PR_URL: ${{github.event.pull_request.url}}

- name: Add comment on PR
uses: actions/github-script@v5
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const npmTag = process.env.NPM_TAG;
const npmVersion = process.env.NPM_VERSION;
const npmURL = 'https://www.npmjs.com/package/graphql/v/' + npmVersion;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body:
`The latest changes of this PR are available as ['graphql@${npmVersion}'](${npmURL}) on NPM.\n` +
'**Note: no gurantees provided so please use your own discretion.**\n\n' +
`Also you can depend on latest version built from this PR: \`npm install --save graphql@${npmTag}\`.`,
})
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,34 @@ jobs:
path: ./npm-dist-diff.html
if-no-files-found: ignore

build-npm-package:
name: Build artifact with NPM package
runs-on: ubuntu-latest
needs: [test, fuzz, lint, integrationTests]
steps:
- name: Checkout repo
uses: actions/checkout@v2
with:
persist-credentials: false

- name: Setup Node.js
uses: actions/setup-node@v2
with:
cache: npm
node-version: ${{ env.NODE_VERSION_USED_FOR_DEVELOPMENT }}

- name: Install Dependencies
run: npm ci --ignore-scripts

- name: Build NPM package
run: npm run build:npm

- name: Upload NPM package
uses: actions/upload-artifact@v2
with:
name: npmDist
path: ./npmDist

deploy-to-npm-branch:
name: Deploy to `npm` branch
runs-on: ubuntu-latest
Expand Down