From 1cbf8e8d4f77ffd7499395d08bc9240295ca1e49 Mon Sep 17 00:00:00 2001 From: zhaoyingbo Date: Sun, 10 Jul 2022 13:04:46 +0800 Subject: [PATCH] update: push to dockerhub --- Dockerfile | 36 ++++++++++++++++++++++++++++++++++++ docker-compose.yml | 15 +++++++++++++++ go.mod | 2 +- main.go | 25 +++++++++++++++++++++---- 4 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b3d6738 --- /dev/null +++ b/Dockerfile @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..0cfee21 --- /dev/null +++ b/docker-compose.yml @@ -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 \ No newline at end of file diff --git a/go.mod b/go.mod index 3fabd75..becc395 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module git.lolli.tech/lollipopkit/gogs-webhook +module git.lacus.site/RainSun/gogs_auto_deploy go 1.18 diff --git a/main.go b/main.go index fd5b513..f371cd4 100644 --- a/main.go +++ b/main.go @@ -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 := ""