|
1 | 1 | import * as core from '@actions/core'
|
2 | 2 | import * as github from '@actions/github'
|
3 |
| -import { create } from './create' |
4 |
| - |
5 |
| -import { deleteAll } from './delete-all' |
6 |
| -import { deleteDeployment } from './delete' |
7 |
| -import { ActionType, DeploymentStatus, getInput, DEPLOYMENT_ID_STATE_NAME, getEnvironment } from './utils' |
| 3 | +import { complete } from './complete' |
| 4 | +import { ActionType, DeploymentStatus, getInput, DEPLOYMENT_ID_STATE_NAME, getEnvironment, postSlackNotification } from './utils' |
8 | 5 |
|
9 | 6 | export async function run (): Promise<void> {
|
10 | 7 | let token: string
|
11 | 8 | let type: ActionType
|
12 |
| - let logsUrl: string |
13 |
| - let description: string |
14 |
| - let status: DeploymentStatus |
15 |
| - let environment: string |
16 |
| - let environmentUrl: string |
17 | 9 | let deploymentId: string
|
18 |
| - let mainBranch: string |
| 10 | + let jobStatus: string |
| 11 | + let environment: string |
| 12 | + let slackToken: string |
| 13 | + let slackChannel: string |
| 14 | + let deploymentConfidenceUrl: string |
| 15 | + let mutateDeployment: boolean |
| 16 | + let currentSha: string |
19 | 17 |
|
20 |
| - const { actor, ref } = github.context |
| 18 | + const { actor, ref, repo, sha } = github.context |
21 | 19 |
|
22 |
| - console.log('### main.context ###') |
| 20 | + console.log('### post.context ###') |
23 | 21 | console.log(`actor: ${actor}`)
|
24 | 22 | console.log(`ref: ${ref}`)
|
| 23 | + console.log(`owner: ${repo.owner}`) |
| 24 | + console.log(`repo: ${repo.repo}`) |
| 25 | + console.log(`compare: ${github.context.payload.compare as string}`) |
| 26 | + console.log(`new_sha: ${sha}`) |
25 | 27 | console.log('\n')
|
26 | 28 |
|
27 | 29 | try {
|
28 |
| - console.log('### main.inputs ###') |
| 30 | + console.log('### post.inputs ###') |
29 | 31 | token = getInput('token', { required: true }) ?? ''
|
30 | 32 |
|
31 | 33 | type = getInput('type', { required: true }) as ActionType
|
32 | 34 | console.log(`type: ${type}`)
|
33 | 35 |
|
34 |
| - logsUrl = getInput('logs') ?? '' |
35 |
| - console.log(`logs: ${logsUrl}`) |
| 36 | + jobStatus = getInput('job_status') ?? 'success' |
| 37 | + console.log(`job_status: ${jobStatus}`) |
36 | 38 |
|
37 |
| - description = getInput('description') ?? `deployed by ${actor}` |
38 |
| - console.log(`description: ${description}`) |
| 39 | + environment = getEnvironment(ref) |
39 | 40 |
|
40 |
| - status = (getInput('status') ?? 'in_progress') as DeploymentStatus |
41 |
| - console.log(`status: ${status}`) |
| 41 | + slackToken = getInput('slack_token') ?? '' |
| 42 | + console.log(`slack_token: ${slackToken === '' ? 'none' : 'passed'}`) |
42 | 43 |
|
43 |
| - environment = getEnvironment(ref) |
| 44 | + slackChannel = getInput('slack_channel') ?? '' |
| 45 | + console.log(`slack_channel: ${slackChannel}`) |
44 | 46 |
|
45 |
| - environmentUrl = getInput('environment_url') ?? '' |
46 |
| - console.log(`environmentUrl: ${environmentUrl}`) |
| 47 | + // We want to mutate the Deployment by default, unless the deployment |
| 48 | + // was already mutated by another action and we just want to notify |
| 49 | + mutateDeployment = getInput('mutate_deployment') !== 'false' |
| 50 | + console.log(`mutate_deployment: ${mutateDeployment.toString()}`) |
47 | 51 |
|
48 |
| - mainBranch = getInput('main_branch') ?? 'master' |
49 |
| - console.log(`main branch: ${mainBranch}`) |
| 52 | + currentSha = getInput('current_sha') ?? sha |
| 53 | + console.log(`current_sha: ${currentSha}`) |
50 | 54 |
|
51 |
| - const shouldRequireDeploymentId = type === 'delete' |
52 |
| - deploymentId = getInput(DEPLOYMENT_ID_STATE_NAME, { required: shouldRequireDeploymentId }) ?? '0' |
53 |
| - console.log(`deploymentId: ${deploymentId}`) |
| 55 | + deploymentConfidenceUrl = getInput('deployment_confidence_url') ?? '' |
| 56 | + console.log(`deployment confidence dashboard URL: ${deploymentConfidenceUrl}`) |
54 | 57 | } catch (error) {
|
55 | 58 | core.error(error)
|
56 | 59 | core.setFailed(`Wrong parameters given: ${JSON.stringify(error, null, 2)}`)
|
57 | 60 | throw error
|
58 | 61 | }
|
59 | 62 | console.log('\n')
|
| 63 | + console.log('### post ###') |
60 | 64 |
|
61 | 65 | const client = new github.GitHub(token, { previews: ['ant-man', 'flash'] })
|
62 |
| - |
63 |
| - console.log('### run ###') |
| 66 | + const status: DeploymentStatus = jobStatus === 'success' ? 'success' : 'failure' |
| 67 | + console.log(`status: ${status}`) |
64 | 68 |
|
65 | 69 | switch (type) {
|
66 | 70 | case 'create':
|
67 |
| - try { |
68 |
| - // If a deployment was already created on a previous job, |
69 |
| - // don't create one again. |
70 |
| - if (deploymentId === '0') { |
71 |
| - deploymentId = await create( |
72 |
| - client, |
73 |
| - logsUrl, |
74 |
| - description, |
75 |
| - status, |
76 |
| - environment, |
77 |
| - environmentUrl, |
78 |
| - mainBranch |
79 |
| - ) |
80 |
| - } |
81 |
| - console.log(`saveState::${DEPLOYMENT_ID_STATE_NAME}: ${deploymentId}`) |
82 |
| - core.saveState(DEPLOYMENT_ID_STATE_NAME, deploymentId) // for internal use |
83 |
| - core.setOutput(DEPLOYMENT_ID_STATE_NAME, deploymentId) // keep that output for external dependencies |
84 |
| - } catch (error) { |
85 |
| - core.error(error) |
86 |
| - core.setFailed(`Create deployment failed: ${JSON.stringify(error, null, 2)}`) |
87 |
| - throw error |
| 71 | + deploymentId = core.getState(DEPLOYMENT_ID_STATE_NAME) |
| 72 | + console.log(`deploymentId: ${deploymentId}`) |
| 73 | + if (deploymentId === undefined || deploymentId === '') { |
| 74 | + console.log('No deploymentId provided, skip status update') |
| 75 | + return |
88 | 76 | }
|
89 |
| - break |
90 |
| - case 'delete': |
| 77 | + |
| 78 | + // Post Slack notification |
| 79 | + await postSlackNotification(slackToken, slackChannel, environment, status, github.context, deploymentConfidenceUrl, currentSha) |
| 80 | + |
91 | 81 | try {
|
92 |
| - await deleteDeployment( |
93 |
| - client, |
94 |
| - Number(deploymentId) |
95 |
| - ) |
| 82 | + // If the deployment was managed by another workflow we don't want to mutate it here |
| 83 | + if (mutateDeployment) await complete(client, Number(deploymentId), status) |
96 | 84 | } catch (error) {
|
| 85 | + if (error.name === 'HttpError' && error.status === 404) { |
| 86 | + console.log('Couldn\'t complete a deployment: not found') |
| 87 | + return |
| 88 | + } |
97 | 89 | core.error(error)
|
98 |
| - core.setFailed(`Delete deployment failed: ${JSON.stringify(error, null, 2)}`) |
| 90 | + core.setFailed(`Complete deployment failed: ${JSON.stringify(error, null, 2)}`) |
99 | 91 | throw error
|
100 | 92 | }
|
| 93 | + |
101 | 94 | break
|
102 |
| - case 'delete-all': |
103 |
| - try { |
104 |
| - await deleteAll( |
105 |
| - client, |
106 |
| - environment |
107 |
| - ) |
108 |
| - } catch (error) { |
109 |
| - core.error(error) |
110 |
| - core.setFailed(`Delete all deployments failed: ${JSON.stringify(error, null, 2)}`) |
111 |
| - throw error |
112 |
| - } |
| 95 | + default: |
| 96 | + console.log(`No post script for type: ${type}`) |
113 | 97 | break
|
114 | 98 | }
|
115 | 99 | }
|
|
0 commit comments