Skip to content

Commit

Permalink
Merge pull request #17 from ankushg/ankushg/production-deploys
Browse files Browse the repository at this point in the history
feat: add environment and commit_hash inputs
  • Loading branch information
PatrickHeneise authored Oct 8, 2021
2 parents 0c99f43 + 2de3d30 commit 9ae939d
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 16 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ 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 |
| `wait_until_ready` | optional | Wait until the Cloudflare deployment is ready, defaults to "false" |
| 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" |
| `environment` | optional | Which Cloudflare deployment environments to allow. If set to null, doesn't filter deploys by environment. Defaults to "preview" |
| `commit_hash` | optional | Optional commit hash to filter deployments on. Useful when the same branch can have multiple deploys. |

## Outputs

Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ inputs:
description: 'Wait for deployment to be ready'
required: false
default: false
environment:
description: "Which Cloudflare deployment environments to allow. If set to null, doesn't filter deploys by environment. Defaults to 'preview'"
required: false
default: 'preview'
commit_hash:
description: "Optional commit hash to filter deployments on. Useful when the same branch can have multiple deploys."
required: false
name: Cloudflare Preview URL
outputs:
preview_url:
Expand Down
12 changes: 10 additions & 2 deletions cloudflare.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export default async function getDeploymentUrl(
accountEmail,
projectId,
repo,
branch
branch,
environment,
commitHash
) {
const apiUrl = `https://api.cloudflare.com/client/v4/accounts/${accountId}/pages/projects/${projectId}/deployments`

Expand All @@ -30,9 +32,15 @@ export default async function getDeploymentUrl(
core.info(`Found ${data.result.length} deployments`)
core.debug(`Looking for matching deployments ${repo}/${branch}`)
const builds = data.result
.filter((d) => d.environment === 'preview')
.filter((d) => d.source.config.repo_name === repo)
.filter((d) => d.deployment_trigger.metadata.branch === branch)
.filter((d) => environment == null || d.environment === environment)
.filter(
(d) =>
commitHash == null ||
(d.deployment_trigger.metadata != null &&
d.deployment_trigger.metadata.commit_hash === commitHash)
)

core.info(`Found ${builds.length} matching builds`)
if (!builds || builds.length <= 0) {
Expand Down
18 changes: 15 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.

6 changes: 5 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ async function run() {
const accountEmail = process.env.CLOUDFLARE_ACCOUNT_EMAIL
const projectId = core.getInput('cloudflare_project_id')
const waitForDeploymentReady = core.getInput('wait_until_ready')
const environment = core.getInput('environment', { required: false })
const commitHash = core.getInput('commit_hash', { required: false })

core.info(
`Retrieving deployment preview for ${githubRepo}/${githubBranch} ...`
Expand All @@ -28,7 +30,9 @@ async function run() {
accountEmail,
projectId,
githubRepo,
githubBranch
githubBranch,
environment,
commitHash
)

if (waitForDeploymentReady === 'true') {
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

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

186 changes: 181 additions & 5 deletions test/cloudflare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('getDeploymentUrl() should return a Cloudflare build', async () => {
result: [
{
environment: 'production',
url: 'https://123.cf-project.pages.dev',
url: 'https://main.cf-project.pages.dev',
deployment_trigger: {
type: 'github:push',
metadata: {
Expand Down Expand Up @@ -66,7 +66,9 @@ test('getDeploymentUrl() should return a Cloudflare build', async () => {
'[email protected]',
'cf-project',
'website',
'fix/test-1'
'fix/test-1',
'preview',
null
)

expect(url).toEqual('https://123.cf-project.pages.dev')
Expand All @@ -87,21 +89,188 @@ test('getDeploymentUrl() should fail if there are no deployments', async () => {
'[email protected]',
'cf-project',
'website',
'fix/test-1'
'fix/test-1',
'preview',
null
)
).rejects.toThrow()
})

test('getDeploymentUrl() should check all environments when null', async () => {
const data = {
data: {
result: [
{
environment: 'production',
url: 'https://main.cf-project.pages.dev',
deployment_trigger: {
type: 'github:push',
metadata: {
branch: 'main'
}
},
latest_stage: {
name: 'deploy',
status: 'success'
},
source: {
type: 'github',
config: {
repo_name: 'website',
production_branch: 'main'
}
}
},
{
environment: 'preview',
url: 'https://123.cf-project.pages.dev',
deployment_trigger: {
type: 'github:push',
metadata: {
branch: 'fix/test-1'
}
},
latest_stage: {
name: 'deploy',
status: 'success'
},
source: {
type: 'github',
config: {
repo_name: 'website'
}
}
}
]
}
}
axios.get.mockResolvedValueOnce(data)

const { url } = await getDeploymentUrl(
'123xyz',
'zentered',
'[email protected]',
'cf-project',
'website',
'main',
null,
null
)

expect(url).toEqual('https://main.cf-project.pages.dev')
})

test('getDeploymentUrl() should filter by commitHash when provided', async () => {
const data = {
data: {
result: [
{
environment: 'production',
url: 'https://main-123.cf-project.pages.dev',
deployment_trigger: {
type: 'github:push',
metadata: {
branch: 'main',
commit_hash: '123'
}
},
latest_stage: {
name: 'deploy',
status: 'success'
},
source: {
type: 'github',
config: {
repo_name: 'website',
production_branch: 'main'
}
}
},
{
environment: 'production',
url: 'https://main-456.cf-project.pages.dev',
deployment_trigger: {
type: 'github:push',
metadata: {
branch: 'main',
commit_hash: '456'
}
},
latest_stage: {
name: 'deploy',
status: 'success'
},
source: {
type: 'github',
config: {
repo_name: 'website',
production_branch: 'main'
}
}
},
{
environment: 'preview',
url: 'https://789.cf-project.pages.dev',
deployment_trigger: {
type: 'github:push',
metadata: {
branch: 'fix/test-1',
commit_hash: '789'
}
},
latest_stage: {
name: 'deploy',
status: 'success'
},
source: {
type: 'github',
config: {
repo_name: 'website'
}
}
}
]
}
}
axios.get.mockResolvedValueOnce(data)

const { url } = await getDeploymentUrl(
'123xyz',
'zentered',
'[email protected]',
'cf-project',
'website',
'main',
null,
'456'
)

expect(url).toEqual('https://main-456.cf-project.pages.dev')
})

test('getDeploymentUrl() should fail if there are no matching builds', async () => {
const data = {
data: {
result: [
{
name: 'zentered-co',
url: 'test-123.cloudflare.app',
deployment_trigger: {
type: 'github:push',
metadata: {
branch: 'test-123',
commit_hash: '456'
}
},
meta: {
githubCommitRef: 'does-not-exist',
githubCommitRepo: 'zentered'
},
source: {
type: 'github',
config: {
repo_name: 'website'
}
}
}
]
Expand All @@ -110,6 +279,13 @@ test('getDeploymentUrl() should fail if there are no matching builds', async ()
axios.get.mockResolvedValueOnce(data)

await expect(
getDeploymentUrl('123xyz', 'zentered', 'fix/huge-bug', 'zentered.co')
).rejects.toThrow()
getDeploymentUrl(
'123xyz',
'zentered',
'fix/huge-bug',
'zentered.co',
'preview',
null
)
).rejects.toThrow('no matching builds found')
})

0 comments on commit 9ae939d

Please sign in to comment.