ci(label): trigger workflows on labelling #44
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Label | |
on: | |
pull_request: | |
branches: [master, vara-stage-1, vara-stage-2, vara-stage-3] | |
types: [labeled] | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
dispatch: | |
runs-on: ubuntu-latest | |
if: >- | |
github.event.label.name == 'A0-pleasereview' | |
|| github.event.label.name == 'E2-forcemacos' | |
steps: | |
- uses: actions/github-script@v6 | |
env: | |
LABEL: ${{ github.event.label.name }} | |
REF: ${{ github.ref_name }} | |
HEAD_SHA: ${{ github.event.pull_request.head.sha }} | |
with: | |
script: | | |
const { LABEL, REF, HEAD_SHA } = process.env; | |
// const HEAD_SHA = "b31eecbfc1224b2e8c624a046f1d559738e9ead7"; | |
// const REF = "cl/issue-2888"; | |
const [owner, repo] = ["gear-tech", "gear"]; | |
const linux = (LABEL === "A0-pleasereview"); | |
// const linux = true; | |
// List the latest check runs | |
const { | |
data: { check_runs } | |
} = await github.rest.checks.listForRef({ | |
owner, | |
repo, | |
ref: REF, | |
}); | |
// Filter the traget skipped workflow run. | |
const runs = linux | |
?check_runs.filter((run) => run.name === "build") | |
:check_runs.filter((run) => run.name === "build / macox-x86") | |
console.log(runs); | |
if (runs.length === 0) return; | |
if (runs[0].conclusion !== "skipped") return; | |
/** | |
* The main logic starts from here. | |
**/ | |
// Dispatch target workflow. | |
const workflow_id = linux | |
?".github/workflows/build.yml" | |
:".github/workflows/build-macos.yml"; | |
await github.rest.actions.createWorkflowDispatch({ | |
owner, | |
repo, | |
workflow_id, | |
ref: REF, | |
}); | |
// Create check and update status | |
const check = async (name) => await github.rest.checks.create({ | |
owner, | |
repo, | |
name, | |
head_sha: HEAD_SHA | |
}); | |
const sleep = ms => new Promise(r => setTimeout(r, ms)); | |
const update = async (checks) => { | |
const { workflow_runs } = await github.rest.actions.listWorkflowRuns({ | |
owner, | |
repo, | |
workflow_id, | |
head_sha: HEAD_SHA, | |
}); | |
for (check of checks) { | |
const name = check.name; | |
let runs = workflow_runs.filter((run) => run.name === name); | |
if (runs.length !== 1) { | |
core.setFailed(`Could not find action ${name} from ${workflow_runs}`); | |
return; | |
} | |
const { | |
status, | |
conclusion, | |
id: external_id, | |
started_at, | |
url: details_url, | |
} = runs[0]; | |
await github.rest.checks.update({ | |
repo, | |
owner, | |
check_run_id: check.run_id, | |
external_id, | |
started_at, | |
details_url, | |
status, | |
conclusion, | |
}); | |
if (status === "completed") { | |
checks = checks.filter((c) => c != check); | |
} | |
} | |
if (checks.length === 0) { | |
return true; | |
} else { | |
await sleep(10000); | |
return await update(checks); | |
} | |
} | |
if (linux) { | |
const buildLinux = await check("build / linux"); | |
const buildWinCross = await check("build / win-cross"); | |
await update([buildLinux, buildWinCross]); | |
} else { | |
const buildMacosX86 = await check("build / macos-x86"); | |
await update([buildMacosX86]) | |
} |