Skip to content

Commit 5f1e457

Browse files
authored
Build container image with github actions (#1268)
Just the bare minimum to build the same container image that we got from cloud build via a github action and push it to `ghcr.io/embarkstudios/quilkin`. I've bypassed `build/Makefile` entirely and only picked out the relevant steps, but leaving all the old stuff as is for now. I created a new `Dockerfile.ghaction` that runs some of the old make targets in a dockerfile multi stage build instead as that works better with `docker/build-push-action`. The `build-env` image is not saved in a remote registry but should be cached between runs in the gha cache. The action should now run on * any push to main * any new tag (without `/` in the name) * triggering the workflow manually (can't test this until the workflow is on main) One image pushed so far: https://github.com/EmbarkStudios/quilkin/pkgs/container/quilkin, same size as the last cloud build image and still has the commit sha embedded in the binary.
1 parent 7d0ba83 commit 5f1e457

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

.github/workflows/image.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: Build Container Image
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
tags:
8+
- '*'
9+
workflow_dispatch:
10+
11+
jobs:
12+
build-image:
13+
runs-on: ubuntu-latest
14+
# Local registry needed to share the build-env image between steps when
15+
# using the docker-container buildkit driver
16+
services:
17+
registry:
18+
image: registry:2
19+
ports:
20+
- 5000:5000
21+
steps:
22+
- uses: docker/login-action@v3
23+
with:
24+
registry: ghcr.io
25+
username: ${{ github.repository_owner }}
26+
password: ${{ secrets.GITHUB_TOKEN }}
27+
28+
- uses: actions/checkout@v4
29+
30+
- name: Gather information
31+
id: info
32+
run: |
33+
echo "toolchain_version=$(yq '.toolchain.channel' --output-format=yaml rust-toolchain.toml)" >> $GITHUB_OUTPUT
34+
echo "crate_version=$(yq '.package.version' --output-format=yaml Cargo.toml)" >> $GITHUB_OUTPUT
35+
echo "git_timestamp=$(git log -1 --pretty=%ct)" >> $GITHUB_OUTPUT
36+
37+
echo "## Build information" >> $GITHUB_STEP_SUMMARY
38+
echo "| | |" >> $GITHUB_STEP_SUMMARY
39+
echo "| --- | --- |" >> $GITHUB_STEP_SUMMARY
40+
cat $GITHUB_OUTPUT | sed 's/\(.*\)=\(.*\)/| \1 | \2 |/' >> $GITHUB_STEP_SUMMARY
41+
42+
- uses: docker/setup-buildx-action@v3
43+
with:
44+
# network=host driver-opt needed to push to local registry
45+
driver-opts: network=host
46+
47+
- name: Prepare build environment image
48+
uses: docker/build-push-action@v6
49+
with:
50+
context: ./build/build-image
51+
build-args: RUST_TOOLCHAIN=${{ steps.info.outputs.toolchain_version }}
52+
cache-from: type=gha,scope=build-env
53+
cache-to: type=gha,mode=max,scope=build-env
54+
push: true
55+
tags: localhost:5000/build-env:latest
56+
57+
- name: Gather image metadata
58+
id: meta
59+
uses: docker/metadata-action@v5
60+
with:
61+
images: |
62+
ghcr.io/embarkstudios/quilkin
63+
tags: |
64+
type=ref,event=tag
65+
type=sha,prefix=${{ steps.info.outputs.crate_version }}-
66+
67+
- name: Build container image
68+
uses: docker/build-push-action@v6
69+
with:
70+
# build.rs runs git commands, need to preserve .git directory
71+
# https://docs.docker.com/reference/dockerfile/#buildkit-built-in-build-args
72+
build-args: BUILDKIT_CONTEXT_KEEP_GIT_DIR=1
73+
file: image/Dockerfile.ghaction
74+
build-contexts: |
75+
build-env=docker-image://localhost:5000/build-env:latest
76+
cache-from: type=gha,scope=quilkin
77+
cache-to: type=gha,mode=max,scope=quilkin
78+
push: true
79+
tags: ${{ steps.meta.outputs.tags }}
80+
annotations: ${{ steps.meta.outputs.annotations }}
81+
env:
82+
# Set the build timestamp to the commit timestamp, so we can
83+
# hopefully get reproducible builds when built from the same commit
84+
# https://github.com/moby/buildkit/blob/master/docs/build-repro.md#source_date_epoch
85+
SOURCE_DATE_EPOCH: ${{ steps.info.outputs.git_timestamp }}

image/Dockerfile.ghaction

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM build-env AS builder
2+
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=/usr/bin/x86_64-linux-gnu-gcc
3+
COPY . /workspace
4+
WORKDIR /workspace
5+
RUN cargo about generate license.html.hbs > license.html
6+
RUN cargo run -p proto-gen -- generate
7+
RUN cargo build --profile=lto --target x86_64-unknown-linux-gnu
8+
RUN ./image/archive_dependencies.sh
9+
10+
FROM gcr.io/distroless/cc-debian12:nonroot AS base
11+
WORKDIR /
12+
COPY --from=builder /workspace/license.html .
13+
COPY --from=builder /workspace/dependencies-src.zip .
14+
COPY --from=builder --chown=nonroot:nonroot /workspace/target/x86_64-unknown-linux-gnu/lto/quilkin .
15+
16+
USER nonroot:nonroot
17+
ENTRYPOINT ["/quilkin"]

0 commit comments

Comments
 (0)