DEV Community

Cover image for Building & Pushing Docker Images with GoReleaser
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

Building & Pushing Docker Images with GoReleaser

This article was originally published on bmf-tech.com.

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

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 }}
Enter fullscreen mode Exit fullscreen mode

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...!!

Top comments (0)