From f430e30c024812508c71e2c35359ed20393b7223 Mon Sep 17 00:00:00 2001 From: Marc Ole Bulling Date: Fri, 19 Mar 2021 00:10:56 +0100 Subject: [PATCH] Enhanced Docker build / release build --- .../workflows/docker-publish-multiarch-dev.yml | 2 +- BuildVars.go | 6 ++++++ Configuration.go | 2 +- Dockerfile | 6 ++++-- HelperFunctions.go | 6 +----- Main.go | 15 ++++++++++++++- release/Dockerfile | 4 ++++ release/entrypoint.sh | 2 +- release/go.mod | 8 ++++++++ 9 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 BuildVars.go create mode 100644 release/go.mod diff --git a/.github/workflows/docker-publish-multiarch-dev.yml b/.github/workflows/docker-publish-multiarch-dev.yml index 819db6a..9a841f4 100644 --- a/.github/workflows/docker-publish-multiarch-dev.yml +++ b/.github/workflows/docker-publish-multiarch-dev.yml @@ -1,4 +1,4 @@ -name: Docker Publish Release Multiarch +name: Docker Publish Dev Multiarch on: workflow_dispatch: diff --git a/BuildVars.go b/BuildVars.go new file mode 100644 index 0000000..b9978b3 --- /dev/null +++ b/BuildVars.go @@ -0,0 +1,6 @@ +package main + +// compiler sets this to true if compiled with the Docker image +var IS_DOCKER = "false" +var BUILD_TIME = "Dev Build" +var BUILDER = "Manual Build" diff --git a/Configuration.go b/Configuration.go index cad150f..80f2af1 100644 --- a/Configuration.go +++ b/Configuration.go @@ -112,7 +112,7 @@ func askForUsername() string { } func askForLocalOnly() bool { - if isDocker() { + if IS_DOCKER != "false" { return false } fmt.Print("Bind port to localhost only? [Y/n]: ") diff --git a/Dockerfile b/Dockerfile index 82b8abc..9bdcaa4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,15 +5,17 @@ FROM golang:1.16 AS build_base ## docker run -it -v gokapi-data:/app/data -v gokapi-config:/app/config -p 127.0.0.1:53842:53842 gokapi RUN mkdir /compile +COPY go.mod /compile +RUN cd /compile && go mod download COPY . /compile -RUN cd /compile && CGO_ENABLED=0 go build -o /compile/gokapi +RUN cd /compile && CGO_ENABLED=0 go build -ldflags="-s -w -X 'main.IS_DOCKER=true' -X 'main.BUILDER=Project Docker File' -X 'main.BUILD_TIME=$(date)'" -o /compile/gokapi FROM alpine:3.13 -RUN apk add ca-certificates && mkdir /app && touch /app/.isdocker +RUN apk add ca-certificates && mkdir /app && touch /app/.isdocker COPY --from=build_base /compile/gokapi /app/gokapi WORKDIR /app diff --git a/HelperFunctions.go b/HelperFunctions.go index 7b9fee2..36f7397 100644 --- a/HelperFunctions.go +++ b/HelperFunctions.go @@ -111,8 +111,4 @@ func readLine() string { reader := bufio.NewReader(os.Stdin) text, _ := reader.ReadString('\n') return strings.Replace(text, "\n", "", -1) -} - -func isDocker() bool { - return fileExists(".isdocker") -} +} \ No newline at end of file diff --git a/Main.go b/Main.go index c901f7b..a8fe52a 100644 --- a/Main.go +++ b/Main.go @@ -11,6 +11,7 @@ import ( const VERSION = "1.1.0" func main() { + checkPrimaryArguments() rand.Seed(time.Now().UnixNano()) fmt.Println("Gokapi v" + VERSION + " starting") createDataDir() @@ -21,11 +22,23 @@ func main() { startWebserver() } +func checkPrimaryArguments() { + if len(os.Args) > 1 { + if os.Args[1] == "--version" || os.Args[1] == "-v" { + fmt.Println("Gokapi v" + VERSION) + fmt.Println("Builder: " + BUILDER) + fmt.Println("Build Date: " + BUILD_TIME) + fmt.Println("Docker Version: " + IS_DOCKER) + os.Exit(0) + } + } +} + func checkArguments() { if len(os.Args) > 1 { if os.Args[1] == "--reset-pw" { fmt.Println("Password change requested") - globalConfig.AdminPassword = hashPassword(askForPassword(),SALT_PW_ADMIN) + globalConfig.AdminPassword = hashPassword(askForPassword(), SALT_PW_ADMIN) saveConfig() } } diff --git a/release/Dockerfile b/release/Dockerfile index 10f2ecc..a37fa52 100644 --- a/release/Dockerfile +++ b/release/Dockerfile @@ -11,6 +11,10 @@ RUN \ update-ca-certificates && \ rm -rf /var/lib/apt +COPY go.mod /tmp/tmp/go.mod + +RUN cd /tmp/tmp/ && go mod download && rm -r /tmp/tmp + COPY entrypoint.sh /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] diff --git a/release/entrypoint.sh b/release/entrypoint.sh index 38a2a17..e54a17e 100755 --- a/release/entrypoint.sh +++ b/release/entrypoint.sh @@ -16,7 +16,7 @@ for target in $targets; do fi echo "----> Building project for: $target" - GOOS=$os GOARCH=$arch CGO_ENABLED=0 go build -o $output + GOOS=$os GOARCH=$arch CGO_ENABLED=0 go build -ldflags="-s -w -X 'main.BUILDER=Github Release Builder' -X 'main.BUILD_TIME=$(date)'" -o $output zip -j $output.zip $output > /dev/null rm $output done diff --git a/release/go.mod b/release/go.mod new file mode 100644 index 0000000..4ddcae5 --- /dev/null +++ b/release/go.mod @@ -0,0 +1,8 @@ +module SingleDownload + +go 1.16 + +require ( + golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 + golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect +)