From 12bc36f23911a785cccad56e9be6757d5b332bcc Mon Sep 17 00:00:00 2001 From: Dejan Svetec Date: Wed, 8 Sep 2021 13:52:50 +0200 Subject: [PATCH] Add option to filter commits by deploy context --- README.md | 6 +++++- action.yml | 3 +++ index.js | 16 ++++++++++++---- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fd98131e..55ba9c6d 100644 --- a/README.md +++ b/README.md @@ -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` @@ -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 }} ``` diff --git a/action.yml b/action.yml index 3017a089..b73a6864 100644 --- a/action.yml +++ b/action.yml @@ -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" diff --git a/index.js b/index.js index f4b769ac..1009fc66 100644 --- a/index.js +++ b/index.js @@ -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) => { @@ -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); @@ -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'); @@ -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`;