From 63655ff1208d934864d0e9ed8ff8e99ca39d228c Mon Sep 17 00:00:00 2001 From: Kris Budde Date: Wed, 15 Dec 2021 20:10:07 +0100 Subject: [PATCH] build dockerfile with git releaser --- .github/workflows/docker.yml | 67 ------------------------- .github/workflows/release.yml | 16 ++++++ .goreleaser.yml | 94 ++++++++++++++++++++++++++++++++++- Dockerfile | 50 +++---------------- 4 files changed, 115 insertions(+), 112 deletions(-) delete mode 100644 .github/workflows/docker.yml diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml deleted file mode 100644 index 4d48f10..0000000 --- a/.github/workflows/docker.yml +++ /dev/null @@ -1,67 +0,0 @@ -name: Docker Build/Publish Image - -on: - schedule: - - cron: '0 10 * * *' # everyday at 10am - push: - branches: - - '**' - tags: - - 'v*.*.*' - pull_request: - - workflow_dispatch: - -env: - PLATFORMS: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64 - -jobs: - build: - runs-on: ubuntu-18.04 - steps: - - - name: Checkout the code - uses: actions/checkout@v2.4.0 - - - name: Docker meta - id: docker_meta - uses: crazy-max/ghaction-docker-meta@v3.6.2 - with: - images: kbudde/rabbitmq-exporter - tag-sha: true # add git short SHA as Docker tag - - - name: Set up QEMU - uses: docker/setup-qemu-action@v1.2.0 - - - name: Docker Setup Buildx - uses: docker/setup-buildx-action@v1.6.0 - - - name: Login to DockerHub - if: github.event_name != 'pull_request' - uses: docker/login-action@v1.10.0 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Declare some variables - id: vars - shell: bash - run: | - echo "##[set-output name=branch;]$(echo ${GITHUB_REF}|grep -oP 'refs/heads/\K.*')" - echo "::set-output name=version::$(echo ${GITHUB_REF}|grep -oP 'refs/tags/\K.*')" - echo "::set-output name=sha_short::$(git rev-parse --short HEAD)" - - - name: Build and push images - uses: docker/build-push-action@v2.7.0 - with: - platforms: ${{ env.PLATFORMS }} - push: ${{ github.event_name != 'pull_request' }} - tags: ${{ steps.docker_meta.outputs.tags }} - labels: ${{ steps.docker_meta.outputs.labels }} - build-args: | - VERSION=${{ steps.vars.outputs.version }} - BRANCH=${{ steps.vars.outputs.branch }} - COMMIT=${{ steps.vars.outputs.sha_short }} - - - - diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 41775ac..7cca8a5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -22,6 +22,22 @@ jobs: uses: actions/setup-go@v2 with: go-version: 1.17 + - + name: Docker Login + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GH_TOKEN }} + - + name: Login to Docker Hub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - + name: Docker Setup Buildx + uses: docker/setup-buildx-action@v1.6.0 - name: Run GoReleaser uses: goreleaser/goreleaser-action@v2 diff --git a/.goreleaser.yml b/.goreleaser.yml index 03b2984..6fdd9e8 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -4,7 +4,7 @@ before: builds: - env: - CGO_ENABLED=0 - binary: rabbitmq_exporter-{{.Version}}.{{.Target}} + #binary: rabbitmq_exporter-{{.Version}}.{{.Target}} targets: - linux_amd64 - linux_arm64 @@ -28,4 +28,94 @@ changelog: - '^docs:' - '^test:' release: - discussion_category_name: Announcements \ No newline at end of file + discussion_category_name: Announcements + +dockers: +- goos: linux + goarch: amd64 + image_templates: + - "kbudde/rabbitmq-exporter:linux-amd64-{{.ShortCommit}}" + - "ghcr.io/kbudde/rabbitmq_exporter:linux-amd64-{{.ShortCommit}}" + use: buildx + build_flag_templates: + - "--label=org.opencontainers.image.created={{.Date}}" + - "--label=org.opencontainers.image.title={{.ProjectName}}" + - "--label=org.opencontainers.image.revision={{.FullCommit}}" + - "--label=org.opencontainers.image.version={{.Version}}" + - "--label=org.opencontainers.image.source=https://github.com/kbudde/rabbitmq_exporter" + - "--platform=linux/amd64" + +- goos: linux + goarch: arm + goarm: 6 + image_templates: + - "kbudde/rabbitmq-exporter:linux-arm6-{{.ShortCommit}}" + - "ghcr.io/kbudde/rabbitmq_exporter:linux-arm6-{{.ShortCommit}}" + use: buildx + build_flag_templates: + - "--label=org.opencontainers.image.created={{.Date}}" + - "--label=org.opencontainers.image.title={{.ProjectName}}" + - "--label=org.opencontainers.image.revision={{.FullCommit}}" + - "--label=org.opencontainers.image.version={{.Version}}" + - "--label=org.opencontainers.image.source=https://github.com/kbudde/rabbitmq_exporter" + - "--platform=linux/arm/v6" + +- goos: linux + goarch: arm + goarm: 7 + image_templates: + - "kbudde/rabbitmq-exporter:linux-arm7-{{.ShortCommit}}" + - "ghcr.io/kbudde/rabbitmq_exporter:linux-arm7-{{.ShortCommit}}" + use: buildx + build_flag_templates: + - "--label=org.opencontainers.image.created={{.Date}}" + - "--label=org.opencontainers.image.title={{.ProjectName}}" + - "--label=org.opencontainers.image.revision={{.FullCommit}}" + - "--label=org.opencontainers.image.version={{.Version}}" + - "--label=org.opencontainers.image.source=https://github.com/kbudde/rabbitmq_exporter" + - "--platform=linux/arm/v7" + +- goos: linux + goarch: arm64 + image_templates: + - "kbudde/rabbitmq-exporter:linux-arm64-{{.ShortCommit}}" + - "ghcr.io/kbudde/rabbitmq_exporter:linux-arm64-{{.ShortCommit}}" + use: buildx + build_flag_templates: + - "--label=org.opencontainers.image.created={{.Date}}" + - "--label=org.opencontainers.image.title={{.ProjectName}}" + - "--label=org.opencontainers.image.revision={{.FullCommit}}" + - "--label=org.opencontainers.image.version={{.Version}}" + - "--label=org.opencontainers.image.source=https://github.com/kbudde/rabbitmq_exporter" + - "--platform=linux/arm64" + + +docker_manifests: +- + name_template: ghcr.io/kbudde/rabbitmq_exporter:latest + image_templates: + - ghcr.io/kbudde/rabbitmq_exporter:linux-amd64-{{.ShortCommit}} + - ghcr.io/kbudde/rabbitmq_exporter:linux-arm6-{{.ShortCommit}} + - ghcr.io/kbudde/rabbitmq_exporter:linux-arm7-{{.ShortCommit}} + - ghcr.io/kbudde/rabbitmq_exporter:linux-arm64-{{.ShortCommit}} +- + name_template: kbudde/rabbitmq-exporter:latest + image_templates: + - kbudde/rabbitmq-exporter:linux-amd64-{{.ShortCommit}} + - kbudde/rabbitmq-exporter:linux-arm6-{{.ShortCommit}} + - kbudde/rabbitmq-exporter:linux-arm7-{{.ShortCommit}} + - kbudde/rabbitmq-exporter:linux-arm64-{{.ShortCommit}} +- + name_template: ghcr.io/kbudde/rabbitmq_exporter:{{.Version}} + image_templates: + - ghcr.io/kbudde/rabbitmq_exporter:linux-amd64-{{.ShortCommit}} + - ghcr.io/kbudde/rabbitmq_exporter:linux-arm6-{{.ShortCommit}} + - ghcr.io/kbudde/rabbitmq_exporter:linux-arm7-{{.ShortCommit}} + - ghcr.io/kbudde/rabbitmq_exporter:linux-arm64-{{.ShortCommit}} +- + name_template: kbudde/rabbitmq-exporter:{{.Version}} + image_templates: + - kbudde/rabbitmq-exporter:linux-amd64-{{.ShortCommit}} + - kbudde/rabbitmq-exporter:linux-arm6-{{.ShortCommit}} + - kbudde/rabbitmq-exporter:linux-arm7-{{.ShortCommit}} + - kbudde/rabbitmq-exporter:linux-arm64-{{.ShortCommit}} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index fcb9f16..3c0e728 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,43 +1,9 @@ -# Accept the Go version for the image to be set as a build argument. -ARG GO_VERSION=1.15 -# First stage: build the executable. -FROM golang:${GO_VERSION}-alpine AS builder - -ARG VERSION="" -ARG BRANCH="" -ARG COMMIT="" - - -# Create the user and group files that will be used in the running container to -# run the process as an unprivileged user. -RUN mkdir /user && \ - echo 'nobody:x:65534:65534:nobody:/:' > /user/passwd && \ - echo 'nobody:x:65534:' > /user/group +FROM alpine AS builder # Install the Certificate-Authority certificates for the app to be able to make # calls to HTTPS endpoints. # Git is required for fetching the dependencies. -RUN apk add --no-cache ca-certificates git - -# Set the working directory outside $GOPATH to enable the support for modules. -WORKDIR /src - -# Fetch dependencies first; they are less susceptible to change on every build -# and will therefore be cached for speeding up the next build -COPY ./go.mod ./go.sum ./ -RUN go mod download - -# Import the code from the context. -COPY ./ ./ - -# Build the executable to `/app`. Mark the build as statically linked. -RUN CGO_ENABLED=0 go build \ - -installsuffix 'static' \ - -ldflags "-X main.Revision=${COMMIT} \ - -X main.Branch=${BRANCH} \ - -X main.Version=${VERSION} \ - -X main.BuildDate=$(date -u ""+%Y%m%d-%H:%M:%S"")" \ - -o /app . +RUN apk add --no-cache ca-certificates # Final stage: the running container. FROM scratch AS final @@ -45,14 +11,11 @@ FROM scratch AS final # Add maintainer label in case somebody has questions. LABEL maintainer="Kris.Budde@gmail.com" -# Import the user and group files from the first stage. -COPY --from=builder /user/group /user/passwd /etc/ - # Import the Certificate-Authority certificates for enabling HTTPS. COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ # Import the compiled executable from the first stage. -COPY --from=builder /app /app +COPY rabbitmq_exporter /rabbitmq_exporter # Declare the port on which the webserver will be exposed. # As we're going to run the executable as an unprivileged user, we can't bind @@ -60,9 +23,10 @@ COPY --from=builder /app /app EXPOSE 9419 # Perform any further action as an unprivileged user. -USER nobody:nobody +USER 65535:65535 # Check if exporter is alive; 10 retries gives prometheus some time to retrieve bad data (5 minutes) -HEALTHCHECK --retries=10 CMD ["/app", "-check-url", "http://localhost:9419/health"] +HEALTHCHECK --retries=10 CMD ["/rabbitmq_exporter", "-check-url", "http://localhost:9419/health"] + # Run the compiled binary. -ENTRYPOINT ["/app"] +ENTRYPOINT ["/rabbitmq_exporter"]