diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..2f673bb --- /dev/null +++ b/.drone.yml @@ -0,0 +1,72 @@ +kind: pipeline +type: docker +name: default + +steps: +- name: go_static_check + image: golang:latest + commands: + - go install honnef.co/go/tools/cmd/staticcheck@latest + - staticcheck ./internal/... + volumes: + - name: gopath + path: /go + +- name: go_lint + image: golang:latest + commands: + - go install golang.org/x/lint/golint@latest + - golint ./internal/... + volumes: + - name: gopath + path: /go + +- name: go_vet + image: golang:latest + commands: + - go vet ./internal/... + volumes: + - name: gopath + path: /go + +- name: build_image + image: plugins/docker + commands: + - sleep 5 + - ./deploy/docker/build_image.sh + depends_on: + - go_static_check + - go_lint + - go_vet + 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: + - ./deploy/docker/publish_image.sh + depends_on: + - build_image + 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: {} +- name: docker-sock + temp: {} diff --git a/deploy/docker/Dockerfile.build b/deploy/docker/Dockerfile.build new file mode 100644 index 0000000..8c7cd0e --- /dev/null +++ b/deploy/docker/Dockerfile.build @@ -0,0 +1,26 @@ +# Builder +FROM golang:alpine AS builder + +WORKDIR /go/src/app + +ARG MAIN_GO=cmd/server/main.go + +ARG SERVER_PORT=8080 +ENV SERVER_PORT {$SERVER_PORT} + +COPY go.mod go.sum ./ +COPY internal ./internal +COPY cmd ./cmd + +RUN ls -lh + +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 $SERVER_PORT +ENTRYPOINT ["/app"] diff --git a/deploy/docker/Dockerfile.run b/deploy/docker/Dockerfile.run new file mode 100644 index 0000000..6a0a8e9 --- /dev/null +++ b/deploy/docker/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/deploy/docker/build_image.sh b/deploy/docker/build_image.sh new file mode 100755 index 0000000..0131ac2 --- /dev/null +++ b/deploy/docker/build_image.sh @@ -0,0 +1,11 @@ +#!/bin/sh +set -evx + +branch=${DRONE_TAG:=$CI_COMMIT_BRANCH} +branch=$(echo $branch | grep -v /) || echo $branch ; +p1=$(echo $branch | cut -d / -f1 -s) && +p2=$(echo $branch | cut -d / -f2 -s) && +tag=${branch:=$p1-$p2} && +echo "Building" $tag + +docker build -t git.pbiernat.dev/golang/rest-api-prototype:$tag -f deploy/docker/Dockerfile.build . diff --git a/deploy/docker/publish_image.sh b/deploy/docker/publish_image.sh new file mode 100755 index 0000000..8a0484b --- /dev/null +++ b/deploy/docker/publish_image.sh @@ -0,0 +1,12 @@ +#!/bin/sh +set -evx + +branch=${DRONE_TAG:=$CI_COMMIT_BRANCH} +branch=$(echo $branch | grep -v /) || echo $branch ; +p1=$(echo $branch | cut -d / -f1 -s) && +p2=$(echo $branch | cut -d / -f2 -s) && +tag=${branch:=$p1-$p2} && +echo "Publishing" $tag + +echo $DOCKER_PASSWORD | docker login git.pbiernat.dev -u $DOCKER_USERNAME --password-stdin && +docker push git.pbiernat.dev/golang/rest-api-prototype:$tag