From 6bc22c2b6b21470b10d39f6bbd7a4ae236269988 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 | 38 +++++++++++++++++++++++++++++++++++++- Dockerfile | 11 ----------- Dockerfile.build | 22 ++++++++++++++++++++++ Dockerfile.run | 11 +++++++++++ vegvisir.json.test | 19 +++++++++++++++++++ 5 files changed, 89 insertions(+), 12 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..854013e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -28,7 +28,43 @@ steps: volumes: - name: gopath path: /go + +- name: build_image + image: plugins/docker + commands: + - p1=$(echo $CI_COMMIT_BRANCH | cut -d '/' -f1) && + p2=$(echo $CI_COMMIT_BRANCH | cut -d '/' -f2) && + docker build -t git.pbiernat.dev/golang/vegvisir:$p1-$p2 -f Dockerfile.build . + volumes: + - 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: + - p1=$(echo $CI_COMMIT_BRANCH | cut -d '/' -f1) && + p2=$(echo $CI_COMMIT_BRANCH | cut -d '/' -f2) && + docker login git.pbiernat.dev -u $DOCKER_USERNAME -p $DOCKER_PASSWORD && + docker push git.pbiernat.dev/golang/vegvisir:$p1-$p2 + 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