Skip to content

Commit 21038be

Browse files
authored
.github/workflows: add containers.yaml (#46)
Add a GH action to build and publish a docker container when a new tag is pushed. Signed-off-by: Benson Wong <[email protected]>
1 parent e6bf0ba commit 21038be

File tree

2 files changed

+88
-7
lines changed

2 files changed

+88
-7
lines changed

.github/workflows/containers.yaml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Publish Containers
2+
3+
permissions:
4+
contents: read
5+
packages: write
6+
7+
on:
8+
push:
9+
tags:
10+
- '*'
11+
12+
# Allows manual triggering of the workflow
13+
workflow_dispatch:
14+
inputs:
15+
tag:
16+
description: 'Tag release (e.g. v1.2.3)'
17+
required: true
18+
19+
# allow for testing of PR updating this file
20+
pull_request:
21+
paths:
22+
- ".github/workflows/containers.yaml"
23+
24+
jobs:
25+
build-and-push:
26+
runs-on: ubuntu-latest
27+
steps:
28+
- name: Checkout code
29+
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # 4.3.0
30+
with:
31+
fetch-depth: 0
32+
ref: ${{ github.event.inputs.tag || github.ref }}
33+
34+
- name: Log in to GitHub Container Registry
35+
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1 # 3.5.0
36+
with:
37+
registry: ghcr.io
38+
username: ${{ github.actor }}
39+
password: ${{ secrets.GITHUB_TOKEN }}
40+
41+
- name: Determine docker image tags
42+
id: image_tags
43+
run: |
44+
# For PRs to this file tag the container "pull_request_test"
45+
if [ "${{ github.event_name }}" = "pull_request" ]; then
46+
echo "tags=ghcr.io/tailscale/tsidp:pull_request_test" >> $GITHUB_OUTPUT
47+
exit 0
48+
fi
49+
50+
# For tag push: use the tag name and also push "latest"
51+
if [ "${{ github.event_name }}" = "push" ]; then
52+
REF="${{ github.ref }}"
53+
TAG="${REF#refs/tags/}"
54+
echo "tags=ghcr.io/tailscale/tsidp:${TAG},ghcr.io/tailscale/tsidp:latest" >> $GITHUB_OUTPUT
55+
exit 0
56+
fi
57+
58+
# For workflow_dispatch: use the provided tag
59+
TAG="${{ github.event.inputs.tag }}"
60+
echo "tags=ghcr.io/tailscale/tsidp:${TAG}" >> $GITHUB_OUTPUT
61+
62+
- name: Set up Docker Buildx
63+
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # 3.11.1
64+
65+
- name: Build and push
66+
uses: docker/build-push-action@v5
67+
with:
68+
context: .
69+
file: ./Dockerfile
70+
push: ${{ github.event_name != 'pull_request' }}
71+
tags: ${{ steps.image_tags.outputs.tags }}
72+
platforms: linux/amd64,linux/arm64

Dockerfile

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
1-
# Build stage
2-
FROM golang:1.24-alpine AS builder
1+
#
2+
# local testing build:
3+
# > docker build -t tsidp-server:local .
4+
#
5+
# To build for a container for linux/amd64:
6+
# docker buildx build --platform linux/amd64 -t tsidp-server:amd64 --load .
37

8+
# Build stage
9+
FROM --platform=$BUILDPLATFORM golang:1.24-alpine AS builder
410
WORKDIR /app
511

12+
# BuildKit will set these automatically when using buildx
13+
ARG TARGETOS
14+
ARG TARGETARCH
15+
616
# Copy go mod files from root
717
COPY go.mod go.sum ./
8-
918
# Download dependencies
1019
RUN go mod download
11-
1220
# Copy the entire project
1321
COPY . ./
1422

15-
# Build the binary from the server directory
16-
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o tsidp-server .
23+
# Build the binary for the target platform
24+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} \
25+
go build -a -installsuffix cgo -o tsidp-server .
1726

1827
# Final stage
19-
FROM alpine:latest
28+
FROM --platform=$TARGETPLATFORM alpine:latest
2029
RUN apk --no-cache add ca-certificates
2130
WORKDIR /app
2231

0 commit comments

Comments
 (0)