Repository navigation
Expand file tree
/
Copy pathenvironments.js
More file actions
106 lines (95 loc) · 3.08 KB
/
Copy pathenvironments.js
File metadata and controls
106 lines (95 loc) · 3.08 KB
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
import {
changedPaths,
dispatchWorkflow,
gitopsRepo,
readFile,
readJson,
tagCommit,
writeSummary
} from './github.js'
import { pinPath } from './gitops.js'
// Files every environment's apply reads.
const SHARED_FILES = new Set(['registry.json', 'global.tfvars.json'])
const NO_COMMIT = /^0{40}$/
/**
* latest-release: the newest ai-platform-infra release, which validates the
* configuration in ai-platform-environments. Exports AIP_RELEASE and AIP_SHA.
*
* @param {object} input - Command context from main.js.
* @returns {Promise<{release: string, sha: string}>} The release.
*/
async function latestRelease({ github, context, core }) {
const infra = gitopsRepo(context.repo.owner, 'infra')
const { data } = await github.rest.repos.getLatestRelease(infra)
const sha = await tagCommit(github, { ...infra, tag: data.tag_name })
core.exportVariable('AIP_RELEASE', data.tag_name)
core.exportVariable('AIP_SHA', sha)
return { release: data.tag_name, sha }
}
/**
* dispatch-changed-environments: after a merge to main of
* ai-platform-environments, dispatches the platform apply for each
* environment whose parameters changed, or for every environment when a
* shared file changed. An environment without a pin is skipped: its first
* pin applies its parameters.
*
* @param {object} input - Command context from main.js; `github` reads this
* repository, `app` dispatches in ai-platform-deployments.
* @returns {Promise<{dispatched: string[]}>} The environments dispatched.
*/
async function dispatchChangedEnvironments({ github, app, context, core }) {
const { owner, repo } = context.repo
const before = context.payload.before ?? ''
const registry = await readJson(github, {
owner,
repo,
path: 'registry.json',
ref: context.sha
})
const paths = NO_COMMIT.test(before)
? null
: await changedPaths(github, {
owner,
repo,
base: before,
head: context.sha
})
const shared = paths === null || paths.some((path) => SHARED_FILES.has(path))
const changed = registry.environments
.map((entry) => entry.name)
.filter(
(name) =>
shared || paths.some((path) => path.startsWith(`environments/${name}/`))
)
const deployments = gitopsRepo(owner, 'deployments')
const dispatched = []
const lines = []
for (const environment of changed) {
const pin = await readFile(app, {
...deployments,
path: pinPath(environment),
ref: 'main'
})
if (pin === null) {
lines.push(`- \`${environment}\`: no pin yet; its first pin applies it`)
continue
}
const url = await dispatchWorkflow(app, {
...deployments,
workflow: 'apply.yml',
inputs: {
environment,
reason: `parameters changed in ${context.sha.slice(0, 7)}`
}
})
dispatched.push(environment)
lines.push(`- \`${environment}\`: ${url ?? 'started'}`)
}
await writeSummary(
core,
'Platform applies dispatched',
lines.length > 0 ? lines : ['No environment changed.']
)
return { dispatched }
}
export { dispatchChangedEnvironments, latestRelease }