update: push to dockerhub

This commit is contained in:
zhaoyingbo 2022-07-10 13:04:46 +08:00
parent e96237b694
commit 1cbf8e8d4f
4 changed files with 73 additions and 5 deletions

36
Dockerfile Normal file
View File

@ -0,0 +1,36 @@
FROM golang:1.18.2
WORKDIR /gogs_auto_deploy
COPY . .
RUN go build
FROM python:3.7
LABEL maintainer="zhaoyingbo@live.cn"
EXPOSE 3001
WORKDIR /gogs_auto_deploy
ENV GOGS_REPOSITORY '/gogs_auto_deploy/repository/'
HEALTHCHECK --interval=5s --timeout=3s \
CMD curl -fs http://0.0.0.0:3001/health || exit 1
RUN echo "[]" > config.json
RUN mkdir .log
RUN mkdir repository
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list \
&& sed -i 's/security.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list \
&& apt-get update && apt-get install -y --no-install-recommends procps wget vim curl \
&& apt-get install -y --no-install-recommends libsm6 libxrender1 libxext-dev libglib2.0-dev \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY --from=0 /gogs_auto_deploy/gogs_auto_deploy gogs_auto_deploy
CMD /bin/bash -c /gogs_auto_deploy/gogs_auto_deploy

15
docker-compose.yml Normal file
View File

@ -0,0 +1,15 @@
version: '3.3'
services:
gogs_auto_deploy:
image: zhaoyingbo/gogs_auto_deploy:1.0.0
container_name: gogs_auto_deploy
environment:
- TZ=Asia/Shanghai # 时区
ports:
- "3001:3001"
volumes:
- ~/.ssh:/root/.ssh
- /usr/bin/docker:/usr/bin/docker
- /var/run/docker.sock:/var/run/docker.sock
user: root
restart: always

2
go.mod
View File

@ -1,3 +1,3 @@
module git.lolli.tech/lollipopkit/gogs-webhook
module git.lacus.site/RainSun/gogs_auto_deploy
go 1.18

25
main.go
View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
@ -17,6 +18,7 @@ import (
const (
path = "/"
healthCheckPath = "/health"
logDir = ".log/"
perm = 0770
)
@ -24,7 +26,12 @@ const (
var (
configs = []Config{}
configsLock = &sync.RWMutex{}
home = os.Getenv("HOME") + "/"
repositoryPath = func () string {
if os.Getenv("GOGS_REPOSITORY") != "" {
return os.Getenv("GOGS_REPOSITORY")
}
return os.Getenv("HOME") + "/repository/"
}()
logFile *os.File
)
@ -39,7 +46,12 @@ func init() {
if err != nil {
panic(err)
}
log.SetOutput(logFile)
if os.Getenv("GOGS_REPOSITORY") != "" {
mw := io.MultiWriter(os.Stdout,logFile)
log.SetOutput(mw)
} else {
log.SetOutput(logFile)
}
time.Sleep(time.Minute)
}
}()
@ -141,7 +153,7 @@ func checkNeedDeploy(payload GogsPayload) (Config, bool) {
// 我们这个系统简单点只要本次提交包含了部署信号就直接部署最后一个commit
config := Config{
Repo: "",
Path: home,
Path: repositoryPath,
Script: "deploy.py",
Signal: "{D}",
Branch: "",
@ -189,6 +201,11 @@ func main() {
addr := flag.String("a", ":3001", "Address to listen on")
flag.Parse()
http.HandleFunc(healthCheckPath, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("I'm alive"))
return
})
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
// 获取请求体
reqBody, err := ioutil.ReadAll(r.Body)
@ -230,7 +247,7 @@ func main() {
if hasPrefix {
base = deployConfig.Path
} else {
base = home + deployConfig.Path
base = repositoryPath + deployConfig.Path
}
repoFmt := ""