diff --git a/.github/workflows/build-image.yaml b/.github/workflows/build-image.yaml new file mode 100644 index 0000000..d61b9c0 --- /dev/null +++ b/.github/workflows/build-image.yaml @@ -0,0 +1,59 @@ +name: build-image + +on: + push: + branches: [main] + tags: ["v*.*.*"] + pull_request: + branches: [main] + workflow_dispatch: + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - uses: actions/checkout@v4 + + - uses: docker/setup-qemu-action@v3 + + - uses: docker/setup-buildx-action@v3 + + - name: Log in to GHCR + if: github.event_name != 'pull_request' + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Compute tags and labels + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=sha,format=long + type=raw,value=latest,enable={{is_default_branch}} + + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: . + file: Dockerfile + 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/Dockerfile b/Dockerfile new file mode 100644 index 0000000..19645b7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +# Multi-arch image for ts-plug. Cross-compiles Go on BUILDPLATFORM and +# ships an alpine runtime. +# +# docker buildx build --platform linux/amd64,linux/arm64 \ +# -t ghcr.io/tailscale/ts-plug:latest -f Dockerfile --push . +FROM --platform=$BUILDPLATFORM golang:1.26-alpine AS build +ARG TARGETOS +ARG TARGETARCH +# Installed on the builder so we can COPY the bundle to the runtime image +# without a RUN there — TARGETPLATFORM RUN needs QEMU registered on the host. +RUN apk add --no-cache ca-certificates +WORKDIR /src +COPY go.mod go.sum ./ +RUN go mod download +COPY . . +RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \ + go build -ldflags='-s -w' -o /out/ts-plug ./cmd/ts-multi-plug + +FROM alpine:3.20 +COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt +COPY --from=build /out/ts-plug /usr/local/bin/ts-plug +ENTRYPOINT ["/usr/local/bin/ts-plug"]