From 716700d2a74b1d06d5f2a58c4057a6e8ea1e7b47 Mon Sep 17 00:00:00 2001 From: clearloop Date: Wed, 29 May 2024 00:52:38 +0800 Subject: [PATCH] ci(label): use fork-action instead of action script (#3764) --- .github/actions/label/build.js | 206 ------------------------- .github/actions/message/main.js | 2 +- .github/workflows/build-macos.yml | 4 + .github/workflows/build-win-native.yml | 4 + .github/workflows/build.yml | 6 + .github/workflows/label.yml | 51 ++++-- 6 files changed, 53 insertions(+), 220 deletions(-) delete mode 100644 .github/actions/label/build.js diff --git a/.github/actions/label/build.js b/.github/actions/label/build.js deleted file mode 100644 index 0880cd6d0f0..00000000000 --- a/.github/actions/label/build.js +++ /dev/null @@ -1,206 +0,0 @@ -/** - * Javascript module for the label action. - */ - -const [owner, repo] = ["gear-tech", "gear"]; -const { LABEL, REF, HEAD_SHA, TITLE, NUMBER, REPO } = process.env; -const linux = - LABEL === "A0-pleasereview" || - LABEL === "A4-insubstantial" || - LABEL === "A2-mergeoncegreen"; -const checks = linux ? ["linux (debug)", "win-cross (debug)"] : ["macos-x86 (debug)", "macos-aarch64 (debug)"]; -const workflow_id = linux - ? ".github/workflows/build.yml" - : ".github/workflows/build-macos.yml"; - -/** - * Sleep for ms milliseconds. - **/ -const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); - -/** - * If skipping this action. - **/ -const checkSkip = async ({ github, core }) => { - core.info(`Checking if need to skip dispatch from ${REPO}:${REF}`); - if (REPO === "gear-tech/gear" && REF.startsWith("dependabot")) { - core.info("This pull request is from dependabot, skipping dispatching."); - process.exit(0); - } - - const { - data: { check_runs }, - } = await github.rest.checks.listForRef({ - owner, - repo, - ref: REF, - }); - - if ( - check_runs.filter( - (run) => - (run.name === "build" && run.conclusion !== "skipped") || - run.name === "build / linux (debug)" || - run.name === "build / macos / x86" - ).length > 0 - ) { - core.info( - "Build has already been processed, check runs: ", - JSON.stringify(check_runs, null, 2) - ); - - process.exit(0); - } -}; - -/** - * Create build checks. - * - * TODO: - * * Queue the new created checks to check suite PR (#3087). - * * Support re-runing the checks. (#3088) - **/ -const createChecks = async ({ core, github }) => { - let status = {}; - for (const check of checks) { - const { data: res } = await github.rest.checks.create({ - owner, - repo, - name: `build / ${check}`, - head_sha: HEAD_SHA, - }); - - core.info(`Created check ${check}`); - status[check] = res; - } - - return status; -}; - -/** - * Dispatch the target workflow. - */ -const dispatchWorkflow = async ({ core, github }) => { - await github.rest.actions.createWorkflowDispatch({ - owner, - repo, - workflow_id, - ref: REF, - inputs: { - title: TITLE, - number: NUMBER, - }, - }); - - // Wait for the workflow to be dispatched. - await sleep(10000); - - // Get the target workflow run - const { - data: { workflow_runs }, - } = await github.rest.actions.listWorkflowRuns({ - owner, - repo, - workflow_id, - head_sha: HEAD_SHA, - }); - - if (workflow_runs.length === 0) { - core.setFailed(`Incorrect workflow runs`); - return; - } - - let sorted_runs = workflow_runs.sort((a, b) => { - return new Date(b.created_at) - new Date(a.created_at); - }); - - return sorted_runs[0]; -}; - -/// List jobs of workflow run. -const listJobs = async ({ github, core, run_id }) => { - const { - data: { jobs }, - } = await github.rest.actions.listJobsForWorkflowRun({ - owner, - repo, - run_id, - }); - - if (jobs.length === 0) { - core.setFailed("Empty jobs from dispatched workflow", jobs); - } else if (jobs.length == 1 && jobs[0].name === "dynamic-profiles") { - core.info("Waiting for matrix job to be completed ... "); - await sleep(5000); - return await listJobs({ github, core, run_id }); - } - - const requiredJobs = jobs.filter((job) => checks.includes(job.name)); - if (requiredJobs.length !== checks.length) { - console.log("all jobs:", jobs); - console.log("required jobs:", requiredJobs); - core.setFailed(`Incorrect count for disptached jobs`); - return; - } - - return requiredJobs; -}; - -/** - * The main function. - **/ -module.exports = async ({ github, core }) => { - await checkSkip({ github, core }); - - const run = await dispatchWorkflow({ core, github }); - core.info(`Dispatched workflow ${run.html_url}`); - let labelChecks = await createChecks({ core, github }); - - // Wait for the jobs to be completed. - while (true) { - const jobs = await listJobs({ github, core, run_id: run.id }); - completed = jobs.filter((job) => job.status === "completed").length; - - for (const job of jobs) { - let checkJob = labelChecks[job.name]; - if ( - checkJob.status !== job.status || - checkJob.conclusion !== job.conclusion - ) { - core.info( - `Updating check ${job.name}, status: ${job.status}, conclusion: ${job.conclusion}` - ); - - let { status, conclusion } = job; - - let data = { - owner, - repo, - check_run_id: checkJob.id, - status, - output: { - title: `Build ${job.name}`, - summary: `ref ${job.html_url}`, - }, - }; - - labelChecks[job.name].status = status; - if (conclusion) { - data.conclusion = conclusion; - labelChecks[job.name].conclusion = conclusion; - } - - await github.rest.checks.update(data); - } else { - continue; - } - } - - if (completed === checks.length) { - core.info("All jobs completed."); - return; - } else { - await sleep(10000); - } - } -}; diff --git a/.github/actions/message/main.js b/.github/actions/message/main.js index 1748233b054..d72d778aa0b 100644 --- a/.github/actions/message/main.js +++ b/.github/actions/message/main.js @@ -55,7 +55,7 @@ async function main() { const { pull_request: { title, - head: { sha, ref: branch }, + head: { sha }, labels: _labels, }, repository: { full_name: fullName }, diff --git a/.github/workflows/build-macos.yml b/.github/workflows/build-macos.yml index 6a73ffcaf4c..b1c24545421 100644 --- a/.github/workflows/build-macos.yml +++ b/.github/workflows/build-macos.yml @@ -7,6 +7,10 @@ on: type: string required: true workflow_dispatch: + inputs: + profiles: + type: string + default: '[{ "name": "debug", "flags": "" }]' env: CARGO_TERM_COLOR: always diff --git a/.github/workflows/build-win-native.yml b/.github/workflows/build-win-native.yml index 975bb3a3a08..5a47d48b6b4 100644 --- a/.github/workflows/build-win-native.yml +++ b/.github/workflows/build-win-native.yml @@ -7,6 +7,10 @@ on: type: string required: true workflow_dispatch: + inputs: + profiles: + type: string + default: '[{ "name": "debug", "flags": "" }]' env: CARGO_TERM_COLOR: always diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index da61adf03d9..d40ee037630 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,6 +23,12 @@ on: number: type: string description: "Pull request number that triggers this workflow" + release: + type: boolean + default: false + production: + type: boolean + default: false run-name: ${{ inputs.title }} ( ${{ format('#{0}', inputs.number) }} ) diff --git a/.github/workflows/label.yml b/.github/workflows/label.yml index e3cad754347..dc027cd678a 100644 --- a/.github/workflows/label.yml +++ b/.github/workflows/label.yml @@ -10,22 +10,47 @@ jobs: runs-on: ubuntu-latest if: >- github.event.label.name == 'A0-pleasereview' - || github.event.label.name == 'E2-forcemacos' || github.event.label.name == 'A4-insubstantial' || github.event.label.name == 'A2-mergeoncegreen' + || github.event.label.name == 'E1-forcenatwin' + || github.event.label.name == 'E2-forcemacos' + steps: - uses: actions/checkout@v4 + + - name: Fork Linux checks + if: >- + github.event.label.name == 'A0-pleasereview' + || github.event.label.name == 'A4-insubstantial' + || github.event.label.name == 'A2-mergeoncegreen' + uses: gear-tech/fork-action@main with: - ref: ${{ github.event.pull_request.head.sha }} - - uses: actions/github-script@v7 - env: - HEAD_SHA: ${{ github.event.pull_request.head.sha }} - LABEL: ${{ github.event.label.name }} - REF: ${{ github.head_ref || github.ref_name }} - TITLE: ${{ github.event.pull_request.title }} - NUMBER: ${{ github.event.number }} - REPO: ${{ github.event.pull_request.head.repo.full_name }} + token: ${{ secrets.GITHUB_TOKEN }} + workflow_id: ".github/workflows/build.yml" + prefix: "build" + jobs: '["linux", "win-cross"]' + useMulti: true + inputs: '{ + "title": "${{ github.event.pull_request.title }}", + "number": "${{ github.event.number }}" + }' + + - name: Fork OSX checks + if: github.event.label.name == 'E2-forcemacos' + uses: gear-tech/fork-action@main + with: + token: ${{ secrets.GITHUB_TOKEN }} + workflow_id: ".github/workflows/build-macos.yml" + prefix: "build" + jobs: '[ "macos-x86", "macos-aarch64" ]' + useProfiles: true + + - name: Fork Win checks + if: github.event.label.name == 'E1-forcenatwin' + uses: gear-tech/fork-action@main with: - script: | - const script = require('./.github/actions/label/build.js'); - await script({ github, core }); + token: ${{ secrets.GITHUB_TOKEN }} + workflow_id: ".github/workflows/build-win-native.yml" + prefix: "build" + jobs: '[ "win-native" ]' + useProfiles: true