-
Notifications
You must be signed in to change notification settings - Fork 3
/
action.js
125 lines (102 loc) · 2.85 KB
/
action.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const core = require('@actions/core')
const { GoogleAuth } = require('google-auth-library')
const process = require('process')
const fs = require('fs')
function payload() {
try {
return JSON.parse(fs.readFileSync(process.env['GITHUB_EVENT_PATH']))
} catch (error) {
core.setFailed(`Failed to read event payload: ${error}`)
}
}
function buildUrl() {
const event = payload()
switch (process.env['GITHUB_EVENT_NAME']) {
case 'check_run':
return event.check_run.details_url
case 'status':
return event.target_url
default:
core.setFailed(
'Failed to extract build URL from event payload. Is this not a GCB event?'
)
}
}
function buildId() {
const url = new URL(buildUrl())
return url.pathname.split('/').pop()
}
async function fetchBuild() {
const creds = JSON.parse(
core.getInput('google_application_credentials', { required: true })
)
const auth = new GoogleAuth({
scopes: 'https://www.googleapis.com/auth/cloud-platform',
})
const client = auth.fromJSON(creds)
const url = `https://cloudbuild.googleapis.com/v1/projects/${
creds.project_id
}/builds/${buildId()}`
const response = await client.request({ url })
return response.data
}
function fetchDigest(build) {
const targetImage = core.getInput('target_image', { required: true })
if (build.status.toUpperCase() != 'SUCCESS') {
core.setFailed(`Build ${build.id} was not successful`)
return
} else {
const image = build.results.images.find(image =>
image.name.split(':', 2)[0].endsWith(targetImage)
)
if (!image) {
core.setFailed(`Failed to find image matching ${targetImage}`)
return
}
return image.digest
}
}
function fetchSha(build) {
const source = build.sourceProvenance
if (source.hasOwnProperty('resolvedRepoSource')) {
return source.resolvedRepoSource.commitSha
} else if (source.hasOwnProperty('resolvedStorageSource')) {
const object = source.resolvedStorageSource.object
return object.split('-')[0]
} else {
return null
}
}
async function run() {
const event = payload()
let isGCB
switch (process.env['GITHUB_EVENT_NAME']) {
case 'check_run':
isGCB = event.check_run.app.name.toLowerCase() == 'google cloud build'
break
case 'check_suite':
isGCB = event.check_suite.app.name.toLowerCase() == 'google cloud build'
break
case 'status':
isGCB = /gcb build/i.test(event.description)
break
default:
isGCB = false
}
if (isGCB) {
const build = await fetchBuild()
const digest = fetchDigest(build)
const sha = fetchSha(build)
core.setOutput('digest', digest)
if (sha) {
core.setOutput('sha', sha)
}
} else {
core.warning('Event does not appear to be from GCB, ignoring')
}
}
try {
run()
} catch (error) {
core.setFailed(`Action failed with error ${error}`)
}