From a505ef3c3f40b011f07c27d9668b7d80e3a20cbc Mon Sep 17 00:00:00 2001 From: genofire Date: Thu, 9 Jun 2022 20:50:34 +0200 Subject: [PATCH] feat(ci): build docker image --- .ci/check-gofmt | 9 +++++++ .ci/check-testfiles | 26 ++++++++++++++++++++ .gitlab-ci.yml | 58 +++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 21 ++++++++++++++++ api/client_test.go | 1 + 5 files changed, 115 insertions(+) create mode 100755 .ci/check-gofmt create mode 100755 .ci/check-testfiles create mode 100644 .gitlab-ci.yml create mode 100644 Dockerfile create mode 100644 api/client_test.go diff --git a/.ci/check-gofmt b/.ci/check-gofmt new file mode 100755 index 0000000..df9d8eb --- /dev/null +++ b/.ci/check-gofmt @@ -0,0 +1,9 @@ +#!/bin/bash + +result="$(gofmt -s -l . | grep -v '^vendor/' )" +if [ -n "$result" ]; then + echo "Go code is not formatted, run 'gofmt -s -w .'" >&2 + echo "$result" + exit 1 +fi + diff --git a/.ci/check-testfiles b/.ci/check-testfiles new file mode 100755 index 0000000..7c2b5fa --- /dev/null +++ b/.ci/check-testfiles @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +# checks if every desired package has test files + +import os +import re +import sys + +source_re = re.compile(".*\.go") +test_re = re.compile(".*_test\.go") +missing = False + +for root, dirs, files in os.walk("."): + # ignore some paths + if root == "." or root.startswith("./vendor") or root.startswith("./.") or root.startswith("./docs"): + continue + + # source files but not test files? + if len([f for f in files if source_re.match(f)]) > 0 and len([f for f in files if test_re.match(f)]) == 0: + print("no test files for {}".format(root)) + missing = True + +if missing: + sys.exit(1) +else: + print("every package has test files") + diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..98131a1 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,58 @@ +image: "golang:latest" + +variables: + DOCKER_IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG + # Tell 'docker:dind' to enable TLS (recommended) + # and generate certificates in the specified directory. + DOCKER_TLS_CERTDIR: "/certs" + +stages: + - test + - build + +test-lint: + stage: test + script: + - go install github.com/client9/misspell/cmd/misspell@latest + - find . -type f -not -path "./webroot/assets" | grep -v "models/.*_testdata.*.go" | xargs misspell -error + - ./.ci/check-gofmt + - ./.ci/check-testfiles + +build-linux: + stage: build + except: + - tags + - master + - main + script: + # build app with version + - go install + - mv "/go/bin/$CI_PROJECT_NAME" "$CI_PROJECT_DIR/$CI_PROJECT_NAME" + artifacts: + paths: + - config_example.toml + - "$CI_PROJECT_NAME" + +build-docker: + stage: build + image: docker:latest + services: + - docker:dind + script: + - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY + - docker build -t $DOCKER_IMAGE_TAG --build-arg VERSION=$CI_COMMIT_REF_SLUG . + - docker push $DOCKER_IMAGE_TAG + +build-release: + stage: build + only: + - tags + script: + # build app with version + - go install + - mv "/go/bin/$CI_PROJECT_NAME" "$CI_PROJECT_DIR/$CI_PROJECT_NAME" + artifacts: + paths: + - config_example.toml + - "$CI_PROJECT_NAME" + expire_in: never diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4cc0d67 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +## +# Compile application +## +FROM golang:latest AS build-env +WORKDIR /app +COPY . . +# ge dependencies +RUN go mod tidy +# build binary +RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o oven-exporter + + +## +# Build Image +## +FROM scratch +COPY --from=build-env /app/oven-exporter /oven-exporter +COPY --from=build-env /app/config_example.toml /config.toml + +WORKDIR / +ENTRYPOINT ["/oven-exporter"] diff --git a/api/client_test.go b/api/client_test.go new file mode 100644 index 0000000..778f64e --- /dev/null +++ b/api/client_test.go @@ -0,0 +1 @@ +package api