|
| 1 | +// @ts-check |
| 2 | +// TODO(NODE-7570): replace with workflow_call once GitHub/npm resolve the OIDC |
| 3 | +// workflow_ref mismatch (https://github.com/npm/documentation/issues/1755). |
| 4 | +// |
| 5 | +// Dispatch a workflow_dispatch-triggered GitHub Actions workflow and wait for |
| 6 | +// the resulting run to complete (propagating its exit status). |
| 7 | +// |
| 8 | +// `gh workflow run` prints the created workflow run URL on stdout when the |
| 9 | +// server returns it (current github.com API version does); we parse the run |
| 10 | +// id out of the URL and pass it to `gh run watch`. |
| 11 | +// |
| 12 | +// Usage: |
| 13 | +// node dispatch-and-wait.mjs <workflow.yml> [key=value ...] |
| 14 | +// |
| 15 | +// For npm-publish.yml specifically: |
| 16 | +// node dispatch-and-wait.mjs npm-publish.yml tag=<tag> version=<v> ref=<sha> |
| 17 | +// |
| 18 | +// Arguments: |
| 19 | +// <workflow.yml> Filename of the target workflow under .github/workflows/ |
| 20 | +// in the same repo. Must declare `on: workflow_dispatch:`. |
| 21 | +// key=value ... Inputs forwarded to the dispatched workflow. Valid keys |
| 22 | +// and which of them are required are determined by the |
| 23 | +// target workflow's `on.workflow_dispatch.inputs`; the |
| 24 | +// dispatch fails if any required input is missing or any |
| 25 | +// unknown input is passed. For `npm-publish.yml`, all of |
| 26 | +// `tag`, `version`, and `ref` are required. |
| 27 | +// Example: tag=nightly version=1.2.3 ref=abc1234 |
| 28 | +// |
| 29 | +// Environment: |
| 30 | +// GH_TOKEN (required) used by the gh CLI; in a workflow set |
| 31 | +// this to ${{ github.token }}. |
| 32 | +// DISPATCH_WORKFLOW_REF (optional, default `main`) git ref the target |
| 33 | +// workflow file is loaded from. Hardcoded to main by |
| 34 | +// default so callers on backport branches don't have |
| 35 | +// to keep a copy of the target workflow on their |
| 36 | +// branch. |
| 37 | +import * as child_process from 'node:child_process'; |
| 38 | +import * as process from 'node:process'; |
| 39 | + |
| 40 | +const [, , workflow, ...inputArgs] = process.argv; |
| 41 | +if (!workflow) { |
| 42 | + console.error('usage: dispatch-and-wait.mjs <workflow.yml> [key=value ...]'); |
| 43 | + process.exit(2); |
| 44 | +} |
| 45 | + |
| 46 | +const dispatchRef = process.env.DISPATCH_WORKFLOW_REF || 'main'; |
| 47 | + |
| 48 | +const ghArgs = [ |
| 49 | + 'workflow', 'run', workflow, |
| 50 | + '--ref', dispatchRef, |
| 51 | + ...inputArgs.flatMap(kv => ['-f', kv]) |
| 52 | +]; |
| 53 | +console.log(`Dispatching ${workflow} from ref ${dispatchRef}`); |
| 54 | + |
| 55 | +const dispatch = child_process.spawnSync('gh', ghArgs, { |
| 56 | + encoding: 'utf8', |
| 57 | + stdio: ['inherit', 'pipe', 'inherit'] |
| 58 | +}); |
| 59 | +if (dispatch.status !== 0) process.exit(dispatch.status ?? 1); |
| 60 | + |
| 61 | +// gh prints e.g. "https://github.com/owner/repo/actions/runs/<id>" |
| 62 | +const match = dispatch.stdout.match(/\/actions\/runs\/(\d+)/); |
| 63 | +if (!match) { |
| 64 | + console.error('Could not extract run id from gh workflow run output:', dispatch.stdout); |
| 65 | + process.exit(1); |
| 66 | +} |
| 67 | +const runId = match[1]; |
| 68 | +console.log(`Dispatched run ${runId}`); |
| 69 | + |
| 70 | +const watch = child_process.spawnSync('gh', ['run', 'watch', runId, '--exit-status'], { |
| 71 | + stdio: 'inherit' |
| 72 | +}); |
| 73 | +process.exit(watch.status ?? 1); |
0 commit comments