Application 2024-05-29

Building & Pushing Docker Images with GoReleaser

Automate Docker image builds across multiple platforms using GoReleaser with GitHub Actions for streamlined CI/CD deployment.

Read in: ja
Building & Pushing Docker Images with GoReleaser

Previously, in the article titled Distributing Go Applications with GoReleaser, I wrote about distributing binaries using GoReleaser. This time, I've tried pushing images to Dockerhub, so I'll summarize it here.

Refer to the overall source code at bmf-san/gondola.

Configure .goreleaser.yaml

dockers:
  - image_templates:
      - bmfsan/gondola:latest-amd64
      - bmfsan/gondola:{{ .Version }}-amd64
    use: buildx
    goos: linux
    goarch: amd64
    build_flag_templates:
      - --platform=linux/amd64
  - image_templates:
      - bmfsan/gondola:latest-arm64
      - bmfsan/gondola:{{ .Version }}-arm64
    use: buildx
    goos: linux
    goarch: arm64
    build_flag_templates:
      - --platform=linux/arm64
  - image_templates:
      - bmfsan/gondola:latest-arm
      - bmfsan/gondola:{{ .Version }}-arm
    use: buildx
    goos: linux
    goarch: arm
    build_flag_templates:
      - --platform=linux/arm

It might look a bit lengthy, but this is how you write the dockers option.

The reason it's lengthy is because it's adjusted to create artifacts in the same way as the builds (binary creation) options. (Perhaps it could be written more concisely...)

builds:
  - env:
      - CGO_ENABLED=0
    goos:
      - linux
      - darwin
      - windows
    goarch:
      - amd64
      - arm
      - arm64
    main: ./cmd/main.go

Refer to the default values of goreleaser below.

cf. github.com/goreleaser/goreleaser/blob/main/.goreleaser.yaml

Create Dockerfile

FROM gcr.io/distroless/static-debian12
COPY gondola /
ENTRYPOINT ["./gondola", "-config", "config.yaml"]

The binary being copied is generated by goreleaser, so there's no need to build it yourself.

Edit GitHub Actions workflow

name: GoReleaser

on:
  push:
    tags:
      - '*'

permissions:
  contents: write

jobs:
  goreleaser:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go-version: [ '1.22.3' ]
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: ${{ matrix.go-version }}
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v5
        with:
          distribution: goreleaser
          version: latest
          args: release --clean
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Since I want to push images to Dockerhub, I adjusted it to log in to Dockerhub.

Set up the secrets in advance from the GitHub repository settings.

And that's how you can easily set it up! GoReleaser is convenient...!!

Tags: Golang GitHub Actions
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
☕ Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles