Skip to content

Commit

Permalink
fix: Fail the run when deployment fails
Browse files Browse the repository at this point in the history
Closes #184.
  • Loading branch information
franky47 committed Mar 3, 2022
1 parent cca8a62 commit 1bc1f0d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
27 changes: 27 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

import run from '../src/action'

const core = require('@actions/core')
Expand All @@ -9,11 +11,14 @@ jest.mock('@actions/core')
// --

test('deploy default application (no arguments)', async () => {
exec.mockResolvedValue(0)

await run({
token: 'token',
secret: 'secret',
cleverCLI: 'clever'
})

expect(exec).toHaveBeenNthCalledWith(1, 'clever', [
'login',
'--token',
Expand All @@ -32,6 +37,7 @@ test('deploy application with alias', async () => {
alias: 'app-alias',
cleverCLI: 'clever'
})

expect(exec).toHaveBeenNthCalledWith(1, 'clever', [
'login',
'--token',
Expand Down Expand Up @@ -221,3 +227,24 @@ test('passing extra env variables, using alias only', async () => {
'foo'
])
})

test('deployment failure fails the workflow', async () => {
exec.mockResolvedValue(42)
await run({
token: 'token',
secret: 'secret',
cleverCLI: 'clever'
})
expect(core.setFailed).toHaveBeenCalledWith('Deployment failed with code 42')
})

test('deployment failure with timeout fails the workflow', async () => {
exec.mockResolvedValue(42)
await run({
token: 'token',
secret: 'secret',
cleverCLI: 'clever',
timeout: 10_000
})
expect(core.setFailed).toHaveBeenCalledWith('Deployment failed with code 42')
})
10 changes: 8 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,21 @@ export default async function run({
resolve()
}, timeout)
})
await Promise.race([exec(cleverCLI, args), timeoutPromise])
const result = await Promise.race([exec(cleverCLI, args), timeoutPromise])
if (timeoutID) {
clearTimeout(timeoutID)
}
if (timedOut) {
core.info('Deployment timed out, moving on with workflow run')
}
if (typeof result === 'number' && result !== 0) {
throw new Error(`Deployment failed with code ${result}`)
}
} else {
await exec(cleverCLI, args)
const code = await exec(cleverCLI, args)
if (code !== 0) {
throw new Error(`Deployment failed with code ${code}`)
}
}
} catch (error) {
if (error instanceof Error) {
Expand Down

0 comments on commit 1bc1f0d

Please sign in to comment.