Skip to content

Commit 742bb8f

Browse files
author
pugang
committed
first init
0 parents  commit 742bb8f

File tree

7 files changed

+136
-0
lines changed

7 files changed

+136
-0
lines changed

Dockerfile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
FROM nginx
2+
3+
LABEL maintainer="Gary"
4+
ARG LOCALE="en"
5+
COPY webhook.js /app/
6+
COPY entrypoint.sh /
7+
COPY hexo.conf /etc/nginx/conf.d
8+
RUN set -x \
9+
&& if [ "$LOCALE" = "cn" ]; then \
10+
sed -i 's/deb.debian.org/mirrors.163.com/g' /etc/apt/sources.list; \
11+
sed -i 's/security.debian.org/mirrors.163.com\/debian-security\//g' /etc/apt/sources.list; \
12+
fi; \
13+
apt-get update \
14+
&& apt-get install --no-install-recommends --no-install-suggests -y curl \
15+
ca-certificates \
16+
xz-utils \
17+
git \
18+
&& if [ "$LOCALE" = "cn" ]; then \
19+
curl -SLO https://npm.taobao.org/mirrors/node/latest-v8.x/node-v8.9.1-linux-x64.tar.xz; \
20+
else \
21+
curl -SLO https://nodejs.org/dist/v8.9.1/node-v8.9.1-linux-x64.tar.xz; \
22+
fi; \
23+
tar -xJf node-v8.9.1-linux-x64.tar.xz -C /usr/local --strip-components=1 --no-same-owner \
24+
&& ln -s /usr/local/bin/node /usr/local/bin/nodejs \
25+
&& if [ "$LOCALE" = "cn" ]; then \
26+
npm config set registry https://registry.npm.taobao.org; \
27+
fi; \
28+
npm install -g hexo-cli pm2 \
29+
&& apt-get purge -y --auto-remove \
30+
&& rm -rf /var/lib/apt/lists/*
31+
32+
WORKDIR /app
33+
ENTRYPOINT ["/entrypoint.sh"]
34+
EXPOSE 80

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Nginx-Hexo
2+
3+
Nginx 做前端代理,使用node server监听Github的webhook。只要Github有push便会拉取最新代码并生成hexo页面。
4+
5+
## 如何使用
6+
7+
```
8+
9+
docker run --name hexo \
10+
-v /tmp/hexo:/app/hexo \
11+
-p 80:80 \
12+
-e HOST=#{Your domain} \
13+
-e GITREPO=#{Your github repo} \
14+
-d nginx-hexo
15+
```
16+

build-cn.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker build ./ --build-arg LOCALE=cn -t nginx-hexo

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker build ./ --build-arg LOCALE=cn -t nginx-hexo

entrypoint.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -x
3+
4+
[ -z $HOST ] && HOST=$(hostname -i)
5+
sed -i "s/IP_OR_HOST/$HOST/" /etc/nginx/conf.d/hexo.conf
6+
pm2 start webhook.js --name webhook
7+
8+
[ ! -d "/app/hexo" ] \
9+
&& git clone $GITREPO /app/hexo/ \
10+
&& cd /app/hexo \
11+
&& npm install
12+
hexo g
13+
nginx -g 'daemon off;'

hexo.conf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
server {
2+
listen 80;
3+
server_name IP_OR_HOST;
4+
5+
root /app/hexo/public;
6+
index index.html;
7+
8+
error_log /var/log/nginx/error.log info;
9+
access_log /var/log/nginx/access.log;
10+
11+
location / {
12+
try_files $uri $uri/ =404;
13+
}
14+
15+
location /webhook {
16+
proxy_pass http://127.0.0.1:5000;
17+
}
18+
19+
location ~ /\. {
20+
log_not_found off;
21+
deny all;
22+
}
23+
}

webhook.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var url = require("url")
2+
var http = require('http')
3+
const { exec } = require("child_process")
4+
5+
function onRequest(request,response){
6+
route(request,response);
7+
}
8+
9+
function route(request,response){
10+
pathname = url.parse(request.url).pathname;
11+
if (typeof router_handler[pathname] === "function"){
12+
router_handler[pathname](request,response);
13+
}else{
14+
response.write("404");
15+
}
16+
response.end();
17+
}
18+
19+
var router_handler = {}
20+
router_handler["/"] = function(request,response){
21+
response.write("hello");
22+
}
23+
24+
router_handler['/webhook'] = function(request,response) {
25+
let event = request.headers["x-github-event"];
26+
let data = {status: "ok"};
27+
if (event == "push") {
28+
request.on('data',(chunk) => {
29+
let payload = JSON.parse(chunk);
30+
exec(`cd /app/hexo && git pull origin master && hexo g`,(err,stdout,stderr) => {
31+
if (err) {
32+
console.error(`exec error: ${err}`);
33+
return;
34+
}
35+
console.log(`stdout: ${stdout}`);
36+
console.log(`stderr: ${stderr}`);
37+
});
38+
// data = {status: payload.head_commit.id};
39+
console.log(`commit id is ${payload.head_commit.id}`)
40+
});
41+
}
42+
response.setHeader('Content-Type', 'application/json')
43+
response.write(JSON.stringify(data));
44+
}
45+
46+
47+
http.createServer(onRequest).listen(5000);
48+
console.log("server listent at %d",5000);

0 commit comments

Comments
 (0)