diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index abfada4..1c3896e 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -1,3 +1,5 @@ +// @ts-check + import run from '../src/action' const core = require('@actions/core') @@ -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', @@ -32,6 +37,7 @@ test('deploy application with alias', async () => { alias: 'app-alias', cleverCLI: 'clever' }) + expect(exec).toHaveBeenNthCalledWith(1, 'clever', [ 'login', '--token', @@ -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') +}) diff --git a/src/action.ts b/src/action.ts index d24e953..bef9866 100644 --- a/src/action.ts +++ b/src/action.ts @@ -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) {