forked from dmrd/exponent-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.js
171 lines (153 loc) · 4.44 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
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
import spawnAsync from '@exponent/spawn-async';
import git from 'git-promise';
import rimraf from 'rimraf';
import CI, { Rocker, Kubernetes as K8S, Log, Github } from 'ci';
export default {
config: {
name: '📚 Docs',
shortname: 'docs',
description: 'Docs Build/Deploy',
branches: 'master',
allowPRs: true,
},
steps: (branch, tag, pr) => {
if (tag) {
// all we need to do when there's a tag is deploy
return [
deploy(branch, tag, pr),
CI.waitStep(),
updateSearchIndex(branch, tag, pr),
];
}
const steps = [
build(branch, tag, pr),
CI.waitStep(),
deploy(branch, tag, pr),
];
if (!pr) {
steps.push(CI.blockStep(':shipit: Deploy to Production?'), tagRelease);
}
return steps;
},
};
const build = (branch, tag, pr) => ({
name: `:hammer: Build`,
agents: {
queue: 'builder',
},
async command() {
const imageName = `gcr.io/exponentjs/exponent-docs-v2`;
const imageTag = `${process.env.BUILDKITE_COMMIT}`;
Log.collapsed(':hammer: Building Docs...');
// Added until caching issues between builds have been resolved in Gatsby.
// Without it, sometimes docs don't update as expected, eg: a section
// that appears locally won't exist on staging/production
rimraf.sync('./gatsby/.intermediate-representation');
rimraf.sync('./gatsby/public');
await Rocker.build({
rockerfile: './deploy/docker/deploy.Rockerfile',
context: '.',
vars: {
ImageName: imageName,
ImageTag: imageTag,
DocsVersion: `v${require('./package.json').version}`,
},
options: {
pull: true,
push: true,
},
});
},
});
const deploy = (branch, tag, pr) => ({
name: `:rocket: Deploy to ${tag && !pr ? 'Production' : pr ? 'Dev' : 'Staging'}`,
concurrency: 1,
concurrency_group: `docs/${tag && !pr ? 'prod' : pr ? `pr-${pr}` : 'staging'}/deploy`,
async command() {
if (!pr && branch !== 'master' && !tag) {
return;
}
const isProduction = tag && !pr;
let environment, ingressHostname;
if (isProduction) {
environment = 'production';
ingressHostname = 'docs.expo.io';
} else if (pr) {
environment = `docs-pr-${pr}`;
ingressHostname = `${environment}.pr.exp.host`;
} else {
environment = 'staging';
ingressHostname = 'staging.docs.expo.io';
}
const imageName = `gcr.io/exponentjs/exponent-docs-v2`;
const imageTag = `${process.env.BUILDKITE_COMMIT}`;
Log.collapsed(':gcloud: Deploy to K8s...');
await Github.performDeployment(
{
projectName: 'docs',
environment,
deploymentUrl: `https://${ingressHostname}`,
deploymentType: 'k8s',
prNumber: pr,
},
// deployment function
async () => {
await K8S.deployHelmChart({
clusterName: 'exp-central',
chartPath: './deploy/charts/docs',
namespace: environment,
releaseName: `docs-${environment}`,
values: {
image: {
repository: imageName,
tag: imageTag,
},
replicaCount: environment === 'production' ? 2 : 1, // PRS // Staging
ingress: [
{
host: ingressHostname,
},
],
},
});
}
);
},
});
const updateSearchIndex = (branch, tag, pr) => ({
name: `:feelsgood: Update Search Index`,
async command() {
if (branch !== 'master' && !tag) {
return;
}
Log.collapsed(':open_mouth: Updating search index...');
await spawnAsync(
'yarn',
['run', 'update-search-index', '--', 'docs.expo.io'],
{
stdio: 'inherit',
}
);
},
});
const tagRelease = {
name: ':git: Tag Release',
async command() {
Log.collapsed(':git: Tagging Release...'); // Build tag name
const tag = `docs/release-${await makeVersionName()}`;
await git(`tag ${tag}`);
Log.collapsed(':github: Pushing Release...');
await git(`push origin ${tag}`); // upload more steps
global.currentPipeline.upload(global.currentPipeline.steps(tag, tag, null));
},
};
function pad(n) {
return n < 10 ? `0${n}` : `${n}`;
}
async function makeVersionName() {
const hash = (await git(
`rev-parse --short=12 ${process.env.BUILDKITE_COMMIT}`
)).trim();
const today = new Date();
return `${today.getFullYear()}-${pad(today.getMonth() + 1)}-${pad(today.getDate())}-${hash}`;
}