From d3ba1a376178bdc80e448762d2745661a462da7f Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Fri, 8 May 2026 23:46:35 +0700 Subject: [PATCH] ci: add Dockerfile and GHCR multi-arch publish workflow --- .../.github/workflows/docker-publish.yml | 58 +++++++++++++++++++ self-hosted/.github/workflows/test.yml | 29 ++++++++++ self-hosted/Dockerfile | 14 +++++ 3 files changed, 101 insertions(+) create mode 100644 self-hosted/.github/workflows/docker-publish.yml create mode 100644 self-hosted/.github/workflows/test.yml create mode 100644 self-hosted/Dockerfile diff --git a/self-hosted/.github/workflows/docker-publish.yml b/self-hosted/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..a979556 --- /dev/null +++ b/self-hosted/.github/workflows/docker-publish.yml @@ -0,0 +1,58 @@ +name: docker-publish + +on: + push: + branches: [main] + tags: ['v*'] + workflow_dispatch: + +permissions: + contents: read + packages: write + id-token: write + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: docker/setup-qemu-action@v3 + + - uses: docker/setup-buildx-action@v3 + + - name: Log in to GHCR + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Derive image metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=raw,value=latest,enable={{is_default_branch}} + type=sha,format=short,prefix=main-,enable={{is_default_branch}} + + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/self-hosted/.github/workflows/test.yml b/self-hosted/.github/workflows/test.yml new file mode 100644 index 0000000..32e4433 --- /dev/null +++ b/self-hosted/.github/workflows/test.yml @@ -0,0 +1,29 @@ +name: test + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-go@v5 + with: + go-version: '1.22' + cache: true + + - name: vet + run: go vet ./... + + - name: test + run: go test -race -cover ./... + + - name: build + run: go build ./... diff --git a/self-hosted/Dockerfile b/self-hosted/Dockerfile new file mode 100644 index 0000000..318b03f --- /dev/null +++ b/self-hosted/Dockerfile @@ -0,0 +1,14 @@ +# syntax=docker/dockerfile:1.7 +FROM --platform=$BUILDPLATFORM golang:1.22-alpine AS build +ARG TARGETOS +ARG TARGETARCH +WORKDIR /src +COPY go.mod go.sum ./ +RUN go mod download +COPY . . +RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \ + go build -trimpath -ldflags="-s -w" -o /out/app . + +FROM gcr.io/distroless/static-debian12:nonroot +COPY --from=build /out/app /app +ENTRYPOINT ["/app"]