diff --git a/.dockerignore b/.dockerignore index ac0b26d..4f1b51c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,10 @@ -vendor +.dockerignore .gitignore .git +.gitlab-ci.yml +.gitlab +example Dockerfile +exported +README.MD +release diff --git a/.gitignore b/.gitignore index 485dee6..4976690 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea +exported diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 03ac5f3..a49bb28 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,81 +1,21 @@ -variables: - GOCACHE: "${CI_PROJECT_DIR}/_go/cache" - DOCKER_HOST: tcp://docker:2375 - DOCKER_DRIVER: overlay2 - -before_script: - - mkdir -p ${CI_PROJECT_DIR}/_go/{pkg,bin,cache} - - rm -rf /go/pkg - - ln -s ${CI_PROJECT_DIR}/_go/pkg /go/pkg - - ln -s ${CI_PROJECT_DIR}/_go/bin /go/bin - -cache: - key: "$CI_COMMIT_REF_NAME" - paths: - - _go - untracked: true +include: +- template: 'Workflows/MergeRequest-Pipelines.gitlab-ci.yml' stages: - - deps - - test - - build - - package - - release +- build -deps: - stage: deps - image: golang:1.12 - script: - - go get -mod=readonly +variables: + DOCKER_HOST: tcp://docker:2375/ -test: - stage: test - dependencies: - - deps - image: golang:1.12 - script: - - go fmt $(go list ./...) - - go vet $(go list ./...) - - CGO_ENABLED=1 go test -mod=readonly -race $(go list ./...) -coverprofile .testCoverage.txt +image: buildtool/build-tools:${BUILDTOOLS_VERSION} build: stage: build - dependencies: - - deps - image: golang:1.12 - script: - - GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -mod=readonly -o release/default-request-adder -ldflags '-w -s' - artifacts: - paths: - - release/ - -package: - stage: package - dependencies: - - build - image: docker:stable services: - - docker:${DOCKER_DIND_VERSION} - before_script: - - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY + - docker:${DOCKER_DIND_VERSION} script: - - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA . - - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME - - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA - - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME - -release: - stage: release - dependencies: - - package - image: docker:stable - services: - - docker:${DOCKER_DIND_VERSION} - before_script: - - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY - script: - - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA - - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:latest - - docker push $CI_REGISTRY_IMAGE:latest - only: - - master \ No newline at end of file + - build + - curl -Os https://uploader.codecov.io/latest/linux/codecov + - chmod +x codecov + - ./codecov -t ${CODECOV_TOKEN} -R $CI_PROJECT_DIR -C $CI_COMMIT_SHA -r $CI_PROJECT_PATH + - push diff --git a/Dockerfile b/Dockerfile index 7de738f..8a1b209 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,25 @@ +FROM golang:1.20.0 as deps +WORKDIR /build +ADD go.* /build +RUN go mod download + +FROM deps as build +ENV CGO_ENABLED=0 +ADD . /build +RUN if [ $(go mod tidy -v 2>&1 | grep -c unused) != 0 ]; then echo "Unused modules, please run 'go mod tidy'"; exit 1; fi +RUN go fmt ./... +RUN go vet ./... +RUN CGO_ENABLED=1 go test -mod=readonly -race -coverprofile=coverage.txt.tmp -covermode=atomic -coverpkg=$(go list ./... | tr '\n' , | sed 's/,$//') ./... +RUN ["/bin/bash", "-c", "cat coverage.txt.tmp | grep -v -f <(find . -type f | xargs grep -l 'Code generated') > coverage.txt"] +RUN go tool cover -html=coverage.txt -o coverage.html +RUN go tool cover -func=coverage.txt +RUN rm coverage.txt.tmp +RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -mod=readonly -o release/default-request-adder -ldflags '-w -s' + +FROM scratch as export +COPY --from=build /build/coverage.txt / + FROM scratch -COPY release/default-request-adder / +COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=build /build/release/default-request-adder / CMD ["/default-request-adder"]