Skip to content

Commit

Permalink
Merge pull request #3 from dnsv/deploy-context
Browse files Browse the repository at this point in the history
Add option to filter commits by deploy context
  • Loading branch information
quantizor authored Oct 26, 2022
2 parents fb529b9 + 12bc36f commit da0b4e5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ This action uses the Netlify API to always retrieve the correct deployment being

Optional — The amount of time to spend waiting on the Netlify deployment to respond with a success HTTP code after reaching "ready" status. Defaults to 60 seconds.

### `context`

Optional — The Netlify deploy context. Can be `branch-deploy`, `production` or `deploy-preview`. Defaults to all of them.

## Outputs

### `url`
Expand All @@ -43,7 +47,7 @@ steps:
site_id: 'YOUR_SITE_ID' # See Settings > Site Details > General in the Netlify UI
env:
NETLIFY_TOKEN: ${{ secrets.NETLIFY_TOKEN }}

# Then use it in a later step like:
# ${{ steps.waitForDeployment.outputs.url }}
```
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ inputs:
max_timeout:
description: "The max time to wait after the deployment is ready for a valid HTTP status code."
required: false
context:
description: "The Netlify deploy context. Can be branch-deploy, production or deploy-preview."
required: false
outputs:
deploy_id:
description: "The Netlify deployment ID"
Expand Down
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function getNetlifyUrl(url) {
});
}

const waitForDeployCreation = (url, commitSha, MAX_TIMEOUT) => {
const waitForDeployCreation = (url, commitSha, MAX_TIMEOUT, context) => {
const increment = 15;

return new Promise((resolve, reject) => {
Expand All @@ -32,7 +32,7 @@ const waitForDeployCreation = (url, commitSha, MAX_TIMEOUT) => {
return reject(`Failed to get deployments for site`);
}

const commitDeployment = netlifyDeployments.find((d) => d.commit_ref === commitSha);
const commitDeployment = netlifyDeployments.find((d) => d.commit_ref === commitSha && (!context || d.context === context));

if (commitDeployment) {
clearInterval(handle);
Expand Down Expand Up @@ -98,6 +98,7 @@ const run = async () => {
const MAX_WAIT_TIMEOUT = 60 * 15; // 15 min
const MAX_READY_TIMEOUT = Number(core.getInput('max_timeout')) || 60;
const siteId = core.getInput('site_id');
const context = core.getInput('context');

if (!netlifyToken) {
core.setFailed('Please set NETLIFY_TOKEN env variable to your Netlify Personal Access Token secret');
Expand All @@ -109,11 +110,18 @@ const run = async () => {
core.setFailed('Required field `site_id` was not provided');
}

console.log(`Waiting for Netlify to create a deployment for git SHA ${commitSha}`);
let message = `Waiting for Netlify to create a deployment for git SHA ${commitSha}`;

if (context) {
message += ` and context ${context}`
}

console.log(message);
const commitDeployment = await waitForDeployCreation(
`https://api.netlify.com/api/v1/sites/${siteId}/deploys`,
commitSha,
MAX_CREATE_TIMEOUT
MAX_CREATE_TIMEOUT,
context
);

const url = `https://${commitDeployment.id}--${commitDeployment.name}.netlify.app`;
Expand Down

0 comments on commit da0b4e5

Please sign in to comment.