This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild-only-changed-images.ts
executable file
·171 lines (147 loc) · 5.29 KB
/
rebuild-only-changed-images.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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* OpenCRVS is also distributed under the terms of the Civil Registration
* & Healthcare Disclaimer located at http://opencrvs.org/license.
*
* Copyright (C) The OpenCRVS Authors. OpenCRVS and the OpenCRVS
* graphic logo are (registered/a) trademark(s) of Plan International.
*/
import * as yaml from 'js-yaml'
import { readFileSync, writeFileSync } from 'fs'
import fetch from 'node-fetch'
import * as cp from 'child_process'
import { promisify } from 'util'
const exec = promisify(cp.exec.bind(cp))
const { DOCKER_USERNAME, DOCKER_PASSWORD } = process.env
async function getToken() {
const response = await fetch('https://hub.docker.com/v2/users/login/', {
method: 'POST',
body: JSON.stringify({
username: DOCKER_USERNAME,
password: DOCKER_PASSWORD
}),
headers: {
'Content-Type': 'application/json'
}
})
const body = await response.json()
return body.token
}
async function getLatestTag(token: string, repository: string) {
const response = await fetch(
`https://hub.docker.com/v2/repositories/${repository}/tags`,
{
headers: {
Authorization: `JWT ${token}`
}
}
)
const body = await response.json()
if (!body.results) {
return null
}
const latest = body.results.find(({ name }) => name === 'latest')
return body.results.find(
({ name, images }) =>
name !== 'latest' && images[0].digest === latest.images[0].digest
)
}
const IMAGE_NAME_TO_PACKAGE_NAME = {
styleguide: 'components'
}
async function preventBuildImageFromBeingBuilt() {
const pkg = JSON.parse(readFileSync('./package.json', 'utf8'))
pkg.scripts['build:image'] = 'echo "Skipping build image creation..."'
writeFileSync('./package.json', JSON.stringify(pkg))
}
async function ignoreFromBuild(packages: string[]) {
const pkg = JSON.parse(readFileSync('./package.json', 'utf8'))
pkg.scripts['build'] =
pkg.scripts['build'] +
' ' +
packages.map(directory => `--ignore @opencrvs/${directory}`).join(' ')
writeFileSync('./package.json', JSON.stringify(pkg))
}
async function isDependencyOf(dependency: string, packageName: string) {
const pkg = JSON.parse(
readFileSync(`./packages/${packageName}/package.json`, 'utf8')
)
return (
pkg.dependencies[`@opencrvs/${dependency}`] ||
pkg.devDependencies[`@opencrvs/${dependency}`]
)
}
async function run() {
const compose = yaml.safeLoad(readFileSync('./docker-compose.yml', 'utf8'))
const token = await getToken()
// Not sure why but docker hub's API sometimes fails if you query it right after getting a token
await new Promise(resolve => setTimeout(resolve, 1000))
const toPackageName = serviceName => {
const service = compose.services[serviceName]
const { image } = service
const repository = image.split(':')[0]
const imageName = repository.replace('opencrvs/ocrvs-', '')
return IMAGE_NAME_TO_PACKAGE_NAME[imageName] || imageName
}
const serviceNames = Object.keys(compose.services)
const allPackages = serviceNames.map(toPackageName)
const packagesThatDontNeedRebuilding = []
for (const serviceName of serviceNames) {
const service = compose.services[serviceName]
const { image } = service
const repository = image.split(':')[0]
const packageName = toPackageName(serviceName)
const latestGitHash = (await exec(
`git --no-pager log -n 1 --format="%h" -- "packages/${packageName}"`
)).trim()
const latestTag = await getLatestTag(token, repository)
if (!latestTag) {
console.log(
'⚠️ ',
serviceName,
': no tags found for repository',
repository
)
continue
}
const imageHash = latestTag.name
try {
// Check that image hash is newer or the same as the latest commit of this package
await exec(`git merge-base --is-ancestor ${latestGitHash} ${imageHash}`)
console.log('✅ ', serviceName, ': no rebuild needed')
service.image = `${repository}:latest`
delete service.build
packagesThatDontNeedRebuilding.push(packageName)
} catch {
console.log('♻️ ', serviceName, ': rebuilding...')
}
}
writeFileSync('./docker-compose.yml', yaml.safeDump(compose))
const packagesThatNeedRebuilding = allPackages.filter(
packageName => !packagesThatDontNeedRebuilding.includes(packageName)
)
if (packagesThatNeedRebuilding.length === 0) {
console.log('No packages to rebuild. Removing build image creation step.')
await preventBuildImageFromBeingBuilt()
} else {
/*
* Rebuild also packages that other rebuilt packages are dependant of
* For example if client needs to be rebuilt, then components also needs to be part of the build image
*/
const packagesThatRebuiltPackagesArentDependendOn = packagesThatDontNeedRebuilding.filter(
packageName =>
packagesThatNeedRebuilding.some(rebuiltPackage =>
isDependencyOf(packageName, rebuiltPackage)
)
)
console.log(
'Following packages ignored from build image building',
packagesThatRebuiltPackagesArentDependendOn
)
await ignoreFromBuild(packagesThatRebuiltPackagesArentDependendOn)
}
}
run()