From c577734ab136c0945a28b2294d11ff09204513ab Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Sat, 16 Apr 2022 18:57:59 +0200 Subject: [PATCH] [test] Multistage build with drone CI --- .drone.yml | 86 ++++++++++++++++++++++++++++++++++------------ Dockerfile | 11 ------ Dockerfile.build | 22 ++++++++++++ Dockerfile.run | 11 ++++++ vegvisir.json.test | 19 ++++++++++ 5 files changed, 116 insertions(+), 33 deletions(-) delete mode 100644 Dockerfile create mode 100644 Dockerfile.build create mode 100644 Dockerfile.run create mode 100644 vegvisir.json.test diff --git a/.drone.yml b/.drone.yml index e84bf25..bce2db0 100644 --- a/.drone.yml +++ b/.drone.yml @@ -3,32 +3,74 @@ type: docker name: default steps: -- name: static_check - image: golang:1.18 - commands: - - go install honnef.co/go/tools/cmd/staticcheck@latest - - staticcheck ./pkg/... - volumes: - - name: gopath - path: /go +# - name: static_check +# image: golang:1.18 +# commands: +# - go install honnef.co/go/tools/cmd/staticcheck@latest +# - staticcheck ./pkg/... +# volumes: +# - name: gopath +# path: /go -- name: lint - image: golang:1.18 - commands: - - go install golang.org/x/lint/golint@latest - - golint -set_exit_status ./pkg/... - volumes: - - name: gopath - path: /go +# - name: lint +# image: golang:1.18 +# commands: +# - go install golang.org/x/lint/golint@latest +# - golint -set_exit_status ./pkg/... +# volumes: +# - name: gopath +# path: /go -- name: vet - image: golang:1.18 +# - name: vet +# image: golang:1.18 +# commands: +# - go vet ./pkg/... +# volumes: +# - name: gopath +# path: /go + +- name: build_image + image: plugins/docker commands: - - go vet ./pkg/... + - branch=$(echo $CI_COMMIT_BRANCH | grep -v '/') || echo $CI_COMMIT_BRANCH + - p1=$(echo $CI_COMMIT_BRANCH | cut -d '/' -f1 -s) + - p2=$(echo $CI_COMMIT_BRANCH | cut -d '/' -f2 -s) + - tag=${branch:-$p1-$p2} + - echo $tag + - docker build -t git.pbiernat.dev/golang/vegvisir:$tag -f Dockerfile.build . volumes: - - name: gopath - path: /go + - name: docker-sock + path: /var/run + +- name: push_image + image: plugins/docker + environment: + DOCKER_USERNAME: + from_secret: registry_username + DOCKER_PASSWORD: + from_secret: registry_password + commands: + - branch=$(echo $CI_COMMIT_BRANCH | grep '/') || echo $CI_COMMIT_BRANCH + - p1=$(echo $CI_COMMIT_BRANCH | cut -d '/' -f1 -s) + - p2=$(echo $CI_COMMIT_BRANCH | cut -d '/' -f2 -s) + - tag=${branch:=$p1-$p2} + - echo $tag + - echo $DOCKER_PASSWORD | docker login git.pbiernat.dev -u $DOCKER_USERNAME --password-stdin && + - docker push git.pbiernat.dev/golang/vegvisir:$tag + volumes: + - name: docker-sock + path: /var/run + +services: +- name: docker + image: docker:dind + privileged: true + volumes: + - name: docker-sock + path: /var/run volumes: - name: gopath - temp: {} \ No newline at end of file + temp: {} +- name: docker-sock + temp: {} diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 2004bdc..0000000 --- a/Dockerfile +++ /dev/null @@ -1,11 +0,0 @@ -FROM golang:1.16-alpine - -RUN mkdir /go/src/vegvisir -WORKDIR /go/src/vegvisir - -COPY vegvisir.json go.mod go.sum ./ -COPY pkg/ ./pkg - -RUN go mod download - -CMD ["go", "run", "pkg/main.go"] \ No newline at end of file diff --git a/Dockerfile.build b/Dockerfile.build new file mode 100644 index 0000000..e6986a7 --- /dev/null +++ b/Dockerfile.build @@ -0,0 +1,22 @@ +# Builder +FROM golang:alpine AS builder + +WORKDIR /go/src/app + +ARG MAIN_GO=pkg/main.go +# ARG HTTP_PORT=8080 +# ENV HTTP_PORT {$HTTP_PORT} + +COPY go.mod go.sum ./ +COPY pkg ./pkg + +RUN go mod download && \ + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/app $MAIN_GO + +# Destination image +FROM gcr.io/distroless/base-debian10 + +COPY --from=builder /go/bin/app /app + +EXPOSE $HTTP_PORT +ENTRYPOINT ["/app"] diff --git a/Dockerfile.run b/Dockerfile.run new file mode 100644 index 0000000..6a0a8e9 --- /dev/null +++ b/Dockerfile.run @@ -0,0 +1,11 @@ +FROM golang:alpine + +# RUN mkdir /go/src/app +WORKDIR /go/src/app + +COPY go.mod go.sum ./ +COPY pkg ./ + +RUN go mod download + +ENTRYPOINT ["go", "run", "pkg/main.go"] diff --git a/vegvisir.json.test b/vegvisir.json.test new file mode 100644 index 0000000..cde77fe --- /dev/null +++ b/vegvisir.json.test @@ -0,0 +1,19 @@ +{ + "server": { + "address": "127.0.0.1", + "port": 8080 + }, + "backends": { + "dummy-app": { + "prefixURL": "/", + "backendAddress": "http://172.17.0.1:80", + "routes": [{ + "pattern": "(.+)", + "target": "$1" + }] + } + }, + "cache": { + "type": "memory" + } +} \ No newline at end of file