-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (56 loc) · 1.54 KB
/
index.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
const fs = require('fs');
const TEMPLATE = `/*
Basic-Auth: AUTH_USER:AUTH_PASS
`;
const CONTEXT_DEPLOY_PREVIEW = 'deploy-preview';
const CONTEXT_BRANCH_DEPLOY = 'branch-deploy';
const CONTEXT_PRODUCTION = 'production';
module.exports = {
onPostBuild({ inputs }) {
const {
branches: _branches,
pullRequest = true,
userEnvKey = 'AUTH_USER',
passEnvKey = 'AUTH_PASS',
} = inputs;
const user = process.env[userEnvKey];
const pass = process.env[passEnvKey];
if (!user || !pass) {
console.log('Username and/or password not found. Skip.');
return;
}
if (process.env.CONTEXT === CONTEXT_PRODUCTION) {
console.log('Production build. Skip.');
return;
}
if (
process.env.CONTEXT === CONTEXT_DEPLOY_PREVIEW &&
(!process.env.PULL_REQUEST || !pullRequest)
) {
console.log(
`Disabled for ${process.env.CONTEXT_DEPLOY_PREVIEW}. Do nothing.`
);
return;
}
const branches = _branches.split(',');
if (
process.env.CONTEXT === CONTEXT_BRANCH_DEPLOY &&
branches.length &&
!branches.includes(process.env.BRANCH)
) {
console.log(
`The ${process.env.BRANCH} is not in the list of branches to be secured. Do nothing.`
);
return;
}
console.log('Securing this deployment.');
fs.appendFileSync(
`${process.cwd()}/public/_headers`,
TEMPLATE.replace('AUTH_USER', process.env.AUTH_USER).replace(
'AUTH_PASS',
process.env.AUTH_PASS
),
'utf-8'
);
},
};