From 1134b87f4c8716ea8429a389e32442d10b5edf4e Mon Sep 17 00:00:00 2001 From: Christian Nunciato Date: Thu, 26 Oct 2023 14:39:10 -0700 Subject: [PATCH] Remove Registry module and await in-progress workflows --- .github/workflows/push.yml | 13 +------ Makefile | 4 +++ config/_default/config.yml | 12 ------- scripts/ci/await-in-progress.js | 60 +++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 scripts/ci/await-in-progress.js diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 4b6d6ddfc32..426f431dc18 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -31,14 +31,9 @@ jobs: with: token: ${{ secrets.PULUMI_BOT_TOKEN }} - - name: Fetch the latest pulumi/registry - run: | - hugo mod get github.com/pulumi/registry/themes/default - hugo mod tidy - - name: Build assets run: | - make ensure build-assets + make ensure ci-await build-assets env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -48,12 +43,6 @@ jobs: file_pattern: theme/default/assets/js/bundle.js theme/default/assets/js/marketing.js theme/default/assets/css/bundle.css theme/default/assets/css/marketing.css commit_message: Commit asset bundles - - name: Commit any changes to go.mod/go.sum - uses: stefanzweifel/git-auto-commit-action@v4 - with: - file_pattern: go.* - commit_message: Update go.mod - notify: if: failure() name: Send slack notification diff --git a/Makefile b/Makefile index 0a58cb2d8ce..7fb90675556 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,10 @@ serve-all: build-assets: yarn --cwd ./themes/default/theme run build +.PHONY: ci-await +ci-await: + node ./scripts/ci/await-in-progress.js + .PHONY: ci-pull-request ci-pull-request: ensure lint ./scripts/ci/pull-request.sh diff --git a/config/_default/config.yml b/config/_default/config.yml index d8fdf5297c4..bbfee73b271 100644 --- a/config/_default/config.yml +++ b/config/_default/config.yml @@ -19,18 +19,6 @@ module: target: assets - source: archetypes target: archetypes - - path: github.com/pulumi/registry/themes/default - mounts: - - source: content - target: content - - source: static - target: static - - source: layouts - target: layouts - - source: data - target: data - - source: assets - target: assets security: funcs: diff --git a/scripts/ci/await-in-progress.js b/scripts/ci/await-in-progress.js new file mode 100644 index 00000000000..bfe788d26e5 --- /dev/null +++ b/scripts/ci/await-in-progress.js @@ -0,0 +1,60 @@ +const { Octokit } = require("@octokit/rest"); + +// Wait for any in-progress runs of the same workflow on this branch to complete before +// proceeding. In other words, if the current workflow is an instance of the "foo" +// workflow, and there's another "foo" workflow running for a different commit on the same +// branch as this one, wait for that workflow to complete before exiting (in order to +// prevent the current workflow from continuing). +// Inspired by https://github.com/softprops/turnstyle. +async function waitForInProgressRuns() { + // See https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables + // for an explanation of each of these variables. + const githubToken = process.env.GITHUB_TOKEN; + const currentRunID = parseInt(process.env.GITHUB_RUN_ID, 10); + const workflowName = process.env.GITHUB_WORKFLOW; + const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/"); + const branch = process.env.GITHUB_HEAD_REF || process.env.GITHUB_REF.replace("refs/heads/", ""); + const status = "in_progress"; + + const octokit = new Octokit({ + auth: githubToken, + }); + + // Given the current workflow name, fetch its ID. + const workflows = await octokit.rest.actions.listRepoWorkflows({ owner, repo }); + const workflow_id = workflows.data.workflows.find(workflow => workflow.name === workflowName).id; + + // Fetch a paginated list of in-progress runs of the current workflow. + const runs = await octokit.paginate( + octokit.rest.actions.listWorkflowRuns.endpoint.merge({ + owner, + repo, + branch, + workflow_id, + status, + }), + ); + + // Sort in-progress runs descendingly, excluding the current one. + const recent = runs.sort((a, b) => b.id - a.id).filter(run => run.id < currentRunID); + + console.log(`Found ${recent.length} other ${workflowName} job(s) running on branch ${branch}.`); + + if (recent.length > 0) { + const [mostRecent] = recent; + console.log(`Waiting for ${mostRecent.html_url} to complete before continuing.`); + await Promise.resolve(setTimeout(waitForInProgressRuns, 60000)); // One minute. + } else { + console.log("Continuing."); + } +} + +// Unhandled errors that happen within Promises yield warnings, but do not (yet) cause the +// process to exit nonzero. Since we want this script to fail loudly when something goes +// wrong, we listen for unhandledRejection events and rethrow, exiting 1. +// https://nodejs.org/api/process.html#process_event_unhandledrejection +process.on("unhandledRejection", error => { + throw error; +}); + +waitForInProgressRuns();