From 7c42dcebe5c220aab628beb700987e5376af44c2 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 13 Oct 2024 22:02:28 -0700 Subject: [PATCH] docker-push-scratch-image-docker --- .github/workflows/release.yml | 52 +++++++++++++++++++++++++++++++++ docker/Dockerfile => Dockerfile | 16 +++++----- 2 files changed, 61 insertions(+), 7 deletions(-) rename docker/Dockerfile => Dockerfile (73%) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c843b45a..20b0fa64 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -32,3 +32,55 @@ 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 GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_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 + + - 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 }}" 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