/
depot.ts
179 lines (160 loc) · 5.93 KB
/
depot.ts
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as http from '@actions/http-client'
import * as csv from 'csv-parse/sync'
import * as fs from 'fs'
import * as handlebars from 'handlebars'
import * as path from 'path'
import type {Inputs} from './context'
import * as context from './context'
const client = new http.HttpClient('depot-build-push-action')
export async function isInstalled(): Promise<boolean> {
try {
const {exitCode} = await exec.getExecOutput('depot', [], {ignoreReturnCode: true, silent: true})
return exitCode === 0
} catch {
return false
}
}
export async function version() {
await exec.exec('depot', ['version'], {failOnStdErr: false})
}
export async function build(inputs: Inputs) {
const defaultContext = context.getDefaultBuildContext()
const resolvedContext = handlebars.compile(inputs.context)({defaultContext})
const args = [
'build',
...flag('--add-host', inputs.addHosts),
...flag('--allow', inputs.allow.join(',')),
...flag('--build-arg', inputs.buildArgs),
...flag('--build-context', inputs.buildContexts),
...flag('--cache-from', inputs.cacheFrom),
...flag('--cache-to', inputs.cacheTo),
...flag('--cgroup-parent', inputs.cgroupParent),
...flag('--file', inputs.file),
...flag('--iidfile', isLocalOrTarOutput(inputs.outputs) ? false : getImageIDFile()),
...flag('--label', inputs.labels),
...flag('--load', inputs.load),
...flag('--metadata-file', getMetadataFile()),
...flag('--network', inputs.network),
...flag('--no-cache', inputs.noCache),
...flag('--no-cache-filter', inputs.noCacheFilters),
...flag('--output', inputs.outputs),
...flag('--platform', inputs.platforms.join(',')),
...flag('--project', inputs.project),
...flag('--pull', inputs.pull),
...flag('--push', inputs.push),
...flag('--shm-size', inputs.shmSize),
...flag('--ssh', inputs.ssh),
...flag('--tag', inputs.tags),
...flag('--target', inputs.target),
...flag('--ulimit', inputs.ulimit),
// Secrets
...flag(
'--secret',
inputs.secrets.map((secret) => getSecret(secret)).filter((i): i is string => Boolean(i)),
),
...flag(
'--secret',
inputs.secretFiles.map((secret) => getSecret(secret, true)).filter((i): i is string => Boolean(i)),
),
...flag(
'--secret',
inputs.githubToken && !hasGitAuthTokenSecret(inputs.secrets) && resolvedContext.startsWith(defaultContext)
? getSecret(`GIT_AUTH_TOKEN=${inputs.githubToken}`)
: false,
),
// Build context
resolvedContext,
]
// Attempt to exchange GitHub Actions OIDC token for temporary Depot trust relationship token
let token = inputs.token ?? process.env.DEPOT_TOKEN
if (!token) {
try {
const odicToken = await core.getIDToken('https://depot.dev')
const res = await client.postJson<{ok: boolean; token: string}>(
'https://depot.dev/api/auth/oidc/github-actions',
{token: odicToken},
)
if (res.result && res.result.token) {
token = res.result.token
core.info(`Exchanged GitHub Actions OIDC token for temporary Depot token`)
}
} catch (err) {
core.info(`Unable to exchange GitHub OIDC token for temporary Depot token: ${err}`)
}
}
const res = await exec.getExecOutput('depot', args, {
ignoreReturnCode: true,
env: {
...process.env,
...(token ? {DEPOT_TOKEN: token} : {}),
},
})
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(`depot failed with: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`)
}
}
function flag(name: string, value: string | string[] | boolean | undefined): string[] {
if (!value) return []
if (value === true) return [name]
if (Array.isArray(value)) return value.flatMap((item) => [name, item])
return [name, value]
}
function isLocalOrTarOutput(outputs: string[]) {
const rows: string[][] = csv.parse(outputs.join('\n'), {
columns: false,
relaxColumnCount: true,
trim: true,
})
for (const row of rows) {
if (row.length === 1 && !row[0].startsWith('type=')) return true
for (const [key, value] of row.map((item) => item.split('=').map((item) => item.trim()))) {
if (key === 'type' && (value === 'local' || value === 'tar')) return true
}
}
return false
}
function hasGitAuthTokenSecret(secrets: string[]): boolean {
return secrets.some((secret) => secret.startsWith('GIT_AUTH_TOKEN='))
}
function getSecret(secret: string, isFile?: boolean) {
try {
const idx = secret.indexOf('=')
const key = secret.substring(0, idx)
let value = secret.substring(idx + 1)
if (!key || !value) throw new Error(`Invalid secret: ${secret}`)
if (isFile) {
if (!fs.existsSync(value)) throw new Error(`Secret file not found: ${value}`)
value = fs.readFileSync(value, 'utf8')
}
const secretFile = context.getTempFile()
fs.writeFileSync(secretFile, value)
return `id=${key},src=${secretFile}`
} catch (error: any) {
core.warning(error.message)
}
}
function getImageIDFile(): string {
return path.join(context.getTempDir(), 'iidfile').split(path.sep).join(path.posix.sep)
}
export function getImageID(): string | undefined {
const iidFile = getImageIDFile()
if (!fs.existsSync(iidFile)) return undefined
return fs.readFileSync(iidFile, {encoding: 'utf-8'}).trim()
}
export function getMetadataFile(): string {
return path.join(context.getTempDir(), 'metadata-file').split(path.sep).join(path.posix.sep)
}
export function getMetadata(): string | undefined {
const metadataFile = getMetadataFile()
if (!fs.existsSync(metadataFile)) return undefined
const content = fs.readFileSync(metadataFile, {encoding: 'utf-8'}).trim()
if (content === 'null') return undefined
return content
}
export function getDigest(source?: string): string | undefined {
if (source === undefined) return undefined
const metadata = JSON.parse(source)
return metadata['containerimage.digest']
}