Skip to content

Commit e13f03b

Browse files
committed
Add Docker image: GHCR multi-arch (linux/amd64 + arm64)
Multi-stage Dockerfile (golang:1.23-alpine → distroless/static:nonroot) bakes the version string via --ldflags. GitHub Actions docker.yml workflow publishes to ghcr.io/unirate-api/unirate-cli on every main-branch push and v* tag; Docker Hub enabled by setting DOCKER_HUB_USERNAME Actions variable + DOCKER_HUB_TOKEN secret.
1 parent 9d9bc34 commit e13f03b

4 files changed

Lines changed: 105 additions & 0 deletions

File tree

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.github/
2+
examples/
3+
*.md

.github/workflows/docker.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Docker
2+
3+
on:
4+
push:
5+
branches: [main]
6+
tags: ["v*"]
7+
8+
permissions:
9+
contents: read
10+
packages: write
11+
12+
jobs:
13+
docker:
14+
name: Build and push
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Set up QEMU
20+
uses: docker/setup-qemu-action@v3
21+
22+
- name: Set up Docker Buildx
23+
uses: docker/setup-buildx-action@v3
24+
25+
- name: Log in to GHCR
26+
uses: docker/login-action@v3
27+
with:
28+
registry: ghcr.io
29+
username: ${{ github.actor }}
30+
password: ${{ secrets.GITHUB_TOKEN }}
31+
32+
- name: Log in to Docker Hub
33+
if: ${{ vars.DOCKER_HUB_USERNAME != '' }}
34+
uses: docker/login-action@v3
35+
with:
36+
username: ${{ vars.DOCKER_HUB_USERNAME }}
37+
password: ${{ secrets.DOCKER_HUB_TOKEN }}
38+
39+
# Build the image list dynamically so Docker Hub is only included when
40+
# the DOCKER_HUB_USERNAME Actions variable is set on the repo.
41+
- name: Compute image list
42+
id: imgs
43+
run: |
44+
IMGS="ghcr.io/unirate-api/unirate-cli"
45+
if [ -n "$DH_USER" ]; then
46+
IMGS="$IMGS"$'\n'"docker.io/$DH_USER/unirate-cli"
47+
fi
48+
printf 'list<<EOF\n%s\nEOF\n' "$IMGS" >> "$GITHUB_OUTPUT"
49+
env:
50+
DH_USER: ${{ vars.DOCKER_HUB_USERNAME }}
51+
52+
- name: Docker metadata
53+
id: meta
54+
uses: docker/metadata-action@v5
55+
with:
56+
images: ${{ steps.imgs.outputs.list }}
57+
tags: |
58+
type=semver,pattern={{version}}
59+
type=semver,pattern={{major}}.{{minor}}
60+
type=raw,value=latest,enable={{is_default_branch}}
61+
labels: |
62+
org.opencontainers.image.title=unirate-cli
63+
org.opencontainers.image.description=Currency exchange rates, conversion, and VAT from the UniRate API
64+
org.opencontainers.image.url=https://github.com/UniRate-API/unirate-cli
65+
org.opencontainers.image.licenses=MIT
66+
67+
- name: Build and push
68+
uses: docker/build-push-action@v6
69+
with:
70+
context: .
71+
platforms: linux/amd64,linux/arm64
72+
push: true
73+
tags: ${{ steps.meta.outputs.tags }}
74+
labels: ${{ steps.meta.outputs.labels }}
75+
build-args: |
76+
VERSION=${{ steps.meta.outputs.version }}
77+
cache-from: type=gha
78+
cache-to: type=gha,mode=max

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM golang:1.23-alpine AS builder
2+
WORKDIR /build
3+
COPY go.mod go.sum ./
4+
RUN go mod download
5+
COPY . .
6+
ARG VERSION=dev
7+
RUN CGO_ENABLED=0 GOOS=linux go build \
8+
-trimpath \
9+
-ldflags="-s -w -X main.version=${VERSION}" \
10+
-o unirate .
11+
12+
FROM gcr.io/distroless/static-debian12:nonroot
13+
COPY --from=builder /build/unirate /unirate
14+
ENTRYPOINT ["/unirate"]

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ go install github.com/UniRate-API/unirate-cli@latest
4747
**Binaries:** prebuilt archives for linux/macOS/windows (amd64 + arm64) are
4848
attached to each [GitHub release](https://github.com/UniRate-API/unirate-cli/releases).
4949

50+
**Docker** (linux/amd64 + arm64):
51+
52+
```bash
53+
docker run --rm -e UNIRATE_API_KEY="$UNIRATE_API_KEY" \
54+
ghcr.io/unirate-api/unirate-cli:latest convert 100 USD EUR
55+
```
56+
57+
Images are published to the [GitHub Container Registry](https://github.com/UniRate-API/unirate-cli/pkgs/container/unirate-cli)
58+
on every release and on pushes to `main`.
59+
5060
## Authentication
5161

5262
Every command needs an API key. Set it once in your environment:

0 commit comments

Comments
 (0)