From ad1f0eed222818cd757127da4541168a7912acec Mon Sep 17 00:00:00 2001 From: liushuyu Date: Fri, 31 Dec 2021 19:14:23 -0700 Subject: [PATCH] CI: make auto-publish workflow more robust and flexible ... * workaround an issue where sometimes GHA does not pass repository object into the context variable * make detection interval adjustable --- .github/workflows/ci-merge.js | 25 ++++++++++++++++++++++--- .github/workflows/publish.yml | 25 +++++++++++++------------ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci-merge.js b/.github/workflows/ci-merge.js index d80091b3e..44e385d16 100644 --- a/.github/workflows/ci-merge.js +++ b/.github/workflows/ci-merge.js @@ -2,10 +2,28 @@ // It is not meant to be executed directly on your machine without modifications const fs = require("fs"); +// how far back in time should we consider the changes are "recent"? (default: 24 hours) +const DETECTION_TIME_FRAME = (parseInt(process.env.DETECTION_TIME_FRAME)) || (24 * 3600 * 1000); + +async function checkBaseChanges(github, context) { + // a special robustness handling for when GHA did not pass the repository info + if (!context.payload.repository) { + const result = await github.rest.repos.get({ + owner: context.repo.owner, + repo: context.repo.repo, + }); + context.payload.repository = result.data; + } + const delta = new Date() - new Date(context.payload.repository.pushed_at); + if (delta <= DETECTION_TIME_FRAME) { + console.info('New changes detected, triggering a new build.'); + return true; + } + return false; +} async function checkCanaryChanges(github, context) { - const delta = new Date() - new Date(context.payload.repository.pushed_at); - if (delta <= 86400000) return true; + if (checkBaseChanges(github, context)) return true; const query = `query($owner:String!, $name:String!, $label:String!) { repository(name:$name, owner:$owner) { pullRequests(labels: [$label], states: OPEN, first: 100) { @@ -22,7 +40,7 @@ async function checkCanaryChanges(github, context) { const pulls = result.repository.pullRequests.nodes; for (let i = 0; i < pulls.length; i++) { let pull = pulls[i]; - if (new Date() - new Date(pull.headRepository.pushedAt) <= 86400000) { + if (new Date() - new Date(pull.headRepository.pushedAt) <= DETECTION_TIME_FRAME) { console.info(`${pull.number} updated at ${pull.headRepository.pushedAt}`); return true; } @@ -180,3 +198,4 @@ async function mergebot(github, context, execa) { module.exports.mergebot = mergebot; module.exports.checkCanaryChanges = checkCanaryChanges; module.exports.tagAndPush = tagAndPush; +module.exports.checkBaseChanges = checkBaseChanges; diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index c603603ed..add387c8e 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -19,25 +19,23 @@ jobs: runs-on: ubuntu-latest if: ${{ github.event.inputs.nightly != 'false' }} steps: + # this checkout is required to make sure the GitHub Actions scripts are available + - uses: actions/checkout@v2 + name: Pre-checkout + with: + submodules: false - uses: actions/github-script@v5 id: check-changes name: 'Check for new changes' + env: + # 24 hours + DETECTION_TIME_FRAME: 86400000 with: result-encoding: string script: | if (context.payload.inputs && context.payload.inputs.nightly === 'true') return true; - const delta = new Date() - new Date(context.payload.repository.pushed_at); - if (delta <= 86400000) { - return true; - } - console.log('No new changes detected.'); - return false; - # this checkout is required to make sure the GitHub Actions scripts are available - - uses: actions/checkout@v2 - if: ${{ steps.check-changes.outputs.result == 'true' }} - name: Pre-checkout - with: - submodules: false + const checkBaseChanges = require('./.github/workflows/ci-merge.js').checkBaseChanges; + return checkBaseChanges(github, context); - run: npm install execa@5 if: ${{ steps.check-changes.outputs.result == 'true' }} - uses: actions/checkout@v2 @@ -71,6 +69,9 @@ jobs: - uses: actions/github-script@v5 id: check-changes name: 'Check for new changes' + env: + # 24 hours + DETECTION_TIME_FRAME: 86400000 with: script: | if (context.payload.inputs && context.payload.inputs.canary === 'true') return true;