From 8d99b94c2a6b80ce25768f26d1ad1dbca7471caa Mon Sep 17 00:00:00 2001 From: wouldgo Date: Tue, 22 Mar 2022 22:22:36 +0100 Subject: [PATCH] multi-arch docker build on tag --- .github/workflows/tags.yaml | 43 +++++++++++++++++++++++++++++++++++++ .gitignore | 3 ++- Dockerfile | 17 +++++++++++++++ Makefile | 28 ++++++++++++++++++++++++ _out/.keep | 0 go.mod | 16 ++++++++++---- 6 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/tags.yaml create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 _out/.keep diff --git a/.github/workflows/tags.yaml b/.github/workflows/tags.yaml new file mode 100644 index 0000000..0cdf9b9 --- /dev/null +++ b/.github/workflows/tags.yaml @@ -0,0 +1,43 @@ +name: build-unbound_explorer +on: + create: + tags: + - ^v\d{1,}\.\d{1,}\.\d{1,}$ + +jobs: + build: + runs-on: ubuntu-18.04 + env: + CONTAINER_IMAGE: ghcr.io/${{github.repository}} + DOCKER_VERSION: ${{github.ref_name}} + DOCKER_TARGET_PLATFORM: linux/arm64,linux/amd64 + steps: + - + id: checkout + name: Checkout the code + uses: actions/checkout@v2 + - + id: install-qemu + name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - + id: install-buildx + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + with: + install: true + - + id: login-registry + name: Log into registry + run: | + echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} \ + --password-stdin + - + id: build + name: Build + run: | + docker build \ + --platform ${DOCKER_TARGET_PLATFORM} \ + --tag ${CONTAINER_IMAGE}:${DOCKER_VERSION} \ + --push \ + . diff --git a/.gitignore b/.gitignore index 32f447f..88dd7f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -unbound_exporter .idea/ +_out/* +!_out/.keep diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..445360b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM golang:1.17-alpine3.14 AS builder + +RUN apk add --no-cache \ + build-base \ + make \ + curl + +WORKDIR /workspace +RUN mkdir _out + +COPY go.mod go.sum Makefile unbound_exporter.go ./ +RUN make clean-compile + +FROM scratch +COPY --from=builder /workspace/_out/unbound_explorer /usr/local/bin/unbound_exporter + +ENTRYPOINT ["unbound_exporter"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2c794af --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +SHELL := /bin/sh +OUT := $(shell pwd)/_out +BUILDARCH := $(shell uname -m) +GCC := $(OUT)/$(BUILDARCH)-linux-musl-cross/bin/$(BUILDARCH)-linux-musl-gcc +LD := $(OUT)/$(BUILDARCH)-linux-musl-cross/bin/$(BUILDARCH)-linux-musl-ld + +clean-compile: musl deps compile + +compile: + CGO_ENABLED=1 \ + CC_FOR_TARGET=$(GCC) \ + CC=$(GCC) \ + go build \ + -ldflags '-linkmode external -extldflags -static' \ + -a -o _out/unbound_explorer . + +deps: + go mod tidy -v + go mod download + +musl: clean + (cd $(OUT); curl -LOk https://musl.cc/$(BUILDARCH)-linux-musl-cross.tgz) + tar zxf $(OUT)/$(BUILDARCH)-linux-musl-cross.tgz -C $(OUT) + +clean: + rm -Rf $(OUT) $(BINARY_NAME) + mkdir -p $(OUT) + touch $(OUT)/.keep diff --git a/_out/.keep b/_out/.keep new file mode 100644 index 0000000..e69de29 diff --git a/go.mod b/go.mod index fdb153c..131158a 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,22 @@ module github.com/letsencrypt/unbound_exporter -go 1.16 +go 1.17 require ( - github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/go-kit/log v0.2.0 - github.com/golang/protobuf v1.5.2 // indirect - github.com/google/go-cmp v0.5.6 // indirect github.com/prometheus/client_golang v1.11.0 github.com/prometheus/common v0.32.1 +) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/go-logfmt/logfmt v0.5.1 // indirect + github.com/golang/protobuf v1.5.2 // indirect + github.com/google/go-cmp v0.5.6 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/prometheus/client_model v0.2.0 // indirect github.com/prometheus/procfs v0.7.3 // indirect golang.org/x/sys v0.0.0-20211112193437-faf0a1b62c6b // indirect google.golang.org/protobuf v1.27.1 // indirect