Skip to content

Commit 9882e39

Browse files
committed
Add velog-v2-netlify-completion-hook for deploy automation
1 parent c69a660 commit 9882e39

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

netlify-completion-hook/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# package directories
2+
node_modules
3+
jspm_packages
4+
5+
# Serverless directories
6+
.serverless

netlify-completion-hook/handler.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const axios = require('axios');
2+
module.exports.webhook = async event => {
3+
if (
4+
event.queryStringParameters &&
5+
event.queryStringParameters.key !== process.env.SSR_DEPLOY_TOKEN
6+
) {
7+
return {
8+
statusCode: 401,
9+
body: JSON.stringify({
10+
error: 'Invalid Key',
11+
}),
12+
};
13+
}
14+
15+
if (!event.body) {
16+
return {
17+
statusCode: 400,
18+
body: JSON.stringify({
19+
error: 'Missing branch name',
20+
}),
21+
};
22+
}
23+
24+
const parsedBody = JSON.parse(event.body);
25+
if (!['master'].includes(parsedBody.branch)) {
26+
return {
27+
statusCode: 403,
28+
body: JSON.stringify({
29+
error: 'This branch cannot be deployed',
30+
}),
31+
};
32+
}
33+
34+
try {
35+
await axios.post(
36+
'https://api.github.com/repos/velopert/velog-client/dispatches',
37+
{
38+
event_type: 'netlify-complete',
39+
},
40+
{
41+
headers: {
42+
Accept: 'application/vnd.github.everest-preview+json',
43+
Authorization: process.env.GITHUB_TOKEN,
44+
},
45+
},
46+
);
47+
48+
return {
49+
statusCode: 204,
50+
body: '',
51+
};
52+
} catch (e) {
53+
console.log(e);
54+
}
55+
};

netlify-completion-hook/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "netlify-completion-hook",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"axios": "^0.19.1"
8+
}
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
service: velog-v2-netlify-completion-hook
2+
3+
provider:
4+
name: aws
5+
runtime: nodejs12.x
6+
region: ap-northeast-2
7+
stage: prod
8+
9+
environment:
10+
GITHUB_TOKEN: ${ssm:/velog-v2/github-token}
11+
SSR_DEPLOY_TOKEN: ${ssm:/velog-v2/ssr-deploy-token}
12+
13+
functions:
14+
webhook:
15+
handler: handler.webhook
16+
events:
17+
- http:
18+
path: /
19+
method: POST

netlify-completion-hook/yarn.lock

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
axios@^0.19.1:
6+
version "0.19.1"
7+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.1.tgz#8a6a04eed23dfe72747e1dd43c604b8f1677b5aa"
8+
integrity sha512-Yl+7nfreYKaLRvAvjNPkvfjnQHJM1yLBY3zhqAwcJSwR/6ETkanUgylgtIvkvz0xJ+p/vZuNw8X7Hnb7Whsbpw==
9+
dependencies:
10+
follow-redirects "1.5.10"
11+
12+
debug@=3.1.0:
13+
version "3.1.0"
14+
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
15+
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
16+
dependencies:
17+
ms "2.0.0"
18+
19+
20+
version "1.5.10"
21+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
22+
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==
23+
dependencies:
24+
debug "=3.1.0"
25+
26+
27+
version "2.0.0"
28+
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
29+
integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=

0 commit comments

Comments
 (0)