Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Remove Registry module and await in-progress workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
cnunciato committed Oct 26, 2023
1 parent 166f3fb commit 1134b87
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 24 deletions.
13 changes: 1 addition & 12 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions config/_default/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
60 changes: 60 additions & 0 deletions scripts/ci/await-in-progress.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 1134b87

Please sign in to comment.