From 1928b50c0ba39af61c8789e835d99ba1e8671508 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 13 Oct 2024 22:02:28 -0700 Subject: [PATCH] Move docker build and push for releases to Github Actions --- .drone.yml | 353 -------------------------------- .github/workflows/release.yml | 71 +++++++ docker/Dockerfile => Dockerfile | 16 +- 3 files changed, 80 insertions(+), 360 deletions(-) delete mode 100644 .drone.yml rename docker/Dockerfile => Dockerfile (73%) diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index 5cce1541..00000000 --- a/.drone.yml +++ /dev/null @@ -1,353 +0,0 @@ -kind: pipeline -name: default -type: docker - - -steps: - - name: start - image: golang:1.23 - pull: if-not-exists - commands: - - sleep 10 - - 'echo "start"' - when: - event: - - tag - - - name: release-docker-image-scratch - image: plugins/docker - pull: if-not-exists - settings: - mtu: 1200 - tags: "latest,latest-amd64,${DRONE_TAG},${DRONE_TAG}-amd64" - dockerfile: ./docker/Dockerfile - repo: oliver006/redis_exporter - target: scratch-release - no_cache: true - build_args: - - 'TAG=${DRONE_TAG}' - - 'SHA1=${DRONE_COMMIT_SHA}' - - 'GOARCH=amd64' - username: - from_secret: docker_user - password: - from_secret: docker_pass - when: - event: - - tag - depends_on: - - start - - - - name: release-docker-image-alpine-arm64 - image: plugins/docker - pull: if-not-exists - settings: - mtu: 1200 - tags: "latest-arm64,${DRONE_TAG}-arm64,alpine-arm64" - dockerfile: ./docker/Dockerfile - repo: oliver006/redis_exporter - target: alpine - no_cache: true - build_args: - - 'TAG=${DRONE_TAG}' - - 'SHA1=${DRONE_COMMIT_SHA}' - - 'GOARCH=arm64' - username: - from_secret: docker_user - password: - from_secret: docker_pass - when: - event: - - tag - depends_on: - - start - - - - name: release-docker-image-alpine-arm - image: plugins/docker - pull: if-not-exists - settings: - mtu: 1200 - tags: "latest-arm,${DRONE_TAG}-arm,alpine-arm" - dockerfile: ./docker/Dockerfile - repo: oliver006/redis_exporter - target: alpine - no_cache: true - build_args: - - 'TAG=${DRONE_TAG}' - - 'SHA1=${DRONE_COMMIT_SHA}' - - 'GOARCH=arm' - username: - from_secret: docker_user - password: - from_secret: docker_pass - when: - event: - - tag - depends_on: - - start - - - - name: release-docker-image-alpine-amd64 - image: plugins/docker - pull: if-not-exists - settings: - mtu: 1200 - tags: "${DRONE_TAG}-alpine,alpine-amd64" - dockerfile: ./docker/Dockerfile - repo: oliver006/redis_exporter - target: alpine - no_cache: true - build_args: - - 'TAG=${DRONE_TAG}' - - 'SHA1=${DRONE_COMMIT_SHA}' - - 'GOARCH=amd64' - username: - from_secret: docker_user - password: - from_secret: docker_pass - when: - event: - - tag - depends_on: - - start - - - - name: manifest-docker-latest - image: plugins/manifest - pull: if-not-exists - settings: - username: - from_secret: docker_user - password: - from_secret: docker_pass - target: oliver006/redis_exporter:latest - template: "oliver006/redis_exporter:${DRONE_TAG}-ARCH" - platforms: - - linux/amd64 - - linux/arm - - linux/arm64 - when: - event: - - tag - depends_on: - - release-docker-image-scratch - - release-docker-image-alpine-arm64 - - release-docker-image-alpine-arm - - release-docker-image-alpine-amd64 - - - - name: manifest-docker-tag - image: plugins/manifest - pull: if-not-exists - settings: - username: - from_secret: docker_user - password: - from_secret: docker_pass - target: "oliver006/redis_exporter:${DRONE_TAG}" - template: "oliver006/redis_exporter:${DRONE_TAG}-ARCH" - platforms: - - linux/amd64 - - linux/arm - - linux/arm64 - when: - event: - - tag - depends_on: - - manifest-docker-latest - - - - name: manifest-docker-alpine - image: plugins/manifest - pull: if-not-exists - settings: - username: - from_secret: docker_user - password: - from_secret: docker_pass - target: oliver006/redis_exporter:alpine - template: oliver006/redis_exporter:alpine-ARCH - platforms: - - linux/amd64 - - linux/arm - - linux/arm64 - when: - event: - - tag - depends_on: - - manifest-docker-tag - - -########### -# -# Mirroring the docker images to quay.io -# - - name: release-quay-scratch - image: plugins/docker - pull: if-not-exists - settings: - mtu: 1200 - registry: "quay.io" - repo: "quay.io/oliver006/redis_exporter" - tags: "latest,latest-amd64,${DRONE_TAG},${DRONE_TAG}-amd64" - dockerfile: ./docker/Dockerfile - target: scratch-release - no_cache: true - build_args: - - 'TAG=${DRONE_TAG}' - - 'SHA1=${DRONE_COMMIT_SHA}' - - 'GOARCH=amd64' - username: - from_secret: quay_user - password: - from_secret: quay_pass - when: - event: - - tag - depends_on: - - start - - - - name: release-quay-alpine-arm64 - image: plugins/docker - pull: if-not-exists - settings: - mtu: 1200 - registry: "quay.io" - repo: "quay.io/oliver006/redis_exporter" - tags: "latest-arm64,${DRONE_TAG}-arm64,alpine-arm64" - dockerfile: ./docker/Dockerfile - target: alpine - no_cache: true - build_args: - - 'TAG=${DRONE_TAG}' - - 'SHA1=${DRONE_COMMIT_SHA}' - - 'GOARCH=arm64' - username: - from_secret: quay_user - password: - from_secret: quay_pass - when: - event: - - tag - depends_on: - - start - - - - name: release-quay-alpine-arm - image: plugins/docker - pull: if-not-exists - settings: - mtu: 1200 - registry: "quay.io" - repo: "quay.io/oliver006/redis_exporter" - tags: "latest-arm,${DRONE_TAG}-arm,alpine-arm" - dockerfile: ./docker/Dockerfile - target: alpine - no_cache: true - build_args: - - 'TAG=${DRONE_TAG}' - - 'SHA1=${DRONE_COMMIT_SHA}' - - 'GOARCH=arm' - username: - from_secret: quay_user - password: - from_secret: quay_pass - when: - event: - - tag - depends_on: - - start - - - - name: release-quay-alpine-amd64 - image: plugins/docker - pull: if-not-exists - settings: - mtu: 1200 - registry: "quay.io" - repo: "quay.io/oliver006/redis_exporter" - tags: "${DRONE_TAG}-alpine,alpine-amd64" - dockerfile: ./docker/Dockerfile - target: alpine - no_cache: true - build_args: - - 'TAG=${DRONE_TAG}' - - 'SHA1=${DRONE_COMMIT_SHA}' - - 'GOARCH=amd64' - username: - from_secret: quay_user - password: - from_secret: quay_pass - when: - event: - - tag - depends_on: - - start - - - - name: manifest-quay-latest - image: plugins/manifest - pull: if-not-exists - settings: - username: - from_secret: quay_user - password: - from_secret: quay_pass - target: quay.io/oliver006/redis_exporter:latest - template: "quay.io/oliver006/redis_exporter:${DRONE_TAG}-ARCH" - platforms: - - linux/amd64 - - linux/arm - - linux/arm64 - when: - event: - - tag - depends_on: - - release-quay-alpine-arm64 - - release-quay-alpine-arm - - release-quay-alpine-amd64 - - - - name: manifest-quay-tag - image: plugins/manifest - pull: if-not-exists - settings: - username: - from_secret: quay_user - password: - from_secret: quay_pass - target: "quay.io/oliver006/redis_exporter:${DRONE_TAG}" - template: "quay.io/oliver006/redis_exporter:${DRONE_TAG}-ARCH" - platforms: - - linux/amd64 - - linux/arm - - linux/arm64 - when: - event: - - tag - depends_on: - - manifest-quay-latest - - - - name: manifest-quay-alpine - image: plugins/manifest - pull: if-not-exists - settings: - username: - from_secret: quay_user - password: - from_secret: quay_pass - target: quay.io/oliver006/redis_exporter:alpine - template: quay.io/oliver006/redis_exporter:alpine-ARCH - platforms: - - linux/amd64 - - linux/arm - - linux/arm64 - when: - event: - - tag - depends_on: - - manifest-quay-tag diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c843b45a..842b9ff3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -32,3 +32,74 @@ jobs: artifacts: "dist/*" allowUpdates: true omitBodyDuringUpdate: true + + + docker-push-scratch-image-docker: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + attestations: write + id-token: write + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Login to ghcr.io + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Login to quay.io + uses: docker/login-action@v3 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_TOKEN }} + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + # list of Docker images to use as base name for tags + images: | + oliver006/redis_exporter + ghcr.io/oliver006/redis_exporter + quay.io/oliver006/redis_exporter + + - name: Build and push scratch image + uses: docker/build-push-action@v6 + with: + context: . + target: scratch-release + platforms: linux/amd64,linux/arm,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: "TAG=${{ github.ref_name }},SHA1=${{ github.sha }}" + + - name: Build and push alpine image + uses: docker/build-push-action@v6 + with: + context: . + target: alpine + platforms: linux/amd64,linux/arm,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }},alpine + labels: ${{ steps.meta.outputs.labels }} + build-args: "TAG=${{ github.ref_name }},SHA1=${{ github.sha }}" diff --git a/docker/Dockerfile b/Dockerfile similarity index 73% rename from docker/Dockerfile rename to Dockerfile index c900162a..85fb5636 100644 --- a/docker/Dockerfile +++ b/Dockerfile @@ -1,28 +1,30 @@ -ARG GOARCH +ARG TARGETPLATFORM + # # build container # -FROM --platform=linux/amd64 golang:1.23-alpine AS builder +FROM --platform=$BUILDPLATFORM golang:1.23-alpine AS builder WORKDIR /go/src/github.com/oliver006/redis_exporter/ ADD . /go/src/github.com/oliver006/redis_exporter/ ARG SHA1="[no-sha]" ARG TAG="[no-tag]" -ARG GOARCH +ARG TARGETOS +ARG TARGETARCH #RUN printf "nameserver 1.1.1.1\nnameserver 8.8.8.8"> /etc/resolv.conf \ && apk --no-cache add ca-certificates git RUN apk --no-cache add ca-certificates git -RUN BUILD_DATE=$(date +%F-%T) CGO_ENABLED=0 GOOS=linux GOARCH=$GOARCH go build -o /redis_exporter \ +RUN BUILD_DATE=$(date +%F-%T) CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /redis_exporter \ -ldflags "-s -w -extldflags \"-static\" -X main.BuildVersion=$TAG -X main.BuildCommitSha=$SHA1 -X main.BuildDate=$BUILD_DATE" . -RUN [ "$GOARCH" = "amd64" ] && /redis_exporter -version || ls -la /redis_exporter +RUN [ "$TARGETARCH" = "amd64" ] && /redis_exporter -version || ls -la /redis_exporter # # scratch release container # -FROM --platform=linux/$GOARCH scratch AS scratch-release +FROM scratch AS scratch-release COPY --from=builder /redis_exporter /redis_exporter COPY --from=builder /etc/ssl/certs /etc/ssl/certs @@ -38,7 +40,7 @@ ENTRYPOINT [ "/redis_exporter" ] # # Alpine release container # -FROM --platform=linux/$GOARCH alpine:3.20 AS alpine +FROM alpine:3.20 AS alpine COPY --from=builder /redis_exporter /redis_exporter COPY --from=builder /etc/ssl/certs /etc/ssl/certs