Skip to content

Commit b3763be

Browse files
authored
change to use gorelaser for build docker images (#140)
1 parent e621192 commit b3763be

File tree

4 files changed

+45
-48
lines changed

4 files changed

+45
-48
lines changed

.github/workflows/release.yaml

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ on:
77

88
jobs:
99
goreleaser:
10-
name: Release pre-build binary
10+
name: Release pre-build binary and docker images by goreleaser
1111
runs-on: ubuntu-latest
1212
permissions:
1313
contents: write
14+
packages: write
15+
env:
16+
REGISTRY: ghcr.io
1417
steps:
1518
- name: Checkout code
1619
uses: actions/checkout@v4.2.2
@@ -20,39 +23,19 @@ jobs:
2023
uses: actions/setup-go@v5.3.0
2124
with:
2225
go-version-file: ./go.mod
26+
- name: Log in to the Container registry
27+
uses: docker/login-action@v3.4.0
28+
with:
29+
registry: ${{ env.REGISTRY }}
30+
username: ${{ github.actor }}
31+
password: ${{ secrets.GITHUB_TOKEN }}
32+
- name: Set up QEMU
33+
uses: docker/setup-qemu-action@v3.6.0
34+
- name: Set up Docker Buildx
35+
uses: docker/setup-buildx-action@v3.10.0
2336
- name: Run GoReleaser
2437
uses: goreleaser/goreleaser-action@v6.2.1
2538
with:
2639
args: release --clean
2740
env:
2841
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29-
30-
# https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-actions#upgrading-a-workflow-that-accesses-a-registry-using-a-personal-access-token
31-
build-and-push-ghcr:
32-
name: Build and Push GHCR
33-
runs-on: ubuntu-latest
34-
permissions:
35-
contents: read
36-
packages: write
37-
env:
38-
IMAGE_NAME: wrench
39-
steps:
40-
- name: Checkout code
41-
uses: actions/checkout@v4.2.2
42-
with:
43-
fetch-depth: 0
44-
- name: Build image
45-
run: docker build . --file Dockerfile --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}"
46-
- name: Log in to registry
47-
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
48-
- name: Push image
49-
run: |
50-
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME
51-
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
52-
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
53-
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
54-
[ "$VERSION" == "main" ] && VERSION=latest
55-
echo IMAGE_ID=$IMAGE_ID
56-
echo VERSION=$VERSION
57-
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
58-
docker push $IMAGE_ID:$VERSION

.goreleaser.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ builds:
1010
- linux
1111
- windows
1212
- darwin
13+
goarch:
14+
- "386"
15+
- amd64
16+
- arm64
1317
ldflags:
1418
- -s -w -X github.com/cloudspannerecosystem/wrench/cmd.version={{.Version}}
1519
ignore:
@@ -32,3 +36,27 @@ changelog:
3236
exclude:
3337
- '^docs:'
3438
- '^test:'
39+
40+
# https://goreleaser.com/customization/docker/
41+
dockers:
42+
- image_templates: ["ghcr.io/cloudspannerecosystem/{{ .ProjectName }}:{{ .Version }}-amd64"]
43+
goarch: amd64
44+
dockerfile: Dockerfile
45+
use: buildx
46+
build_flag_templates:
47+
- --platform=linux/amd64
48+
- image_templates: ["ghcr.io/cloudspannerecosystem/{{ .ProjectName }}:{{ .Version }}-arm64v8"]
49+
goarch: arm64
50+
dockerfile: Dockerfile
51+
use: buildx
52+
build_flag_templates:
53+
- --platform=linux/arm64/v8
54+
docker_manifests:
55+
- name_template: ghcr.io/cloudspannerecosystem/{{ .ProjectName }}:{{ .Version }}
56+
image_templates:
57+
- ghcr.io/cloudspannerecosystem/{{ .ProjectName }}:{{ .Version }}-amd64
58+
- ghcr.io/cloudspannerecosystem/{{ .ProjectName }}:{{ .Version }}-arm64v8
59+
- name_template: ghcr.io/cloudspannerecosystem/{{ .ProjectName }}:latest
60+
image_templates:
61+
- ghcr.io/cloudspannerecosystem/{{ .ProjectName }}:{{ .Version }}-amd64
62+
- ghcr.io/cloudspannerecosystem/{{ .ProjectName }}:{{ .Version }}-arm64v8

Dockerfile

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
FROM golang:1.24 as build
2-
3-
ARG VERSION
4-
5-
WORKDIR /go/src/app
6-
COPY . .
1+
FROM gcr.io/distroless/static-debian12
72

8-
RUN go mod download
9-
RUN CGO_ENABLED=0 go build \
10-
-ldflags "-s -w -X github.com/cloudspannerecosystem/wrench/cmd.version=${VERSION}" \
11-
-o /go/bin/app/wrench
3+
# The binary is built by goreleaser
4+
COPY wrench /
125

13-
FROM gcr.io/distroless/static-debian12
14-
COPY --from=build /go/bin/app/wrench /
156
ENTRYPOINT ["/wrench"]

Makefile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ SPANNER_EMULATOR_HOST_REST := localhost:9020
66
export SPANNER_PROJECT_ID ?= wrench-test-project
77
export SPANNER_INSTANCE_ID ?= wrench-test-instance
88

9-
REGISTRY := ghcr.io/cloudspannerecosystem/wrench
10-
119
.PHONY: test
1210
test:
1311
go test -race -v ./...
@@ -23,6 +21,3 @@ build:
2321

2422
setup-emulator:
2523
curl -s "${SPANNER_EMULATOR_HOST_REST}/v1/projects/${SPANNER_PROJECT_ID}/instances" --data '{"instanceId": "'${SPANNER_INSTANCE_ID}'"}'
26-
27-
docker-build:
28-
docker build . -t $(REGISTRY):$(VERSION) --build-arg VERSION=$(VERSION)

0 commit comments

Comments
 (0)