From ebb12b0fe0a1bbda8f23241da02383917dd4368e Mon Sep 17 00:00:00 2001 From: Gerrit Date: Fri, 18 Aug 2023 13:20:54 +0200 Subject: [PATCH] Get rid off builder image. (#460) --- .github/workflows/docker.yaml | 23 +++++++------- Dockerfile | 4 +-- Makefile | 56 +++++++++++++++++++++++------------ proto/Makefile | 4 +-- 4 files changed, 52 insertions(+), 35 deletions(-) diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index ca0409e5d..20ddaa75c 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -31,19 +31,16 @@ jobs: - name: Checkout uses: actions/checkout@v3 - - name: Set up Go 1.21 + - name: Setup Go uses: actions/setup-go@v4 with: - go-version: '1.21.x' - - - name: generate proto and lint - working-directory: proto - run: make protoc + go-version-file: 'go.mod' + cache: false - name: Lint uses: golangci/golangci-lint-action@v3 with: - args: --build-tags integration -p bugs -p unused --timeout=3m + args: --build-tags integration -p bugs -p unused --timeout=5m - name: Make tag run: | @@ -51,7 +48,11 @@ jobs: [ "${GITHUB_EVENT_NAME}" == 'release' ] && echo "tag=${GITHUB_REF##*/}" >> $GITHUB_ENV || true [ "${GITHUB_EVENT_NAME}" == 'push' ] && echo "tag=latest" >> $GITHUB_ENV || true - - name: Build and push image + - name: Build + run: | + make release + + - name: Push image uses: docker/build-push-action@v4 with: context: . @@ -64,11 +65,11 @@ jobs: - name: Checkout uses: actions/checkout@v3 - - name: Set up Go 1.21 + - name: Setup Go uses: actions/setup-go@v4 with: - go-version: '1.21.x' + go-version-file: 'go.mod' - name: Run integration tests run: | - go test -tags=integration -timeout 600s -p 1 ./... + make test-integration diff --git a/Dockerfile b/Dockerfile index f245c4e84..bc44455f3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,4 @@ -FROM metalstack/builder:latest as builder - FROM alpine:3.18 RUN apk -U add ca-certificates -COPY --from=builder /work/bin/metal-api /metal-api +COPY bin/metal-api /metal-api CMD ["/metal-api"] diff --git a/Makefile b/Makefile index bdfa96b08..d426b1910 100644 --- a/Makefile +++ b/Makefile @@ -1,36 +1,54 @@ -BINARY := metal-api -MAINMODULE := github.com/metal-stack/metal-api/cmd/metal-api -COMMONDIR := $(or ${COMMONDIR},../builder) +CGO_ENABLED := 1 + +SHA := $(shell git rev-parse --short=8 HEAD) +GITVERSION := $(shell git describe --long --all) +BUILDDATE := $(shell date --rfc-3339=seconds) +VERSION := $(or ${VERSION},$(shell git describe --tags --exact-match 2> /dev/null || git symbolic-ref -q --short HEAD || git rev-parse --short HEAD)) + MINI_LAB_KUBECONFIG := $(shell pwd)/../mini-lab/.kubeconfig -include $(COMMONDIR)/Makefile.inc +LINKMODE := -linkmode external -extldflags '-static -s -w' \ + -X 'github.com/metal-stack/v.Version=$(VERSION)' \ + -X 'github.com/metal-stack/v.Revision=$(GITVERSION)' \ + -X 'github.com/metal-stack/v.GitSHA1=$(SHA)' \ + -X 'github.com/metal-stack/v.BuildDate=$(BUILDDATE)' + +.PHONY: release +release: protoc test build + +.PHONY: build +build: + go build \ + -tags 'osusergo netgo static_build' \ + -ldflags \ + "$(LINKMODE)" \ + -o bin/metal-api \ + github.com/metal-stack/metal-api/cmd/metal-api + + md5sum bin/metal-api > bin/metal-api.md5 -release:: spec check-diff all ; + bin/metal-api dump-swagger | jq -r -S 'walk(if type == "array" then sort_by(strings) else . end)' > spec/metal-api.json || { echo "jq >=1.6 required"; exit 1; } -.PHONY: spec -spec: all - bin/$(BINARY) dump-swagger | jq -r -S 'walk(if type == "array" then sort_by(strings) else . end)' > spec/metal-api.json || { echo "jq >=1.6 required"; exit 1; } +.PHONY: test +test: test-unit check-diff + +.PHONY: test-unit +test-unit: + go test -race -cover ./... + +.PHONY: test-integration +test-integration: + go test -tags=integration -timeout 600s -p 1 ./... .PHONY: check-diff check-diff: spec git diff --exit-code spec pkg -.PHONY: redoc -redoc: - docker run --rm --user $$(id -u):$$(id -g) -v $(PWD):/work -w /work letsdeal/redoc-cli bundle -o generate/index.html /work/spec/metal-api.json - xdg-open generate/index.html - .PHONY: protoc protoc: rm -rf pkg/api/v1 make -C proto protoc -.PHONY: protoc-docker -protoc-docker: - rm -rf pkg/api/v1 - docker pull bufbuild/buf:1.14.0 - docker run --rm --user $$(id -u):$$(id -g) -v $(PWD):/work --tmpfs /.cache -w /work/proto bufbuild/buf:1.14.0 generate -v - .PHONY: mini-lab-push mini-lab-push: make diff --git a/proto/Makefile b/proto/Makefile index 17a9b1bad..2fba29055 100644 --- a/proto/Makefile +++ b/proto/Makefile @@ -1,5 +1,5 @@ MAKEFLAGS += --no-print-directory -BUF_VERSION := 1.23.1 +BUF_VERSION := 1.26.1 _buf: docker run --rm \ @@ -16,4 +16,4 @@ protolint: .PHONY: protoc protoc: protolint - @$(MAKE) _buf CMD="generate -v" \ No newline at end of file + @$(MAKE) _buf CMD="generate -v"