Skip to content

Commit

Permalink
docker-push-scratch-image-docker
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver006 committed Oct 14, 2024
1 parent b879136 commit 7c42dce
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
16 changes: 9 additions & 7 deletions docker/Dockerfile → Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down

0 comments on commit 7c42dce

Please sign in to comment.