Skip to content

Commit

Permalink
feat: log message with dashboard url when unexpected error (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjschuster authored Feb 3, 2022
1 parent 31a6c67 commit 1e01692
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
23 changes: 14 additions & 9 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.

7 changes: 5 additions & 2 deletions src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { logDeploymentStages } from './logs'
import { Sdk } from './sdk'
import { Deployment, StagePollIntervalConfig } from './types'

export async function deploy(sdk: Sdk, config: StagePollIntervalConfig = {}): Promise<Deployment> {
export async function deploy(
sdk: Sdk,
pollIntervalConfig: StagePollIntervalConfig = {},
): Promise<Deployment> {
const deployment = await sdk.createDeployment()

await logDeploymentStages(deployment, sdk, config)
await logDeploymentStages(deployment, sdk, pollIntervalConfig)

return await sdk.getDeploymentInfo(deployment.id)
}
9 changes: 7 additions & 2 deletions src/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type Log = { type: 'stage-log'; log: StageLog } | { type: 'new-stage'; na
export async function logDeploymentStages(
{ id, stages }: Deployment,
sdk: Sdk,
config: StagePollIntervalConfig = {},
pollIntervalConfig: StagePollIntervalConfig = {},
): Promise<void> {
for (const { name } of stages) {
let stageLogs: StageLogsResult = await sdk.getStageLogs(id, name)
Expand All @@ -26,7 +26,7 @@ export async function logDeploymentStages(

if (isStageComplete(stageLogs)) break

await wait(config[name] ?? getPollInterval(stageLogs))
await wait(pollIntervalConfig[name] ?? getPollInterval(stageLogs))

lastLogId = getLastLogId(stageLogs)
stageLogs = await sdk.getStageLogs(id, name)
Expand Down Expand Up @@ -71,14 +71,19 @@ function extraStageLogs(stageName: StageName): string[] {

export function getPollInterval(stage: StageLogsResult): number {
switch (stage.name) {
case 'queued':
case 'initialize':
case 'build':
return 15000
case 'clone_repo':
case 'deploy':
default:
return 5000
}
}

// The logs endpoint doesn't offer pagination or tail logging so we have to fetch all logs every poll
// https://api.cloudflare.com/#pages-deployment-get-deployment-stage-logs
function getNewStageLogs(logs: StageLogsResult, lastLogId?: number): StageLog[] {
if (lastLogId === undefined) return logs.data
if (logs.end === lastLogId) return []
Expand Down
16 changes: 11 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { setFailed } from '@actions/core'
import { endGroup, setFailed } from '@actions/core'
import { run } from './run'

try {
run()
} catch (e) {
run().catch((e) => {
endGroup()

setFailed(e instanceof Error ? e.message : `${e}`)
}

console.log(
`\nThere was an unexpected error. It's possible that your Cloudflare Pages deploy is still in progress or was successful. Go to https://dash.cloudflare.com and visit your Pages dashboard for more details.`,
)

return Promise.reject(e)
})

0 comments on commit 1e01692

Please sign in to comment.