Skip to content

Commit

Permalink
My first action is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
wbeuil committed Nov 11, 2020
1 parent c31f7ea commit 7b71840
Show file tree
Hide file tree
Showing 369 changed files with 88,436 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# wait-for-deployment
Github action that waits for a deployment
# Wait For GitHub Deployment

Wait for a GitHub deployment to complete and get its url
16 changes: 16 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Wait For GitHub Deployment
description: Wait for a GitHub deployment to complete and get its url
inputs:
token:
description: Your GitHub access token
required: true
timeout:
description: The max time to run the action (in seconds)
required: false
default: "120"
outputs:
url:
description: The url of the new deployment
runs:
using: node12
main: index.js
84 changes: 84 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const core = require("@actions/core");
const github = require("@actions/github");

const sleep = (seconds) =>
new Promise((resolve) => setTimeout(resolve, seconds * 1000));

async function waitForDeployment() {
const { eventName, payload, repo } = github.context;
const token = core.getInput("token");
const timeout = core.getInput("timeout") * 1000;
const endTime = new Date().getTime() + timeout;

let params = {
...repo,
};

core.debug(`eventName? ${eventName}`);

if (eventName === "pull_request") {
params = {
...params,
sha: payload.pull_request.head.sha,
environment: "Preview",
};
} else if (eventName === "push") {
params = {
...params,
sha: payload.head_commit.id,
environment: "Production",
};
} else {
throw new Error(`Unhandled event: ${eventName}`);
}

let attempt = 1;

const octokit = github.getOctokit(token);

while (new Date().getTime() < endTime) {
try {
const { data: deployments } = await octokit.repos.listDeployments(params);

if (deployments.length > 1) {
throw new Error(
`There should be only one deployment for ${params.sha} but found ${deployments.length} instead.`
);
}

for (const deployment of deployments) {
const { data: statuses } = await octokit.repos.listDeploymentStatuses({
...repo,
deployment_id: deployment.id,
});

const [success] = statuses.filter(
(status) => status.state === "success"
);

if (success) {
return success.target_url;
}
}
} catch (error) {
throw error;
}

console.log(`Url unavailable. Attempt ${attempt++}.`);

await sleep(2);
}

throw new Error(
`Timeout reached before deployment for ${params.sha} was found.`
);
}

(async () => {
try {
const url = await waitForDeployment();
core.setOutput("url", url);
} catch (err) {
core.setFailed(err.message);
}
})();
9 changes: 9 additions & 0 deletions node_modules/@actions/core/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

147 changes: 147 additions & 0 deletions node_modules/@actions/core/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions node_modules/@actions/core/lib/command.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions node_modules/@actions/core/lib/command.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/@actions/core/lib/command.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7b71840

Please sign in to comment.