From 3888c91a6b0fb8cdd746f02b726210a8360e9ac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Tue, 6 Jun 2023 18:52:03 +0200 Subject: [PATCH] Switch to multi-stage Dockerbuild --- scnserver/Dockerfile | 24 ++++++++++++++++-------- scnserver/Makefile | 13 ++++++++----- scnserver/TODO.md | 2 ++ scnserver/dgi.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 scnserver/dgi.go diff --git a/scnserver/Dockerfile b/scnserver/Dockerfile index 1ffa44e..ac314e3 100644 --- a/scnserver/Dockerfile +++ b/scnserver/Dockerfile @@ -1,9 +1,19 @@ -FROM golang as builder -WORKDIR /repo -COPY . /repo -RUN make build +FROM golang:1-bullseye AS builder + +RUN apt-get update && \ + apt-get install -y ca-certificates openssl make git tar coreutils && \ + rm -rf /var/lib/apt/lists/* + +COPY . /buildsrc + +RUN cd /buildsrc && make build + + + + + FROM debian:bookworm @@ -13,9 +23,7 @@ RUN apt-get update && \ rm -rf /var/cache/apt/archives && \ rm -rf /var/lib/apt/lists -COPY --from=builder /repo/_build/scn_backend /app/scnserver - -COPY DOCKER_GIT_INFO /app/DOCKER_GIT_INFO +COPY --from=builder /buildsrc/_build/scn_backend /app/server RUN mkdir /data @@ -23,4 +31,4 @@ WORKDIR /app EXPOSE 80 -CMD ["/app/scnserver"] +CMD ["/app/server"] diff --git a/scnserver/Makefile b/scnserver/Makefile index 51e9533..5529fe4 100644 --- a/scnserver/Makefile +++ b/scnserver/Makefile @@ -21,12 +21,15 @@ gow: which gow || go install github.com/mitranim/gow@latest gow -e "go,mod,html,css,json,yaml,js" run -tags "timetzdata sqlite_fts5 sqlite_foreign_keys" blackforestbytes.com/simplecloudnotifier/cmd/scnserver -docker: +dgi: [ ! -f "DOCKER_GIT_INFO" ] || rm DOCKER_GIT_INFO - git rev-parse --abbrev-ref HEAD >> DOCKER_GIT_INFO - git rev-parse HEAD >> DOCKER_GIT_INFO - git log -1 --format=%cd --date=iso >> DOCKER_GIT_INFO - git config --get remote.origin.url >> DOCKER_GIT_INFO + echo -n "VCSTYPE=" >> DOCKER_GIT_INFO ; echo "git" >> DOCKER_GIT_INFO + echo -n "BRANCH=" >> DOCKER_GIT_INFO ; git rev-parse --abbrev-ref HEAD >> DOCKER_GIT_INFO + echo -n "HASH=" >> DOCKER_GIT_INFO ; git rev-parse HEAD >> DOCKER_GIT_INFO + echo -n "COMMITTIME=" >> DOCKER_GIT_INFO ; git log -1 --format=%cd --date=iso >> DOCKER_GIT_INFO + echo -n "REMOTE=" >> DOCKER_GIT_INFO ; git config --get remote.origin.url >> DOCKER_GIT_INFO + +build-docker: dgi docker build \ -t "$(DOCKER_NAME):$(HASH)" \ -t "$(DOCKER_NAME):$(NAMESPACE)-latest" \ diff --git a/scnserver/TODO.md b/scnserver/TODO.md index 6b4d82a..42292ee 100644 --- a/scnserver/TODO.md +++ b/scnserver/TODO.md @@ -49,6 +49,8 @@ - TODO-comments + - multistage build + #### PERSONAL - in my script: use `srvname` for sendername diff --git a/scnserver/dgi.go b/scnserver/dgi.go new file mode 100644 index 0000000..f73d6ae --- /dev/null +++ b/scnserver/dgi.go @@ -0,0 +1,42 @@ +package server + +import ( + _ "embed" + "fmt" + "gogs.mikescher.com/BlackForestBytes/goext/langext" + "strings" +) + +//go:embed DOCKER_GIT_INFO +var FileDockerGitInfo string + +var CommitHash *string +var VCSType *string +var CommitTime *string +var BranchName *string +var RemoteURL *string + +func init() { + for _, v := range strings.Split(FileDockerGitInfo, "\n") { + if v == "" { + continue + } else if strings.HasPrefix(v, "VCSTYPE=") { + VCSType = langext.Ptr(v[len("VCSTYPE="):]) + fmt.Printf("Found DGI Config: '%s' := '%s'\n", "VCSType", *VCSType) + } else if strings.HasPrefix(v, "BRANCH=") { + BranchName = langext.Ptr(v[len("BRANCH="):]) + fmt.Printf("Found DGI Config: '%s' := '%s'\n", "BranchName", *BranchName) + } else if strings.HasPrefix(v, "HASH=") { + CommitHash = langext.Ptr(v[len("HASH="):]) + fmt.Printf("Found DGI Config: '%s' := '%s'\n", "CommitHash", *CommitHash) + } else if strings.HasPrefix(v, "COMMITTIME=") { + CommitTime = langext.Ptr(v[len("COMMITTIME="):]) + fmt.Printf("Found DGI Config: '%s' := '%s'\n", "CommitTime", *CommitTime) + } else if strings.HasPrefix(v, "REMOTE=") { + RemoteURL = langext.Ptr(v[len("REMOTE="):]) + fmt.Printf("Found DGI Config: '%s' := '%s'\n", "RemoteURL", *RemoteURL) + } else { + fmt.Printf("[ERROR] Failed to parse DGI Config '%s'\n", v) + } + } +}