前几天写过一个使用gitstats 统计分析代码的,但是那个因为开发的问题,对于直接和容器集成是有问题的,统计需要进入容器执行
命令,对于自动构建的还不是很方便,所以使用了git_stats
环境准备
- docker-compose 文件
version: "3"
services:
hooks:
build: ./
image: dalongrong/webhook-docker-client
command: ["-verbose", "-hooks=/etc/webhook/hooks.json","-hotreload"]
tty: true
volumes:
- "./conf/hooks.json:/etc/webhook/hooks.json"
- "./app.sh:/app.sh"
- "/var/run/docker.sock:/var/run/docker.sock"
- "./result:/gitstats"
- "./gitcode:/git"
ports:
- "9090:9000"
privileged: true
web:
image: nginx
ports:
- "8080:80"
volumes:
- "./result:/usr/share/nginx/html"
- webhook 配置
[
{
"id": "webhook",
"execute-command": "/app.sh",
"command-working-directory": "/",
"include-command-output-in-response":true,
"trigger-rule":
{
"and":
[
{
"match":
{
"type": "value",
"value": "dalong",
"parameter":
{
"source": "payload",
"name": "username"
}
}
}
]
}
}
]
- webhook 执行代码 app.sh
#!/bin/sh
HOSTPATH=/opt/rong/webhook-docker-in-docker
git clone https://github.com/wurstmeister/kafka-docker.git /git
docker info
docker run --rm -v $HOSTPATH/gitcode:/git:ro -v $HOSTPATH/result:/gitstats goodideal/docker-git-stats
- dockerfile
FROM almir/webhook
RUN apk update && apk add docker && apk add git
原理说明
主要是通过数据卷的模式进行git 代码挂载,同时通过nginx 提供静态web 站点的访问,对于统计的构建,通过webhook触发
目前写的比较简单,实际上,我们可以进行基于git pull 模式的统计
运行&&测试
- 启动
docker-compose up -d
- 触发统计
curl -X POST \
http://localhost:9090/hooks/webhook \
-H 'Content-Type: application/json' \
-H 'Postman-Token: ba2d2fbf-62ad-4eda-b9a9-84091e19bdfb' \
-H 'cache-control: no-cache' \
-d '{
"username":"dalong"
}'
- 效果
参考资料
https://github.com/goodideal/git_stats
https://github.com/rongfengliang/webhook-docker-in-docker