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
This commit is contained in:
liushuyu 2021-12-31 19:14:23 -07:00
parent 45ea8340be
commit ad1f0eed22
2 changed files with 35 additions and 15 deletions

View File

@ -2,10 +2,28 @@
// It is not meant to be executed directly on your machine without modifications // It is not meant to be executed directly on your machine without modifications
const fs = require("fs"); 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) { async function checkCanaryChanges(github, context) {
const delta = new Date() - new Date(context.payload.repository.pushed_at); if (checkBaseChanges(github, context)) return true;
if (delta <= 86400000) return true;
const query = `query($owner:String!, $name:String!, $label:String!) { const query = `query($owner:String!, $name:String!, $label:String!) {
repository(name:$name, owner:$owner) { repository(name:$name, owner:$owner) {
pullRequests(labels: [$label], states: OPEN, first: 100) { pullRequests(labels: [$label], states: OPEN, first: 100) {
@ -22,7 +40,7 @@ async function checkCanaryChanges(github, context) {
const pulls = result.repository.pullRequests.nodes; const pulls = result.repository.pullRequests.nodes;
for (let i = 0; i < pulls.length; i++) { for (let i = 0; i < pulls.length; i++) {
let pull = pulls[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}`); console.info(`${pull.number} updated at ${pull.headRepository.pushedAt}`);
return true; return true;
} }
@ -180,3 +198,4 @@ async function mergebot(github, context, execa) {
module.exports.mergebot = mergebot; module.exports.mergebot = mergebot;
module.exports.checkCanaryChanges = checkCanaryChanges; module.exports.checkCanaryChanges = checkCanaryChanges;
module.exports.tagAndPush = tagAndPush; module.exports.tagAndPush = tagAndPush;
module.exports.checkBaseChanges = checkBaseChanges;

View File

@ -19,25 +19,23 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: ${{ github.event.inputs.nightly != 'false' }} if: ${{ github.event.inputs.nightly != 'false' }}
steps: 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 - uses: actions/github-script@v5
id: check-changes id: check-changes
name: 'Check for new changes' name: 'Check for new changes'
env:
# 24 hours
DETECTION_TIME_FRAME: 86400000
with: with:
result-encoding: string result-encoding: string
script: | script: |
if (context.payload.inputs && context.payload.inputs.nightly === 'true') return true; if (context.payload.inputs && context.payload.inputs.nightly === 'true') return true;
const delta = new Date() - new Date(context.payload.repository.pushed_at); const checkBaseChanges = require('./.github/workflows/ci-merge.js').checkBaseChanges;
if (delta <= 86400000) { return checkBaseChanges(github, context);
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
- run: npm install execa@5 - run: npm install execa@5
if: ${{ steps.check-changes.outputs.result == 'true' }} if: ${{ steps.check-changes.outputs.result == 'true' }}
- uses: actions/checkout@v2 - uses: actions/checkout@v2
@ -71,6 +69,9 @@ jobs:
- uses: actions/github-script@v5 - uses: actions/github-script@v5
id: check-changes id: check-changes
name: 'Check for new changes' name: 'Check for new changes'
env:
# 24 hours
DETECTION_TIME_FRAME: 86400000
with: with:
script: | script: |
if (context.payload.inputs && context.payload.inputs.canary === 'true') return true; if (context.payload.inputs && context.payload.inputs.canary === 'true') return true;