forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeployUtils.ts
219 lines (195 loc) · 7.61 KB
/
DeployUtils.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import fs from "fs-extra"
import { BuildkiteTrigger } from "../baker/BuildkiteTrigger.js"
import { logErrorAndMaybeSendToBugsnag, warn } from "../serverUtils/errorLog.js"
import { DeployQueueServer } from "./DeployQueueServer.js"
import {
BAKED_SITE_DIR,
BAKED_BASE_URL,
BUILDKITE_API_ACCESS_TOKEN,
SLACK_BOT_OAUTH_TOKEN,
} from "../settings/serverSettings.js"
import { SiteBaker } from "../baker/SiteBaker.js"
import { WebClient } from "@slack/web-api"
import { DeployChange, DeployMetadata } from "@ourworldindata/utils"
import { KnexReadWriteTransaction } from "../db/db.js"
const deployQueueServer = new DeployQueueServer()
export const defaultCommitMessage = async (): Promise<string> => {
let message = "Automated update"
// In the deploy.sh script, we write the current git rev to 'public/head.txt'
// and want to include it in the deploy commit message
try {
const sha = await fs.readFile("public/head.txt", "utf8")
message += `\nowid/owid-grapher@${sha}`
} catch (err) {
warn(err)
}
return message
}
/**
* Initiate a deploy, without any checks. Throws error on failure.
*/
// TODO: this transaction is only RW because somewhere inside it we fetch images
const triggerBakeAndDeploy = async (
deployMetadata: DeployMetadata,
knex: KnexReadWriteTransaction,
lightningQueue?: DeployChange[]
) => {
// deploy to Buildkite if we're on master and BUILDKITE_API_ACCESS_TOKEN is set
if (BUILDKITE_API_ACCESS_TOKEN) {
const buildkite = new BuildkiteTrigger()
if (lightningQueue?.length) {
await buildkite
.runLightningBuild(
lightningQueue.map((change) => change.slug!),
deployMetadata
)
.catch(logErrorAndMaybeSendToBugsnag)
} else {
await buildkite
.runFullBuild(deployMetadata)
.catch(logErrorAndMaybeSendToBugsnag)
}
} else {
// otherwise, bake locally. This is used for local development or staging servers
const baker = new SiteBaker(BAKED_SITE_DIR, BAKED_BASE_URL)
if (lightningQueue?.length) {
if (!lightningQueue.every((change) => change.slug))
throw new Error("Lightning deploy is missing a slug")
await baker.bakeGDocPosts(
knex,
lightningQueue.map((c) => c.slug!)
)
} else {
await baker.bakeAll(knex)
}
}
}
const getChangesAuthorNames = (queueItems: DeployChange[]): string[] => {
// Do not remove duplicates here, because we want to show the history of changes within a deploy
return queueItems
.map((item) => `${item.message} (by ${item.authorName})`)
.filter(Boolean)
}
const getChangesSlackMentions = async (
queueItems: DeployChange[]
): Promise<string[]> => {
const emailSlackMentionMap = await getEmailSlackMentionsMap(queueItems)
// Do not remove duplicates here, because we want to show the history of changes within a deploy
return queueItems.map(
(item) =>
`${item.message} (by ${
!item.authorEmail
? item.authorName
: emailSlackMentionMap.get(item.authorEmail) ??
item.authorName
})`
)
}
const getEmailSlackMentionsMap = async (
queueItems: DeployChange[]
): Promise<Map<string, string>> => {
// SLACK_BOT_OAUTH_TOKEN is not available on staging environments
if (!SLACK_BOT_OAUTH_TOKEN) return new Map()
const slackClient = new WebClient(SLACK_BOT_OAUTH_TOKEN)
// Get unique author emails
const uniqueAuthorEmails = [
...new Set(queueItems.map((item) => item.authorEmail)),
]
// Get a Map of email -> Slack mention (e.g. "<@U123456>")
const emailSlackMentionMap = new Map()
await Promise.all(
uniqueAuthorEmails.map(async (authorEmail) => {
if (authorEmail) {
const slackId = await getSlackMentionByEmail(
authorEmail,
slackClient
)
if (slackId) {
emailSlackMentionMap.set(authorEmail, slackId)
}
}
})
)
return emailSlackMentionMap
}
/**
*
* Get a Slack mention for a given email address. Format it according to the
* Slack API requirements to mention a user in a message
* (https://api.slack.com/reference/surfaces/formatting#mentioning-users).
*/
const getSlackMentionByEmail = async (
email: string | undefined,
slackClient: WebClient
): Promise<string | undefined> => {
if (!email) return
try {
const response = await slackClient.users.lookupByEmail({ email })
return response.user?.id ? `<@${response.user.id}>` : undefined
} catch (error) {
await logErrorAndMaybeSendToBugsnag(
`Error looking up email "${email}" in slack: ${error}`
)
}
return
}
const MAX_SUCCESSIVE_FAILURES = 2
/** Whether a deploy is currently running */
let deploying = false
/**
* Initiate deploy if no other deploy is currently pending, and there are changes
* in the queue.
* If there is a deploy pending, another one will be automatically triggered at
* the end of the current one, as long as there are changes in the queue.
* If there are no changes in the queue, a deploy won't be initiated.
*/
export const deployIfQueueIsNotEmpty = async (
// TODO: this transaction is only RW because somewhere inside it we fetch images
knex: KnexReadWriteTransaction
) => {
if (deploying) return
deploying = true
let failures = 0
while (
!(await deployQueueServer.queueIsEmpty()) &&
failures < MAX_SUCCESSIVE_FAILURES
) {
const deployContent =
await deployQueueServer.readQueuedAndPendingFiles()
// Truncate file immediately. Ideally this would be an atomic action, otherwise it's
// possible that another process writes to this file in the meantime...
await deployQueueServer.clearQueueFile()
// Write to `.pending` file to be able to recover the deploy message
// in case of failure.
await deployQueueServer.writePendingFile(deployContent)
const parsedQueue = deployQueueServer.parseQueueContent(deployContent)
// Log the changes that are about to be deployed in a text format.
const dateStr: string = new Date().toISOString()
const changesAuthorNames = getChangesAuthorNames(parsedQueue)
console.log(
`Deploying site...\n---\n📆 ${dateStr}\n\n${changesAuthorNames.join(
"\n"
)}\n---`
)
try {
const changesSlackMentions =
await getChangesSlackMentions(parsedQueue)
await triggerBakeAndDeploy(
{ title: changesAuthorNames[0], changesSlackMentions },
knex,
// If every DeployChange is a lightning change, then we can do a
// lightning deploy. In the future, we might want to separate
// lightning updates from regular deploys so we could prioritize
// them, no matter the content of the queue.
parsedQueue.every(isLightningChange) ? parsedQueue : undefined
)
await deployQueueServer.deletePendingFile()
} catch (err) {
failures++
// The error is already sent to Slack inside the deploy() function.
// The deploy will be retried unless we've reached MAX_SUCCESSIVE_FAILURES.
}
}
deploying = false
}
const isLightningChange = (item: DeployChange) => item.slug