feat: Create multiarch artifacts (#58)

This commit is contained in:
2026-07-22 12:33:57 +02:00
committed by GitHub
parent a36a5504fa
commit 49a63ef071
5 changed files with 124 additions and 58 deletions
+92 -16
View File
@@ -27,17 +27,26 @@ jobs:
- name: ${{ matrix.make }} - name: ${{ matrix.make }}
run: make ${{ matrix.make }} run: make ${{ matrix.make }}
deploy: build-binaries:
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: tests needs: tests
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
strategy: strategy:
matrix: matrix:
goosarch: [linux-amd64] include:
- goos: linux
goarch: amd64
- goos: linux
goarch: arm64
- goos: darwin
goarch: amd64
- goos: darwin
goarch: arm64
- goos: windows
goarch: amd64
steps: steps:
- uses: actions/checkout@v6 - uses: actions/checkout@v6
with:
fetch-depth: 0
- name: install go - name: install go
uses: actions/setup-go@v6 uses: actions/setup-go@v6
with: with:
@@ -47,27 +56,94 @@ jobs:
- name: Set env - name: Set env
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Sign in to dockerhub
run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
- name: Deploy image
run: make docker-push VERSION=$RELEASE_VERSION
- name: Build - name: Build
run: make build VERSION=$RELEASE_VERSION run: make build VERSION="$RELEASE_VERSION" GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }}
- name: Prepare release - name: Package
run: |
BASE="whatismyip-$RELEASE_VERSION-${{ matrix.goos }}-${{ matrix.goarch }}"
if [ "${{ matrix.goos }}" = "windows" ]; then
cp whatismyip "${BASE}.exe"
zip "${BASE}.zip" "${BASE}.exe" LICENSE README.md
sha256sum "${BASE}.zip" > "${BASE}.zip.sha256"
else
tar zcvf "${BASE}.tar.gz" whatismyip LICENSE README.md
sha256sum "${BASE}.tar.gz" > "${BASE}.tar.gz.sha256"
fi
- name: Upload artifact
uses: actions/upload-artifact@v6
with:
name: binary-${{ matrix.goos }}-${{ matrix.goarch }}
path: |
whatismyip-${{ env.RELEASE_VERSION }}-${{ matrix.goos }}-${{ matrix.goarch }}.*
if-no-files-found: error
publish-docker:
runs-on: ubuntu-latest
needs: tests
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
steps:
- uses: actions/checkout@v6
- name: Set env
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Set up QEMU
uses: docker/setup-qemu-action@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Sign in to Docker Hub
uses: docker/login-action@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
build-args: ARG_VERSION=${{ env.RELEASE_VERSION }}
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/whatismyip:${{ env.RELEASE_VERSION }}
${{ secrets.DOCKERHUB_USERNAME }}/whatismyip:latest
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
release:
runs-on: ubuntu-latest
needs: [build-binaries, publish-docker]
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Set env
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Download all artifacts
uses: actions/download-artifact@v6
with:
pattern: binary-*
merge-multiple: true
- name: Generate changelog
run: | run: |
git log $(git describe HEAD~ --tags --abbrev=0)..HEAD --pretty='format:%h - %s <%an>' --no-merges > changelog.txt git log $(git describe HEAD~ --tags --abbrev=0)..HEAD --pretty='format:%h - %s <%an>' --no-merges > changelog.txt
tar zcvf whatismyip-$RELEASE_VERSION-${{matrix.goosarch}}.tar.gz whatismyip LICENSE README.md
sha256sum whatismyip-$RELEASE_VERSION-${{matrix.goosarch}}.tar.gz > whatismyip-$RELEASE_VERSION-${{matrix.goosarch}}.tar.gz.sha256
- name: Release - name: Release
uses: softprops/action-gh-release@v3 uses: softprops/action-gh-release@v3
with: with:
body_path: changelog.txt body_path: changelog.txt
files: | files: |
whatismyip-${{env.RELEASE_VERSION}}-${{matrix.goosarch}}.tar.gz whatismyip-${{ env.RELEASE_VERSION }}-*.tar.gz
whatismyip-${{env.RELEASE_VERSION}}-${{matrix.goosarch}}.tar.gz.sha256 whatismyip-${{ env.RELEASE_VERSION }}-*.tar.gz.sha256
whatismyip-${{ env.RELEASE_VERSION }}-*.zip
whatismyip-${{ env.RELEASE_VERSION }}-*.zip.sha256
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+1
View File
@@ -1 +1,2 @@
whatismyip whatismyip
dist/
+9 -8
View File
@@ -5,16 +5,17 @@ Single-binary Go service: HTTP/TLS/QUIC server that returns the client's IP, geo
## Commands ## Commands
```sh ```sh
make build # CGO_ENABLED=0 build -ldflags "-s -w" -> ./whatismyip make build # CGO_ENABLED=0 -> ./whatismyip (GOOS/GOARCH for cross-compile)
make unit-test # go test -count=1 -race -short -cover ./... make build-all # cross-compile all 5 platforms into dist/
make integration-test # requires Docker; runs separate module (integration-tests/) make unit-test # go test -count=1 -race -short -cover ./...
make test # unit-test + integration-test make integration-test # requires Docker; runs integration-tests/ module
make lint # gofmt -l + golangci-lint + shadow (auto-installs tools) make test # unit-test + integration-test
make docker-run # builds & runs container exposing :8080 :8081 :9100 make lint # gofmt -l + golangci-lint + shadow (auto-installs tools)
make docker-push VERSION=x.y.z # tagged release push (blocks devel builds) make docker-build # local single-arch build (root Dockerfile)
make docker-run # builds dev image + runs with test DBs, ports :8080 :8081 :9100
``` ```
**Order**: `lint -> unit-test -> integration-test`. Integration tests depend on testcontainers-go (needs Docker running). Integration tests are a separate Go module with its own `go.mod` — run from the `integration-tests/` directory. **Order and quirks**: `lint -> unit-test -> integration-test`. Integration tests use testcontainers-go (needs Docker). They are a separate Go module with its own `go.mod` — run from the `integration-tests/` directory. They use `test/Dockerfile` (distinct from the root `Dockerfile`).
## Architecture ## Architecture
+9 -18
View File
@@ -1,29 +1,20 @@
FROM golang:1.25-alpine AS builder FROM --platform=$BUILDPLATFORM golang:1.25-alpine AS builder
ARG ARG_VERSION ARG ARG_VERSION
ARG TARGETOS
ARG TARGETARCH
ENV VERSION=$ARG_VERSION ENV VERSION=$ARG_VERSION
WORKDIR /app WORKDIR /app
COPY go.mod . COPY go.mod go.sum ./
COPY go.sum .
RUN --mount=type=cache,target=/go/pkg/mod/ go mod download -x RUN --mount=type=cache,target=/go/pkg/mod/ go mod download -x
COPY . . COPY . .
FROM builder AS build-dev-app RUN --mount=type=cache,target=/go/pkg/mod/ apk --no-cache add make ca-certificates \
# hadolint ignore=DL3018
RUN --mount=type=cache,target=/go/pkg/mod/ apk --no-cache add make && make build
FROM builder AS build-prod-app
# hadolint ignore=DL3018
RUN apk --no-cache update && apk add --no-cache ca-certificates make upx \
&& update-ca-certificates \ && update-ca-certificates \
&& make build \ && GOOS=$TARGETOS GOARCH=$TARGETARCH make build
&& upx --best --lzma whatismyip
FROM scratch AS dev FROM scratch
COPY --from=build-dev-app /app/whatismyip /usr/bin/ COPY --from=builder /app/whatismyip /usr/bin/
ENTRYPOINT ["whatismyip"] COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
FROM scratch AS prod
COPY --from=build-prod-app /app/whatismyip /usr/bin/
ENTRYPOINT ["whatismyip"] ENTRYPOINT ["whatismyip"]
+13 -16
View File
@@ -1,6 +1,8 @@
GOPATH ?= $(shell go env GOPATH) GOPATH ?= $(shell go env GOPATH)
VERSION ?= devel-$(shell git rev-parse --short HEAD) VERSION ?= devel-$(shell git rev-parse --short HEAD)
DOCKER_URL ?= dcarrillo/whatismyip DOCKER_URL ?= dcarrillo/whatismyip
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
.PHONY: test .PHONY: test
test: unit-test integration-test test: unit-test integration-test
@@ -32,25 +34,20 @@ lint: install-tools
shadow ./... shadow ./...
build: build:
CGO_ENABLED=0 go build -ldflags="-s -w -X 'github.com/dcarrillo/whatismyip/internal/core.Version=${VERSION}'" -o whatismyip ./cmd CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -ldflags="-s -w -X 'github.com/dcarrillo/whatismyip/internal/core.Version=${VERSION}'" -o whatismyip ./cmd
docker-build-dev: build-all:
docker build --target=dev --build-arg=ARG_VERSION="${VERSION}" --tag ${DOCKER_URL}:${VERSION} . @mkdir -p dist
GOOS=linux GOARCH=amd64 $(MAKE) build && mv whatismyip dist/whatismyip-linux-amd64
GOOS=linux GOARCH=arm64 $(MAKE) build && mv whatismyip dist/whatismyip-linux-arm64
GOOS=darwin GOARCH=amd64 $(MAKE) build && mv whatismyip dist/whatismyip-darwin-amd64
GOOS=darwin GOARCH=arm64 $(MAKE) build && mv whatismyip dist/whatismyip-darwin-arm64
GOOS=windows GOARCH=amd64 $(MAKE) build && mv whatismyip dist/whatismyip-windows-amd64.exe
docker-build-prod: docker-build:
docker build --target=prod --build-arg=ARG_VERSION="${VERSION}" --tag ${DOCKER_URL}:${VERSION} . docker build --build-arg=ARG_VERSION="${VERSION}" --tag ${DOCKER_URL}:${VERSION} .
docker-push: docker-build-prod docker-run: docker-build
ifneq (,$(findstring devel-,$(VERSION)))
@echo "VERSION is set to ${VERSION}, I can't push devel builds"
exit 1
else
docker push ${DOCKER_URL}:${VERSION}
docker tag ${DOCKER_URL}:${VERSION} ${DOCKER_URL}:latest
docker push ${DOCKER_URL}:latest
endif
docker-run: docker-build-dev
docker run --tty --interactive --rm \ docker run --tty --interactive --rm \
--publish 8080:8080/tcp \ --publish 8080:8080/tcp \
--publish 8081:8081/tcp \ --publish 8081:8081/tcp \