forked from dmrd/exponent-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.js
131 lines (109 loc) · 2.96 KB
/
pipeline.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
/* global CI, git, Promise, spawnAsync */
const {
Docker,
Log,
} = CI;
export default {
config: {
name: '📚 Docs',
shortname: 'docs',
description: 'Docs Build/Deploy',
branches: 'master',
},
steps: (branch, tag) => ([
build(branch, tag),
CI.waitStep(),
deploy(branch, tag),
CI.waitStep(),
updateSearchIndex(branch, tag),
]),
};
const build = (branch, tag) => ({
name: `:hammer: Build`,
agents: {
queue: 'builder',
},
async command() {
const imageName = `gcr.io/exponentjs/docs`;
const imageTag = `${process.env.BUILDKITE_COMMIT}`;
Log.collapsed(':hammer: Building Docs...');
const buildScript = `make all`;
await Docker.runInContainer({
image: 'gcr.io/exponentjs/docs-builder:latest',
volumes: {
[`${CI.getBuildDir()}`]: '/root/docs',
},
}, buildScript);
Log.collapsed(':docker: Building docker image...');
await Docker.build({
imageName,
imageTag,
context: '.',
args: {
DOCS_VERSION: `v${require('./package.json').version}`,
},
});
await Docker.push({ imageName, imageTag });
},
});
const deploy = (branch, tag) => ({
name: `:rocket: Deploy`,
async command() {
if (branch !== 'master' && !tag) {
return;
}
const imageName = `gcr.io/exponentjs/docs`;
const imageTag = `${process.env.BUILDKITE_COMMIT}`;
const deployScript = `#!/bin/bash
set -eo pipefail
appExists=$(kubectl get deployments -l app=$APP_NAME -o name --namespace=$KUBE_NAMESPACE)
if [ "$appExists" != "" ]; then
envsubst < ./deploy/k8s/docs-deployment.template.yml | kubectl apply --record -f -
else # create the app deployment
envsubst < ./deploy/k8s/docs-deployment.template.yml | kubectl create --record -f -
fi`;
await Docker.runInContainer({
image: 'gcr.io/exponentjs/deployer',
env: {
DOCKER_IMAGE: imageName,
DOCKER_TAG: imageTag,
APP_NAME: 'docs',
KUBE_NAMESPACE: 'production',
COMMIT_HASH: process.env.BUILDKITE_COMMIT,
},
volumes: {
[CI.getBuildDir()]: '/workdir',
},
}, deployScript);
},
});
const updateSearchIndex = (branch, tag) => ({
name: `:feelsgood: Update Search Index`,
async command() {
if (branch !== 'master' && !tag) {
return;
}
Log.collapsed(':timer_clock: Waiting 10 seconds...');
await setTimeoutAsync(10000);
Log.collapsed(':open_mouth: Updating search index...');
await spawnAsync('node', [
'scripts/update-search-index.js',
], {
stdio: 'inherit',
});
},
});
function pad(n) {
return n < 10 ? `0${n}` : `${n}`;
}
async function makeVersionName() {
const hash = (await git('rev-parse --short=12 HEAD')).trim();
const today = new Date();
return `${today.getFullYear()}-` +
`${pad(today.getMonth() + 1)}-${pad(today.getDate())}-${hash}`;
}
function setTimeoutAsync(timeout) {
return new Promise(resolve => {
setTimeout(() => { resolve(); }, timeout);
});
}