From 1ada96b8f7b5c8bbd6d10232b59c82208fc567a7 Mon Sep 17 00:00:00 2001 From: James Ives Date: Thu, 14 May 2020 17:24:32 -0400 Subject: [PATCH] Deploy Status (#296) * Deploy Status changes * DEPLOY_STATUS -> DEPLOYMENT_STATUS --- README.md | 10 ++++++++++ __tests__/git.test.ts | 4 ++-- __tests__/main.test.ts | 21 ++++++++++++++++++++- action.yml | 6 +++++- src/constants.ts | 8 ++++++++ src/git.ts | 9 +++++---- src/lib.ts | 19 ++++++++++++------- 7 files changed, 62 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 1a714a01..82c5ad33 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,16 @@ In addition to the deployment options you must also configure the following. With the action correctly configured you should see the workflow trigger the deployment under the configured conditions. +#### Deployment Status + +The action will export an environment variable called `DEPLOYMENT_STATUS` that you can use in your workflow to determine if the deployment was successful or not. You can find an explanation of each status code below. + +| Status | Description | +| ------------- |:-------------:| +| `success` | The `success` status indicates that the action was able to successfully deploy the project to the branch. | +| `failed` | The `failed` status indicates that the action encountered an error while trying to deploy the project. | +| `skipped` | The `skipped` status indicates that the action exited early as there was nothing new to deploy. | + --- ### Using an SSH Deploy Key 🔑 diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index 5104cdd8..262bc1af 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -88,7 +88,7 @@ describe('git', () => { pusher: { name: 'asd', email: 'as@cat' - }, + } }) try { @@ -110,7 +110,7 @@ describe('git', () => { name: 'asd', email: 'as@cat' }, - accessToken: '', + accessToken: '' }) try { diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index bccd5c05..140ce144 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -8,7 +8,7 @@ import {action} from '../src/constants' import run from '../src/lib' import {execute} from '../src/execute' import {rmRF} from '@actions/io' -import {setFailed} from '@actions/core' +import {setFailed, exportVariable} from '@actions/core' const originalAction = JSON.stringify(action) @@ -49,6 +49,24 @@ describe('main', () => { await run(action) expect(execute).toBeCalledTimes(18) expect(rmRF).toBeCalledTimes(1) + expect(exportVariable).toBeCalledTimes(1) + }) + + it('should run through the commands and succeed', async () => { + Object.assign(action, { + repositoryPath: 'JamesIves/github-pages-deploy-action', + folder: 'build', + branch: 'branch', + gitHubToken: '123', + pusher: { + name: 'asd', + email: 'as@cat' + } + }) + await run(action) + expect(execute).toBeCalledTimes(17) + expect(rmRF).toBeCalledTimes(1) + expect(exportVariable).toBeCalledTimes(1) }) it('should throw if an error is encountered', async () => { @@ -68,5 +86,6 @@ describe('main', () => { await run(action) expect(execute).toBeCalledTimes(0) expect(setFailed).toBeCalledTimes(1) + expect(exportVariable).toBeCalledTimes(1) }) }) diff --git a/action.yml b/action.yml index 19812f6f..97beef22 100644 --- a/action.yml +++ b/action.yml @@ -66,4 +66,8 @@ inputs: SINGLE_COMMIT: description: "This option can be used if you'd prefer to have a single commit on the deployment branch instead of maintaining the full history." - required: false \ No newline at end of file + required: false + +outputs: + DEPLOYMENT_STATUS: + description: 'The status of the deployment that indicates if the run failed or passed. Possible outputs include: success|failed|skipped' \ No newline at end of file diff --git a/src/constants.ts b/src/constants.ts index 94b49f82..a2a89ac8 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -93,3 +93,11 @@ export const action: ActionInterface = { targetFolder: getInput('TARGET_FOLDER'), workspace: process.env.GITHUB_WORKSPACE || '' } + +/** Status codes for the action. */ +export enum Status { + SUCCESS = 'success', + FAILED = 'failed', + SKIPPED = 'skipped', + RUNNING = 'running' +} diff --git a/src/git.ts b/src/git.ts index 8aa2248d..b949f95d 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,6 +1,6 @@ import {info} from '@actions/core' import {rmRF, mkdirP} from '@actions/io' -import {ActionInterface} from './constants' +import {ActionInterface, Status} from './constants' import {execute} from './execute' import { hasRequiredParameters, @@ -92,7 +92,7 @@ export async function generateBranch(action: ActionInterface): Promise { } /* Runs the necessary steps to make the deployment. */ -export async function deploy(action: ActionInterface): Promise { +export async function deploy(action: ActionInterface): Promise { const temporaryDeploymentDirectory = 'gh-action-temp-deployment-folder' const temporaryDeploymentBranch = 'gh-action-temp-deployment-branch' @@ -179,8 +179,7 @@ export async function deploy(action: ActionInterface): Promise { ) if (!hasFilesToCommit && !action.isTest) { - info('There is nothing to commit. Exiting early… 📭') - return + return Status.SKIPPED } // Commits to GitHub. @@ -236,6 +235,8 @@ export async function deploy(action: ActionInterface): Promise { `git checkout --progress --force ${action.defaultBranch}`, action.workspace ) + + return Status.SUCCESS } catch (error) { throw new Error( `The deploy step encountered an error: ${suppressSensitiveInformation( diff --git a/src/lib.ts b/src/lib.ts index 78e86ea0..2f303444 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -1,5 +1,5 @@ -import {info, setFailed} from '@actions/core' -import {action, ActionInterface} from './constants' +import {exportVariable, info, setFailed} from '@actions/core' +import {action, ActionInterface, Status} from './constants' import {deploy, generateBranch, init} from './git' import {generateRepositoryPath, generateTokenType} from './util' @@ -10,7 +10,7 @@ import {generateRepositoryPath, generateTokenType} from './util' export default async function run( configuration: ActionInterface ): Promise { - let errorState = false + let status: Status = Status.RUNNING try { info('Checking configuration and starting deployment… 🚦') @@ -25,18 +25,23 @@ export default async function run( settings.tokenType = generateTokenType(settings) await init(settings) - await deploy(settings) + status = await deploy(settings) } catch (error) { - errorState = true + status = Status.FAILED setFailed(error.message) } finally { + console.log(status) info( `${ - errorState + status === Status.FAILED ? 'Deployment Failed ❌' - : 'Completed Deployment Successfully! ✅' + : status === Status.SUCCESS + ? 'Completed Deployment Successfully! ✅' + : 'There is nothing to commit. Exiting early… 📭' }` ) + + exportVariable('DEPLOYMENT_STATUS', status) } }