Skip to content

Commit 6081232

Browse files
committed
build: generate docker images with arch variants
1 parent 84c3c42 commit 6081232

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

.github/workflows/release.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ jobs:
4949
echo "$MATRIX"
5050
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT
5151
52+
ARCH_LIST=$(echo "$MATRIX" | jq -r '.[].arch' | jq -R -s -c 'split("\n") | map(select(. != ""))')
53+
echo "arch_list=$ARCH_LIST" >> $GITHUB_OUTPUT
54+
5255
build:
5356
runs-on: ubuntu-latest
5457
needs: generate-matrix
@@ -132,3 +135,63 @@ jobs:
132135
files: |
133136
artifacts/*
134137
CHANGES.md
138+
139+
docker-build:
140+
runs-on: ubuntu-latest
141+
needs: [generate-matrix]
142+
strategy:
143+
fail-fast: false
144+
matrix:
145+
include: ${{ fromJson(needs.generate-matrix.outputs.matrix) }}
146+
env:
147+
VERSION: ${{ github.event.inputs.release_name || github.ref_name }}
148+
steps:
149+
- uses: actions/checkout@v4
150+
151+
- name: Install Nix
152+
uses: cachix/install-nix-action@v30
153+
with:
154+
nix_path: nixpkgs=channel:nixos-unstable
155+
github_access_token: ${{ secrets.GITHUB_TOKEN }}
156+
extra_nix_config: |
157+
experimental-features = nix-command flakes
158+
159+
- name: Build individual images with Nix
160+
run: |
161+
nix build .#image-${{ matrix.arch }}
162+
docker load < ./result
163+
docker tag grhooks:${{ env.VERSION }} ghcr.io/${{ github.repository_owner }}/grhooks:${{ matrix.arch }}
164+
165+
- name: Create and push unified manifest
166+
run: |
167+
docker manifest create ghcr.io/${{ github.repository_owner }}/grhooks:${{ env.VERSION }} \
168+
--amend ghcr.io/${{ github.repository_owner }}/grhooks:${{ matrix.arch }}
169+
170+
docker-publish:
171+
runs-on: ubuntu-latest
172+
needs: [generate-matrix, docker-build]
173+
env:
174+
VERSION: ${{ github.event.inputs.release_name || github.ref_name }}
175+
steps:
176+
- name: Log in to GHCR
177+
uses: docker/login-action@v3
178+
with:
179+
registry: ghcr.io
180+
username: ${{ github.actor }}
181+
password: ${{ secrets.GITHUB_TOKEN }}
182+
183+
- name: Create and push manifest
184+
run: |
185+
IMAGE=ghcr.io/${{ github.repository_owner }}/grhooks:${{ env.VERSION }}
186+
ARCHS='${{ needs.generate-matrix.outputs.arch_list }}'
187+
188+
echo "Creating manifest for architectures: $ARCHS"
189+
190+
manifest_args=""
191+
for arch in $(echo "$ARCHS" | jq -r '.[]'); do
192+
manifest_args="$manifest_args --amend ghcr.io/${{ github.repository_owner }}/grhooks:$arch"
193+
done
194+
195+
echo "Running docker manifest create $IMAGE $manifest_args"
196+
eval docker manifest create $IMAGE $manifest_args
197+
docker manifest push $IMAGE

flake.nix

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
outputs = { nixpkgs, flake-utils, rust-overlay, ... }:
1212
flake-utils.lib.eachDefaultSystem (baseSystem:
1313
let
14+
cargoManifest = builtins.fromTOML (builtins.readFile ./Cargo.toml);
1415
overlays = [ (import rust-overlay) ];
1516
pkgs = import nixpkgs {
1617
system = baseSystem;
@@ -67,6 +68,19 @@
6768
++ lib.optionals (os == "macos") [ clang darwin.apple_sdk.frameworks.CoreFoundation ];
6869
};
6970

71+
containerPkg = variant: let
72+
pkg = mkPackage variant;
73+
# arch = if variant.arch == "x86_64" then "amd" else "arm";
74+
in pkgs.dockerTools.buildLayeredImage rec {
75+
name = cargoManifest.package.name;
76+
tag = cargoManifest.package.version;
77+
created = "now";
78+
# architecture = "linux/${arch}64";
79+
80+
contents = [ pkg ];
81+
config.Cmd = ["/bin/${name}"];
82+
};
83+
7084
generatedMatrixJson = builtins.toJSON (lib.flatten (map ({ arch, os, formats, ... }:
7185
map (format: {
7286
arch = arch;
@@ -96,7 +110,10 @@
96110
packages = lib.listToAttrs (map ({ arch, os, target, ... }: {
97111
name = "${os}-${arch}";
98112
value = mkPackage { inherit arch os target; };
99-
}) architectures);
113+
}) architectures) // (pkgs.lib.listToAttrs (map ({arch, ...} @ args: {
114+
name = "image-${arch}";
115+
value = containerPkg args;
116+
}) architectures));
100117

101118
apps = {
102119
help = {
@@ -112,6 +129,8 @@
112129
echo -e "\033[0;35mAvailable OS:\033[0m"
113130
${lib.concatMapStringsSep "\n" (os: ''echo " - ${os}"'') (lib.lists.unique (map ({ os, ... }: os) architectures))}
114131
echo ""
132+
echo -e "\033[0;32mTo build docker image, use:\033[0m"
133+
echo " nix build .#image-<arch>"
115134
echo -e "\033[0;32mTo build a specific variant, use:\033[0m"
116135
echo " nix build .#<os>-<arch>"
117136
echo ""

0 commit comments

Comments
 (0)