-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
198 lines (163 loc) · 5.39 KB
/
main.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import path from 'path'
import {
getLatestPublishedVersionRecord,
setLatestPublishedVersion,
} from './lib/latest-published-version'
import { getTextFromEnv } from './lib/env'
import { runCommand } from './lib/shell'
import {
generateTempName,
createDirectory,
copyFile,
readFile,
writeFile,
deleteDirectory,
} from './lib/fs'
import { parseSemver } from './lib/semver'
import { log, info, warning, error } from './lib/log'
import settings from './repos/settings'
const BUILD_FOR_ARCHS = ['amd64', 'armhf']
const MULTIARCH_ALPINE_BRANCH = 'edge'
const MULTIARCH_UBUNTU_BRANCH = 'bionic-slim'
function failWithError(msg) {
error(msg)
process.exit(1)
}
async function main() {
const BUILD = getTextFromEnv('BUILD')
const DOCKER_USERNAME = getTextFromEnv('DOCKER_USERNAME')
const DOCKER_PASSWORD = getTextFromEnv('DOCKER_PASSWORD')
const AIRTABLE_BASE_URL = getTextFromEnv('AIRTABLE_BASE_URL')
const AIRTABLE_API_KEY = getTextFromEnv('AIRTABLE_API_KEY')
const airtableCreds = {
baseUrl: AIRTABLE_BASE_URL,
apiKey: AIRTABLE_API_KEY,
}
const isOsBuild = BUILD.startsWith('_')
const buildName = isOsBuild ? BUILD.substring(1) : BUILD
const buildPath = BUILD
let buildSettings = null
if (!isOsBuild) {
buildSettings = settings[buildName]
}
info(`Building Crossarch images for ${BUILD_FOR_ARCHS.join(', ')}`)
log('Registering QEMU...')
await runCommand('docker', [
'run',
'--rm',
'--privileged',
'multiarch/qemu-user-static:register',
'--reset',
])
/*
* Build
*/
for (const arch of BUILD_FOR_ARCHS) {
const tempDir = generateTempName('crossarch')
await createDirectory(tempDir)
await copyFile(
path.join(__dirname, 'repos', buildPath, 'Dockerfile'),
path.join(tempDir, 'Dockerfile')
)
let imageToUse
if (!isOsBuild) {
imageToUse = `crossarch/${buildSettings.image}:${arch}-latest`
} else {
if (buildName === 'alpine') {
imageToUse = `multiarch/alpine:${arch}-${MULTIARCH_ALPINE_BRANCH}`
} else if (buildName === 'ubuntu') {
imageToUse = `multiarch/ubuntu-debootstrap:${arch}-${MULTIARCH_UBUNTU_BRANCH}`
}
}
log(`Using ${imageToUse}`)
const prepend = `\
FROM ${imageToUse}
ENV CROSSARCH_ARCH=${arch}
RUN echo "Building image for \${CROSSARCH_ARCH}"`
const dockerfilePath = path.join(tempDir, 'Dockerfile')
const dockerfileContent = await readFile(dockerfilePath)
await writeFile(dockerfilePath, `${prepend}\n${dockerfileContent}`)
log(`Building ${arch} image...`)
await runCommand('docker', ['build', '--no-cache', '-t', `build:${arch}`, tempDir])
await deleteDirectory(tempDir)
}
/*
* Getting version
*/
const repofile = await import(path.join(__dirname, 'repos', buildPath, 'Repofile.js')) // eslint-disable-line
const call = args => runCommand('docker', ['run', '--rm', 'build:amd64'].concat(args), true)
let version
try {
version = await repofile.getVersion(call)
} catch (err) {
failWithError(`Could not find repo version: ${err.stack}`)
}
const publishedVersionRecord = await getLatestPublishedVersionRecord(airtableCreds, buildName)
if (publishedVersionRecord.version === version && !isOsBuild) {
warning('Software not updated since last push - skipping deployment')
return
}
/*
* Deployment
*/
info(`Deploying ${buildName} (${version})`)
log('Pushing images to Docker Hub...')
const tags = ['latest']
if (isOsBuild || buildSettings.versioning === 'as-is') {
tags.push(version)
} else {
let manipulatedVersion = version
if (buildSettings.versioning === 'major-minor') {
manipulatedVersion += '.0'
}
const semver = parseSemver(manipulatedVersion)
tags.push(semver.major)
tags.push(`${semver.major}.${semver.minor}`)
if (buildSettings.versioning === 'semver') {
tags.push(`${semver.major}.${semver.minor}.${semver.patch}`)
}
}
await runCommand('docker', ['login', '-u', DOCKER_USERNAME, '-p', DOCKER_PASSWORD])
for (const arch of BUILD_FOR_ARCHS) {
log(`Pushing tags ${tags.join(', ')}`)
for (let tag of tags) {
await runCommand('docker', ['tag', `build:${arch}`, `crossarch/${buildName}:${arch}-${tag}`])
await runCommand('docker', ['push', `crossarch/${buildName}:${arch}-${tag}`])
}
}
info('Creating and pushing manifests...')
for (const tag of tags) {
const childImages = BUILD_FOR_ARCHS.map(arch => `crossarch/${buildName}:${arch}-${tag}`)
await runCommand('docker', [
'manifest',
'create',
`crossarch/${buildName}:${tag}`,
...childImages,
])
for (const arch of BUILD_FOR_ARCHS) {
let archDescription = ['--os', 'linux']
if (arch === 'amd64') {
archDescription = archDescription.concat(['--arch', 'amd64'])
} else if (arch === 'armhf') {
archDescription = archDescription.concat(['--arch', 'arm', '--variant', 'v7'])
}
await runCommand('docker', [
'manifest',
'annotate',
`crossarch/${buildName}:${tag}`,
`crossarch/${buildName}:${arch}-${tag}`,
...archDescription,
])
}
await runCommand('docker', ['manifest', 'push', `crossarch/${buildName}:${tag}`])
}
info('Updating latest published version...')
await setLatestPublishedVersion(airtableCreds, publishedVersionRecord, version)
}
main()
.then(() => {
info('Done.')
})
.catch(err => {
failWithError(err.stack)
})