Skip to content

Instantly share code, notes, and snippets.

@Enity
Last active August 2, 2019 16:15
Show Gist options
  • Save Enity/ee05c633675d64dbb5280bcb8ac3cd6e to your computer and use it in GitHub Desktop.
Save Enity/ee05c633675d64dbb5280bcb8ac3cd6e to your computer and use it in GitHub Desktop.
Golang microservice gitlab-ci
.git
.dockerignore
LICENSE
README.md
.gitignore
Dockerfile
docker-compose.yml
.DS_Store
/target
.vscode
/.gopath-cache/**
!/.gopath-cache/pkg/**
variables:
CONTAINER_NAME: $CI_REGISTRY_IMAGE
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- .gopath-cache
stages:
- build
- lint-test
lint-test:
image: golang:1.12.7
stage: lint-test
except:
- master
before_script:
- mkdir -p .gopath-cache
- export GOPATH="$CI_PROJECT_DIR/.gopath-cache"
- export PATH="$PATH:$GOPATH/bin"
- go mod download
script:
- GO111MODULE=off go get github.com/golangci/golangci-lint/cmd/golangci-lint
- make lint
- make test
build-docker:
image: docker:stable
services:
- docker:dind
stage: build
when: manual
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker pull $CONTAINER_NAME:latest || true
- docker build --cache-from $CONTAINER_NAME:latest -t $CONTAINER_NAME:latest .
- docker push $CONTAINER_NAME:latest
FROM golang:1.12.7-alpine as builder
WORKDIR /app
COPY . .
# for ci cache
ENV GOPATH=/app/.gopath-cache
# golang-alpine dont includes git lol
RUN apk add --no-cache git
RUN CGO_ENABLED=0 GOOS=linux go build -o main .
############################
FROM scratch
WORKDIR /app
COPY --from=builder /app/main ./main
CMD ["./main"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment