Skip to content

Commit 108c343

Browse files
committed
Add docker build with all formatting tools
1 parent 86f88e7 commit 108c343

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed

.github/workflows/docker-release.yml

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
name: Build and push docker image
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches: [main]
7+
8+
env:
9+
CARGO_TERM_COLOR: always
10+
REGISTRY_IMAGE: ghcr.io/svix/openapi-codegen
11+
12+
jobs:
13+
build:
14+
strategy:
15+
matrix:
16+
platform: [ {runner: ubuntu-24.04, name: amd64 }, {runner: ubuntu-24.04-arm, name: arm64 } ]
17+
name: Build and publish ${{ matrix.platform.name }} docker image
18+
if: github.ref == 'refs/heads/main'
19+
runs-on: "${{ matrix.platform.runner }}"
20+
steps:
21+
- uses: actions/checkout@v4
22+
23+
- name: Login to ghcr
24+
uses: docker/login-action@v3
25+
with:
26+
registry: ghcr.io
27+
username: ${{ github.actor }}
28+
password: ${{ secrets.GITHUB_TOKEN }}
29+
30+
- name: Set up Docker Buildx
31+
uses: docker/setup-buildx-action@v3
32+
33+
- name: Build and push by digest
34+
id: build
35+
uses: docker/build-push-action@v6
36+
with:
37+
tags: ${{ env.REGISTRY_IMAGE }}
38+
file: Dockerfile.${{ matrix.platform.name }}
39+
cache-from: type=gha
40+
cache-to: type=gha
41+
platforms: linux/${{ matrix.platform.name }}
42+
outputs: type=image,push-by-digest=true,name-canonical=true,push=true
43+
44+
- name: Export digest
45+
# we create empty files with the sha256 digest of the docker image as the filename
46+
# since we did not push with a tag, the only way to identify the image is with the digest
47+
run: |
48+
mkdir -p ${{ runner.temp }}/digests
49+
digest="${{ steps.build.outputs.digest }}"
50+
touch "${{ runner.temp }}/digests/${digest#sha256:}"
51+
52+
- name: Upload digest
53+
uses: actions/upload-artifact@v4
54+
with:
55+
name: digests-${{ matrix.platform.name }}
56+
path: ${{ runner.temp }}/digests/*
57+
if-no-files-found: error
58+
retention-days: 1
59+
60+
publish-merged-manifest:
61+
if: github.ref == 'refs/heads/main'
62+
runs-on: ubuntu-24.04
63+
needs:
64+
- build
65+
steps:
66+
- uses: actions/checkout@v4
67+
68+
- name: Download digests
69+
uses: actions/download-artifact@v4
70+
with:
71+
path: ${{ runner.temp }}/digests
72+
pattern: digests-*
73+
merge-multiple: true
74+
75+
- name: Login to ghcr
76+
uses: docker/login-action@v3
77+
with:
78+
registry: ghcr.io
79+
username: ${{ github.actor }}
80+
password: ${{ secrets.GITHUB_TOKEN }}
81+
82+
- name: Set up Docker Buildx
83+
uses: docker/setup-buildx-action@v3
84+
85+
- run: echo "IMAGE_TAG=$(date +%Y%m%d)-$(git rev-parse --short "$GITHUB_SHA")" >> "$GITHUB_ENV"
86+
87+
- name: Create manifest list and push
88+
# inside the ${{ runner.temp }}/digests we downloaded empty files with the sha256 digest of the image as the filename
89+
# using printf we get the digest from the filename and we add the digest to the manifest
90+
# this is the recommend way of doing things :(
91+
# https://docs.docker.com/build/ci/github-actions/multi-platform/#distribute-build-across-multiple-runners
92+
working-directory: ${{ runner.temp }}/digests
93+
run: |
94+
docker buildx imagetools create \
95+
-t ${{ env.REGISTRY_IMAGE }}:latest \
96+
-t ${{ env.REGISTRY_IMAGE }}:${{ env.IMAGE_TAG }} \
97+
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
98+
99+
- name: Inspect image
100+
run: |
101+
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:latest

Dockerfile

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# build openapi-codegen
2+
FROM lukemathwalker/cargo-chef:latest-rust-1.85 AS chef
3+
WORKDIR /app
4+
5+
FROM chef AS planner
6+
7+
COPY Cargo.toml .
8+
COPY Cargo.lock .
9+
COPY build.rs .
10+
COPY src /app/src
11+
12+
RUN cargo chef prepare --recipe-path recipe.json
13+
14+
FROM chef AS openapi-codegen-builder
15+
16+
COPY --from=planner /app/recipe.json recipe.json
17+
18+
RUN cargo chef cook --release --recipe-path recipe.json
19+
20+
COPY Cargo.toml .
21+
COPY Cargo.lock .
22+
COPY build.rs .
23+
COPY src /app/src
24+
25+
RUN cargo build --release --bin openapi-codegen
26+
27+
# build goimports
28+
FROM golang:1.24-bookworm AS goimports-builder
29+
RUN go install golang.org/x/tools/cmd/goimports@latest
30+
31+
# build rubyfmt
32+
FROM rust:1.85 AS rubyfmt-builder
33+
WORKDIR /app
34+
35+
RUN apt-get update && \
36+
apt-get install -y --no-install-recommends ruby bison && \
37+
apt-get clean
38+
39+
RUN git clone https://github.com/fables-tales/rubyfmt.git \
40+
--recurse-submodules --shallow-submodules /app && \
41+
git checkout 71cbb4adc53d3d8b36a6f1b3dcff87865d0204b8
42+
43+
RUN cargo build --release
44+
45+
# main container
46+
FROM ubuntu:noble
47+
48+
ENV DEBIAN_FRONTEND=noninteractive
49+
50+
RUN apt-get update && \
51+
apt-get install -y --no-install-recommends curl default-jre-headless && \
52+
apt-get clean
53+
54+
# Rust
55+
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- \
56+
-y \
57+
--profile minimal \
58+
--no-modify-path \
59+
--no-update-default-toolchain \
60+
--default-toolchain nightly \
61+
--component rustfmt
62+
63+
ENV PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.cargo/bin/"
64+
65+
# Javascript
66+
COPY --from=ghcr.io/biomejs/biome:1.9.4 /usr/local/bin/biome /usr/bin/biome
67+
68+
# Python
69+
COPY --from=ghcr.io/astral-sh/ruff:0.9.8 /ruff /usr/bin/ruff
70+
71+
# Java
72+
RUN echo "25157797a0a972c2290b5bc71530c4f7ad646458025e3484412a6e5a9b8c9aa6 google-java-format-1.25.2-all-deps.jar" > google-java-format-1.25.2-all-deps.jar.sha256 && \
73+
curl -fsSL --output google-java-format-1.25.2-all-deps.jar "https://github.com/google/google-java-format/releases/download/v1.25.2/google-java-format-1.25.2-all-deps.jar" && \
74+
sha256sum google-java-format-1.25.2-all-deps.jar.sha256 -c && \
75+
rm google-java-format-1.25.2-all-deps.jar.sha256 && \
76+
mv google-java-format-1.25.2-all-deps.jar /usr/bin/ && \
77+
echo '#!/usr/bin/bash\njava -jar /usr/bin/google-java-format-1.25.2-all-deps.jar $@' > /usr/bin/google-java-format && \
78+
chmod +x /usr/bin/google-java-format
79+
80+
# Kotlin
81+
RUN echo "5e7eb28a0b2006d1cefbc9213bfc73a8191ec2f85d639ec4fc4ec0cd04212e82 ktfmt-0.54-jar-with-dependencies.jar" > ktfmt-0.54-jar-with-dependencies.jar.sha256 && \
82+
curl -fsSL --output ktfmt-0.54-jar-with-dependencies.jar "https://github.com/facebook/ktfmt/releases/download/v0.54/ktfmt-0.54-jar-with-dependencies.jar" && \
83+
sha256sum ktfmt-0.54-jar-with-dependencies.jar.sha256 -c && \
84+
rm ktfmt-0.54-jar-with-dependencies.jar.sha256 && \
85+
mv ktfmt-0.54-jar-with-dependencies.jar /usr/bin/ && \
86+
echo '#!/usr/bin/bash\njava -jar /usr/bin/ktfmt-0.54-jar-with-dependencies.jar $@' > /usr/bin/ktfmt && \
87+
chmod +x /usr/bin/ktfmt
88+
89+
# Go
90+
COPY --from=goimports-builder /go/bin/goimports /usr/bin
91+
COPY --from=goimports-builder /usr/local/go/bin/gofmt /usr/bin
92+
93+
# openapi-codegen
94+
COPY --from=openapi-codegen-builder /app/target/release/openapi-codegen /usr/bin/
95+
96+
# Ruby
97+
COPY --from=rubyfmt-builder /app/target/release/rubyfmt-main /usr/bin/rubyfmt

0 commit comments

Comments
 (0)