Skip to content

Commit 1ada96b

Browse files
authored
Deploy Status (JamesIves#296)
* Deploy Status changes * DEPLOY_STATUS -> DEPLOYMENT_STATUS
1 parent a524440 commit 1ada96b

File tree

7 files changed

+62
-15
lines changed

7 files changed

+62
-15
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ In addition to the deployment options you must also configure the following.
151151

152152
With the action correctly configured you should see the workflow trigger the deployment under the configured conditions.
153153

154+
#### Deployment Status
155+
156+
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.
157+
158+
| Status | Description |
159+
| ------------- |:-------------:|
160+
| `success` | The `success` status indicates that the action was able to successfully deploy the project to the branch. |
161+
| `failed` | The `failed` status indicates that the action encountered an error while trying to deploy the project. |
162+
| `skipped` | The `skipped` status indicates that the action exited early as there was nothing new to deploy. |
163+
154164
---
155165

156166
### Using an SSH Deploy Key 🔑

__tests__/git.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('git', () => {
8888
pusher: {
8989
name: 'asd',
9090
email: 'as@cat'
91-
},
91+
}
9292
})
9393

9494
try {
@@ -110,7 +110,7 @@ describe('git', () => {
110110
name: 'asd',
111111
email: 'as@cat'
112112
},
113-
accessToken: '',
113+
accessToken: ''
114114
})
115115

116116
try {

__tests__/main.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {action} from '../src/constants'
88
import run from '../src/lib'
99
import {execute} from '../src/execute'
1010
import {rmRF} from '@actions/io'
11-
import {setFailed} from '@actions/core'
11+
import {setFailed, exportVariable} from '@actions/core'
1212

1313
const originalAction = JSON.stringify(action)
1414

@@ -49,6 +49,24 @@ describe('main', () => {
4949
await run(action)
5050
expect(execute).toBeCalledTimes(18)
5151
expect(rmRF).toBeCalledTimes(1)
52+
expect(exportVariable).toBeCalledTimes(1)
53+
})
54+
55+
it('should run through the commands and succeed', async () => {
56+
Object.assign(action, {
57+
repositoryPath: 'JamesIves/github-pages-deploy-action',
58+
folder: 'build',
59+
branch: 'branch',
60+
gitHubToken: '123',
61+
pusher: {
62+
name: 'asd',
63+
email: 'as@cat'
64+
}
65+
})
66+
await run(action)
67+
expect(execute).toBeCalledTimes(17)
68+
expect(rmRF).toBeCalledTimes(1)
69+
expect(exportVariable).toBeCalledTimes(1)
5270
})
5371

5472
it('should throw if an error is encountered', async () => {
@@ -68,5 +86,6 @@ describe('main', () => {
6886
await run(action)
6987
expect(execute).toBeCalledTimes(0)
7088
expect(setFailed).toBeCalledTimes(1)
89+
expect(exportVariable).toBeCalledTimes(1)
7190
})
7291
})

action.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,8 @@ inputs:
6666

6767
SINGLE_COMMIT:
6868
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."
69-
required: false
69+
required: false
70+
71+
outputs:
72+
DEPLOYMENT_STATUS:
73+
description: 'The status of the deployment that indicates if the run failed or passed. Possible outputs include: success|failed|skipped'

src/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,11 @@ export const action: ActionInterface = {
9393
targetFolder: getInput('TARGET_FOLDER'),
9494
workspace: process.env.GITHUB_WORKSPACE || ''
9595
}
96+
97+
/** Status codes for the action. */
98+
export enum Status {
99+
SUCCESS = 'success',
100+
FAILED = 'failed',
101+
SKIPPED = 'skipped',
102+
RUNNING = 'running'
103+
}

src/git.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {info} from '@actions/core'
22
import {rmRF, mkdirP} from '@actions/io'
3-
import {ActionInterface} from './constants'
3+
import {ActionInterface, Status} from './constants'
44
import {execute} from './execute'
55
import {
66
hasRequiredParameters,
@@ -92,7 +92,7 @@ export async function generateBranch(action: ActionInterface): Promise<void> {
9292
}
9393

9494
/* Runs the necessary steps to make the deployment. */
95-
export async function deploy(action: ActionInterface): Promise<void> {
95+
export async function deploy(action: ActionInterface): Promise<Status> {
9696
const temporaryDeploymentDirectory = 'gh-action-temp-deployment-folder'
9797
const temporaryDeploymentBranch = 'gh-action-temp-deployment-branch'
9898

@@ -179,8 +179,7 @@ export async function deploy(action: ActionInterface): Promise<void> {
179179
)
180180

181181
if (!hasFilesToCommit && !action.isTest) {
182-
info('There is nothing to commit. Exiting early… 📭')
183-
return
182+
return Status.SKIPPED
184183
}
185184

186185
// Commits to GitHub.
@@ -236,6 +235,8 @@ export async function deploy(action: ActionInterface): Promise<void> {
236235
`git checkout --progress --force ${action.defaultBranch}`,
237236
action.workspace
238237
)
238+
239+
return Status.SUCCESS
239240
} catch (error) {
240241
throw new Error(
241242
`The deploy step encountered an error: ${suppressSensitiveInformation(

src/lib.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {info, setFailed} from '@actions/core'
2-
import {action, ActionInterface} from './constants'
1+
import {exportVariable, info, setFailed} from '@actions/core'
2+
import {action, ActionInterface, Status} from './constants'
33
import {deploy, generateBranch, init} from './git'
44
import {generateRepositoryPath, generateTokenType} from './util'
55

@@ -10,7 +10,7 @@ import {generateRepositoryPath, generateTokenType} from './util'
1010
export default async function run(
1111
configuration: ActionInterface
1212
): Promise<void> {
13-
let errorState = false
13+
let status: Status = Status.RUNNING
1414

1515
try {
1616
info('Checking configuration and starting deployment… 🚦')
@@ -25,18 +25,23 @@ export default async function run(
2525
settings.tokenType = generateTokenType(settings)
2626

2727
await init(settings)
28-
await deploy(settings)
28+
status = await deploy(settings)
2929
} catch (error) {
30-
errorState = true
30+
status = Status.FAILED
3131
setFailed(error.message)
3232
} finally {
33+
console.log(status)
3334
info(
3435
`${
35-
errorState
36+
status === Status.FAILED
3637
? 'Deployment Failed ❌'
37-
: 'Completed Deployment Successfully! ✅'
38+
: status === Status.SUCCESS
39+
? 'Completed Deployment Successfully! ✅'
40+
: 'There is nothing to commit. Exiting early… 📭'
3841
}`
3942
)
43+
44+
exportVariable('DEPLOYMENT_STATUS', status)
4045
}
4146
}
4247

0 commit comments

Comments
 (0)