Skip to content
Merged
9 changes: 9 additions & 0 deletions .github/actions/awaitStagingDeploys/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: 'Await staging deploys'
description: 'Waits for any active staging deploys to finish'
inputs:
GITHUB_TOKEN:
description: Auth token for New Expensify Github
required: true
runs:
using: 'node12'
main: './index.js'
48 changes: 48 additions & 0 deletions .github/actions/awaitStagingDeploys/awaitStagingDeploys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const _ = require('underscore');
const GitHubUtils = require('../../libs/GithubUtils');
const {promiseDoWhile} = require('../../libs/promiseWhile');

function run() {
let currentStagingDeploys = [];
return promiseDoWhile(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW our async/await ban really kills me here, in my opinion this would be much cleaner written like so:

const _ = require('underscore');
const GitHubUtils = require('../../libs/GithubUtils');

async function run() {
    let currentStagingDeploys = [];
    do {
        const response = await GitHubUtils.octokit.actions.listWorkflowRuns({
            owner: GitHubUtils.GITHUB_OWNER,
            repo: GitHubUtils.APP_REPO,
            workflow_id: 'platformDeploy.yml',
            event: 'push',
        });
        currentStagingDeploys = _.filter(response.data.workflow_runs, workflowRun => workflowRun.status !== 'completed');
        
        console.log(
            _.isEmpty(currentStagingDeploys)
                ? 'No current staging deploys found'
                : `Found ${currentStagingDeploys.length} staging deploy${currentStagingDeploys.length > 1 ? 's' : ''} still running...`,
        );
        
        await new Promise(resolve => setTimeout(resolve, GitHubUtils.POLL_RATE * 9))
    } while(!_.isEmpty(currentStagingDeploys));
}

if (require.main === module) {
    run();
}

module.exports = run;

() => !_.isEmpty(currentStagingDeploys),
_.throttle(
() => Promise.all([
// These are active deploys
GitHubUtils.octokit.actions.listWorkflowRuns({
owner: GitHubUtils.GITHUB_OWNER,
repo: GitHubUtils.APP_REPO,
workflow_id: 'platformDeploy.yml',
event: 'push',
}),

// These have the potential to become active deploys, so we need to wait for them to finish as well
// In this context, we'll refer to unresolved preDeploy workflow runs as staging deploys as well
GitHubUtils.octokit.actions.listWorkflowRuns({
owner: GitHubUtils.GITHUB_OWNER,
repo: GitHubUtils.APP_REPO,
workflow_id: 'preDeploy.yml',
}),
])
.then(responses => [
...responses[0].data.workflow_runs,
...responses[1].data.workflow_runs,
])
.then(workflowRuns => currentStagingDeploys = _.filter(workflowRuns, workflowRun => workflowRun.status !== 'completed'))
.then(() => console.log(
_.isEmpty(currentStagingDeploys)
? 'No current staging deploys found'
: `Found ${currentStagingDeploys.length} staging deploy${currentStagingDeploys.length > 1 ? 's' : ''} still running...`,
)),

// Poll every 60 seconds instead of every 10 seconds
GitHubUtils.POLL_RATE * 6,
),
);
}

if (require.main === module) {
run();
}

module.exports = run;
Loading