Skip to content

Commit

Permalink
ci(label): skip duplicated build on PR creation (#3155)
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop authored and shamilsan committed Sep 11, 2023
1 parent d880b9f commit 1e16d23
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
51 changes: 39 additions & 12 deletions .github/actions/label/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
*/

const [owner, repo] = ["gear-tech", "gear"];
const { LABEL, REF, HEAD_SHA } = process.env;
const linux = LABEL === "A0-pleasereview";
const { LABEL, REF, HEAD_SHA, TITLE, NUMBER } = process.env;
const linux =
LABEL === "A0-pleasereview" ||
LABEL === "A4-insubstantial" ||
LABEL === "A2-mergeoncegreen";
const checks = linux ? ["linux", "win-cross"] : ["x86"];
const workflow_id = linux
? ".github/workflows/build.yml"
Expand All @@ -18,7 +21,7 @@ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
/**
* If skipping this action.
**/
const skip = async ({ github }) => {
const skip = async ({ core, github }) => {
const {
data: { check_runs },
} = await github.rest.checks.listForRef({
Expand All @@ -27,16 +30,29 @@ const skip = async ({ github }) => {
ref: REF,
});

core.info(`check runs: ${check_runs}`);

const runs = linux
? check_runs.filter((run) => run.name === "build" || run.name === "build / linux")
: check_runs.filter((run) => run.name === "build / macox-x86");
? check_runs.filter(
(run) => run.name === "build" || run.name === "build / linux"
)
: check_runs.filter((run) => run.name === "build / macos-x86");

if (runs.length === 0) return false;
// Skip this action by default.
let skipped = false;
for (run of runs) {
if (run.conclusion !== "skipped") return true;
// Process this action only if the previous build has been skipped.
if (
(run.name === "build" && run.conclusion === "skipped")
)
skipped = true;

// If there is already a build, skip this action without more conditions.
if (run.name === "build / linux" || run.name === "build / macos-x86")
return true;
}

return false;
return !skipped;
};

/**
Expand Down Expand Up @@ -72,8 +88,13 @@ const dispatchWorkflow = async ({ core, github }) => {
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
Expand All @@ -86,12 +107,16 @@ const dispatchWorkflow = async ({ core, github }) => {
head_sha: HEAD_SHA,
});

if (workflow_runs.length != 1) {
if (workflow_runs.length === 0) {
core.setFailed(`Incorrect workflow runs`);
return;
}

return workflow_runs[0];
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.
Expand Down Expand Up @@ -122,14 +147,16 @@ const listJobs = async ({ github, core, run_id }) => {
* The main function.
**/
module.exports = async ({ github, core }) => {
if (await skip({ github })) {
if (await skip({ core, github })) {
core.info("Build has already been processed.");
return;
}

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;
Expand Down Expand Up @@ -171,7 +198,7 @@ module.exports = async ({ github, core }) => {

if (completed === checks.length) {
core.info("All jobs completed.");
break;
return;
} else {
await sleep(10000);
}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/PR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ jobs:
needs: status
if: >-
contains(github.event.pull_request.labels.*.name, 'A0-pleasereview')
|| contains(github.event.pull_request.labels.*.name, 'A4-insubstantial')
|| contains(github.event.pull_request.labels.*.name, 'A2-mergeoncegreen')
uses: ./.github/workflows/build.yml
with:
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ on:
type: boolean
default: false
workflow_dispatch:
inputs:
title:
type: string
description: "Pull request title that triggers this workflow."
number:
type: string
description: "Pull request number that triggers this workflow"

run-name: ${{ inputs.title }} ( ${{ format('#{0}', inputs.number) }} )

env:
CARGO_INCREMENTAL: 0
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ on:
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'
|| github.event.label.name == 'A4-insubstantial'
|| github.event.label.name == 'A2-mergeoncegreen'
steps:
- uses: actions/checkout@v3
with:
Expand All @@ -24,6 +22,8 @@ jobs:
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 }}
with:
script: |
const script = require('./.github/actions/label/build.js');
Expand Down

0 comments on commit 1e16d23

Please sign in to comment.