Skip to content

Commit

Permalink
feat: wait for deployments to be ready
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickHeneise committed Jul 6, 2021
1 parent 833d8ed commit 8b75e27
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 20 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Here are the steps for an example job:
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
with:
cloudflare_project_id: 'repo-name'
wait_until_ready: true
- name: Get URL
run: echo "https://${{ steps.cloudflare_preview_url.outputs.preview_url }}"
```
Expand All @@ -54,9 +55,10 @@ In the repository, go to "Settings", then "Secrets" and add "CLOUDFLARE_API_TOKE

## Inputs

| Name | Requirement | Description |
| ----------------------- | ----------- | -------------------------- |
| `cloudflare_project_id` | required | Cloudflare project id/name |
| Name | Requirement | Description |
| ----------------------- | ----------- | ------------------------------------------------------------------ |
| `cloudflare_project_id` | required | Cloudflare project id/name |
| `wait_until_ready` | optional | Wait until the Cloudflare deployment is ready, defaults to "false" |

## Outputs

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ inputs:
cloudflare_project_id:
description: 'Cloudflare project id / name'
required: true
wait_until_ready:
description: 'Wait for deployment to be ready'
required: false
default: false
name: Cloudflare Preview URL
outputs:
preview_url:
Expand Down
35 changes: 35 additions & 0 deletions cloudflare-statuscheck.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import core from '@actions/core'
import axios from 'axios'

export default async function waitForDeployment(
token,
accountId,
accountEmail,
projectId,
deploymentId
) {
const apiUrl = `https://api.cloudflare.com/client/v4/accounts/${accountId}/pages/projects/${projectId}/deployments`

core.info(`Checking deployment status: ${deploymentId}`)

const { data } = await axios.get(apiUrl, {
headers: {
'X-Auth-Key': token,
'X-Auth-Email': accountEmail
},
responseType: 'json',
responseEncoding: 'utf8'
})

const build = data.result.filter((d) => d.id === deploymentId)[0]

if (!build) {
core.error(data)
core.setFailed('no build with this ID found.')
}

return (
build.latest_stage.name === 'deploy' &&
build.latest_stage.status === 'success'
)
}
5 changes: 2 additions & 3 deletions cloudflare.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export default async function getDeploymentUrl(
core.info(
`Preview URL: ${build.url} (${build.latest_stage.name} - ${build.latest_stage.status})`
)
return {
url: build.url
}

return build
}
66 changes: 63 additions & 3 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

26 changes: 25 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import core from '@actions/core'
import getDeploymentUrl from './cloudflare.mjs'
import checkDeploymentStatus from './cloudflare-statuscheck.mjs'

async function delay(ms) {
return await new Promise((resolve) => setTimeout(resolve, ms))
}

async function run() {
try {
Expand All @@ -11,12 +16,13 @@ async function run() {
const accountId = process.env.CLOUDFLARE_ACCOUNT_ID
const accountEmail = process.env.CLOUDFLARE_ACCOUNT_EMAIL
const projectId = core.getInput('cloudflare_project_id')
const waitForDeploymentReady = core.getInput('wait_until_ready')

core.info(
`Retrieving deployment preview for ${githubRepo}/${githubBranch} ...`
)

const { url } = await getDeploymentUrl(
const { id, url } = await getDeploymentUrl(
cloudflareToken,
accountId,
accountEmail,
Expand All @@ -25,6 +31,24 @@ async function run() {
githubBranch
)

if (waitForDeploymentReady === 'true') {
let deploymentReady = false

while (!deploymentReady) {
deploymentReady = await checkDeploymentStatus(
cloudflareToken,
accountId,
accountEmail,
projectId,
id
)

if (!deploymentReady) {
await delay(2000)
}
}
}

core.setOutput('preview_url', url)
} catch (error) {
core.setFailed(error.message)
Expand Down
60 changes: 60 additions & 0 deletions test/cloudflare-statuscheck.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable node/no-unsupported-features/es-syntax */
import checkDeploymentStatus from '../cloudflare-statuscheck.mjs'
import axios from 'axios'

jest.mock('@actions/core', () => {
return {
info: jest.fn()
}
})
jest.mock('axios')

test('waitForDeployment() should wait until a deployment is successful', async () => {
axios.get.mockResolvedValueOnce({
data: {
result: [
{
id: '123abc',
environment: 'preview',
latest_stage: {
name: 'deploy',
status: 'initialized'
}
}
]
}
})

axios.get.mockResolvedValueOnce({
data: {
result: [
{
id: '123abc',
environment: 'preview',
latest_stage: {
name: 'deploy',
status: 'success'
}
}
]
}
})

const firstCheck = await checkDeploymentStatus(
'123xyz',
'zentered',
'[email protected]',
'cf-project',
'123abc'
)

const secondCheck = await checkDeploymentStatus(
'123xyz',
'zentered',
'[email protected]',
'cf-project',
'123abc'
)
expect(firstCheck).toEqual(false)
expect(secondCheck).toEqual(true)
})
18 changes: 9 additions & 9 deletions index.test.js → test/cloudflare.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable node/no-unsupported-features/es-syntax */
import getDeploymentUrl from './cloudflare.mjs'
import getDeploymentUrl from '../cloudflare.mjs'
import axios from 'axios'

jest.mock('@actions/core', () => {
Expand All @@ -18,16 +18,16 @@ test('getDeploymentUrl() should return a Cloudflare build', async () => {
{
environment: 'production',
url: 'https://123.cf-project.pages.dev',
latest_stage: {
name: 'deploy',
status: 'success'
},
deployment_trigger: {
type: 'github:push',
metadata: {
branch: 'main'
}
},
latest_stage: {
name: 'deploy',
status: 'success'
},
source: {
type: 'github',
config: {
Expand All @@ -39,16 +39,16 @@ test('getDeploymentUrl() should return a Cloudflare build', async () => {
{
environment: 'preview',
url: 'https://123.cf-project.pages.dev',
latest_stage: {
name: 'deploy',
status: 'success'
},
deployment_trigger: {
type: 'github:push',
metadata: {
branch: 'fix/test-1'
}
},
latest_stage: {
name: 'deploy',
status: 'success'
},
source: {
type: 'github',
config: {
Expand Down

0 comments on commit 8b75e27

Please sign in to comment.