Skip to content

Commit 581f1fa

Browse files
authored
feat: Break up build process into separate jobs to improve caching in matrix strategy (#63)
1 parent cf4e489 commit 581f1fa

File tree

4 files changed

+189
-15
lines changed

4 files changed

+189
-15
lines changed

.github/workflows/build.yml

+161-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,165 @@ on:
1616
env:
1717
IMAGE_REGISTRY: ghcr.io/${{ github.repository_owner }}
1818
jobs:
19+
build-akmods:
20+
name: Build akmods package
21+
runs-on: ubuntu-22.04
22+
permissions:
23+
contents: read
24+
packages: write
25+
id-token: write
26+
strategy:
27+
fail-fast: false
28+
matrix:
29+
major_version: [37, 38]
30+
driver_version: [520, 525, 470]
31+
exclude:
32+
- driver_version: 520
33+
major_version: 38
34+
- driver_version: 470
35+
major_version: 38
36+
steps:
37+
# Checkout push-to-registry action GitHub repository
38+
- name: Checkout Push to Registry action
39+
uses: actions/checkout@v3
40+
41+
- name: Matrix Variables
42+
run: |
43+
REPO=${{ github.repository }}
44+
echo "IMAGE_NAME=akmods-${REPO##*/}" >> $GITHUB_ENV
45+
- name: Generate tags
46+
id: generate-tags
47+
shell: bash
48+
run: |
49+
# Generate a timestamp for creating an image version history
50+
TIMESTAMP="$(date +%Y%m%d)"
51+
VARIANT="${{ matrix.major_version }}-${{ matrix.driver_version }}"
52+
53+
COMMIT_TAGS=()
54+
BUILD_TAGS=()
55+
56+
# Have tags for tracking builds during pull request
57+
SHA_SHORT="$(git rev-parse --short HEAD)"
58+
COMMIT_TAGS+=("pr-${{ github.event.number }}-${VARIANT}")
59+
COMMIT_TAGS+=("${SHA_SHORT}-${VARIANT}")
60+
61+
BUILD_TAGS=("${VARIANT}" "${VARIANT}-${TIMESTAMP}")
62+
63+
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
64+
echo "Generated the following commit tags: "
65+
for TAG in "${COMMIT_TAGS[@]}"; do
66+
echo "${TAG}"
67+
done
68+
69+
alias_tags=("${COMMIT_TAGS[@]}")
70+
else
71+
alias_tags=("${BUILD_TAGS[@]}")
72+
fi
73+
74+
echo "Generated the following build tags: "
75+
for TAG in "${BUILD_TAGS[@]}"; do
76+
echo "${TAG}"
77+
done
78+
79+
echo "alias_tags=${alias_tags[*]}" >> $GITHUB_OUTPUT
80+
81+
- name: Retrieve akmods signing key
82+
run: |
83+
mkdir -p certs
84+
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
85+
echo "Using test signing key"
86+
else
87+
echo "${{ secrets.AKMOD_PRIVKEY }}" > certs/private_key.priv
88+
fi
89+
# DEBUG: get character count of key
90+
wc -c certs/private_key.priv
91+
92+
# Build metadata
93+
- name: Image Metadata
94+
uses: docker/metadata-action@v4
95+
id: meta
96+
with:
97+
images: |
98+
${{ env.IMAGE_NAME }}
99+
labels: |
100+
org.opencontainers.image.title=${{ env.IMAGE_NAME }}
101+
org.opencontainers.image.description=ublue-os ${{ env.IMAGE_NAME }} with akmods-nvidia packages pre-built
102+
io.artifacthub.package.readme-url=https://raw.githubusercontent.com/${{ github.repository }}/main/README.md
103+
io.artifacthub.package.logo-url=https://avatars.githubusercontent.com/u/1728152?s=200&v=4
104+
# Build image using Buildah action
105+
- name: Build Image
106+
id: build_image
107+
uses: redhat-actions/buildah-build@v2
108+
with:
109+
containerfiles: |
110+
./build.Containerfile
111+
image: ${{ env.IMAGE_NAME }}
112+
tags: |
113+
${{ steps.generate-tags.outputs.alias_tags }}
114+
build-args: |
115+
IMAGE_NAME=base
116+
FEDORA_MAJOR_VERSION=${{ matrix.major_version }}
117+
NVIDIA_MAJOR_VERSION=${{ matrix.driver_version }}
118+
labels: ${{ steps.meta.outputs.labels }}
119+
oci: false
120+
121+
# Workaround bug where capital letters in your GitHub username make it impossible to push to GHCR.
122+
# https://github.com/macbre/push-to-ghcr/issues/12
123+
- name: Lowercase Registry
124+
id: registry_case
125+
uses: ASzc/change-string-case-action@v5
126+
with:
127+
string: ${{ env.IMAGE_REGISTRY }}
128+
129+
# Push the image to GHCR (Image Registry)
130+
- name: Push To GHCR
131+
uses: redhat-actions/push-to-registry@v2
132+
id: push
133+
env:
134+
REGISTRY_USER: ${{ github.actor }}
135+
REGISTRY_PASSWORD: ${{ github.token }}
136+
with:
137+
image: ${{ steps.build_image.outputs.image }}
138+
tags: ${{ steps.build_image.outputs.tags }}
139+
registry: ${{ steps.registry_case.outputs.lowercase }}
140+
username: ${{ env.REGISTRY_USER }}
141+
password: ${{ env.REGISTRY_PASSWORD }}
142+
extra-args: |
143+
--disable-content-trust
144+
145+
# Sign container
146+
- uses: sigstore/cosign-installer@v3.0.1
147+
148+
# Only needed when running `cosign sign` using a key
149+
- name: Write signing key to disk
150+
run: |
151+
echo "${{ env.COSIGN_PRIVATE_KEY }}" > cosign.key
152+
# DEBUG: get character count of key
153+
wc -c cosign.key
154+
env:
155+
COSIGN_PRIVATE_KEY: ${{ secrets.SIGNING_SECRET }}
156+
157+
- name: Login to GitHub Container Registry
158+
uses: docker/login-action@v2
159+
with:
160+
registry: ghcr.io
161+
username: ${{ github.actor }}
162+
password: ${{ secrets.GITHUB_TOKEN }}
163+
164+
- name: Sign container image
165+
run: |
166+
cosign sign -y --key cosign.key ${{ steps.registry_case.outputs.lowercase }}/${{ steps.build_image.outputs.image }}@${TAGS}
167+
env:
168+
TAGS: ${{ steps.push.outputs.digest }}
169+
COSIGN_EXPERIMENTAL: false
170+
171+
- name: Echo outputs
172+
run: |
173+
echo "${{ toJSON(steps.push.outputs) }}"
174+
19175
push-ghcr:
20176
name: Build and push image
177+
needs: build-akmods
21178
runs-on: ubuntu-22.04
22179
permissions:
23180
contents: read
@@ -108,8 +265,10 @@ jobs:
108265
done
109266
110267
alias_tags=("${COMMIT_TAGS[@]}")
268+
echo "AKMODS_VERSION=pr-${{ github.event.number }}-${{ matrix.major_version }}" >> $GITHUB_ENV
111269
else
112270
alias_tags=("${BUILD_TAGS[@]}")
271+
echo "AKMODS_VERSION=${{ matrix.major_version }}" >> $GITHUB_ENV
113272
fi
114273
115274
echo "Generated the following build tags: "
@@ -148,12 +307,13 @@ jobs:
148307
uses: redhat-actions/buildah-build@v2
149308
with:
150309
containerfiles: |
151-
./Containerfile
310+
./install.Containerfile
152311
image: ${{ env.IMAGE_NAME }}
153312
tags: |
154313
${{ steps.generate-tags.outputs.alias_tags }}
155314
build-args: |
156315
IMAGE_NAME=${{ matrix.image_name }}
316+
AKMODS_VERSION=${{ env.AKMODS_VERSION }}
157317
FEDORA_MAJOR_VERSION=${{ matrix.major_version }}
158318
NVIDIA_MAJOR_VERSION=${{ matrix.driver_version }}
159319
labels: ${{ steps.meta.outputs.labels }}

Containerfile build.Containerfile

+2-12
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,8 @@ ADD files/etc/sway/environment /tmp/ublue-os-nvidia-addons/rpmbuild/SOURCES/envi
2525

2626
RUN /tmp/build.sh
2727

28-
FROM ${BASE_IMAGE}:${FEDORA_MAJOR_VERSION}
29-
30-
ARG IMAGE_NAME="${IMAGE_NAME}"
28+
FROM scratch
3129

30+
COPY --from=builder /var/cache /var/cache
3231
COPY --from=builder /tmp/ublue-os /tmp/ublue-os
33-
COPY --from=builder /var/cache/akmods /tmp/akmods
3432
COPY --from=builder /tmp/ublue-os-nvidia-addons /tmp/ublue-os-nvidia-addons
35-
36-
COPY install.sh /tmp/install.sh
37-
COPY post-install.sh /tmp/post-install.sh
38-
RUN /tmp/install.sh
39-
RUN /tmp/post-install.sh
40-
RUN rm -rf /tmp/* /var/*
41-
RUN ostree container commit
42-
RUN mkdir -p /var/tmp && chmod -R 1777 /var/tmp

install.Containerfile

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
ARG IMAGE_NAME="${IMAGE_NAME:-silverblue}"
2+
ARG BASE_IMAGE="ghcr.io/ublue-os/${IMAGE_NAME}-main"
3+
ARG FEDORA_MAJOR_VERSION="${FEDORA_MAJOR_VERSION:-37}"
4+
ARG NVIDIA_MAJOR_VERSION="${NVIDIA_MAJOR_VERSION:-525}"
5+
ARG AKMODS_VERSION="${AKMODS_VERSION:-37}"
6+
7+
FROM ${BASE_IMAGE}:${FEDORA_MAJOR_VERSION}
8+
9+
ARG FEDORA_MAJOR_VERSION="${FEDORA_MAJOR_VERSION}"
10+
ARG NVIDIA_MAJOR_VERSION="${NVIDIA_MAJOR_VERSION}"
11+
ARG AKMODS_VERSION="${AKMODS_VERSION}"
12+
13+
ARG BUILDER_IMAGE="ghcr.io/ublue-os/akmods-nvidia:${AKMODS_VERSION}-${NVIDIA_MAJOR_VERSION}"
14+
ARG IMAGE_NAME="${IMAGE_NAME}"
15+
16+
COPY --from=${BUILDER_IMAGE} / .
17+
18+
COPY install.sh /tmp/install.sh
19+
COPY post-install.sh /tmp/post-install.sh
20+
RUN /tmp/install.sh
21+
RUN /tmp/post-install.sh
22+
RUN rm -rf /tmp/* /var/*
23+
RUN ostree container commit
24+
RUN mkdir -p /var/tmp && chmod -R 1777 /var/tmp

install.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ sed -i 's@enabled=1@enabled=0@g' /etc/yum.repos.d/fedora-{cisco-openh264,modular
77
install -D /tmp/ublue-os-nvidia-addons/rpmbuild/SOURCES/nvidia-container-runtime.repo \
88
/etc/yum.repos.d/nvidia-container-runtime.repo
99

10-
source /tmp/akmods/nvidia-vars
10+
source /var/cache/akmods/nvidia-vars
1111

1212
rpm-ostree install \
1313
xorg-x11-drv-${NVIDIA_PACKAGE_NAME}-{,cuda-,devel-,kmodsrc-,power-}${NVIDIA_FULL_VERSION} \
1414
nvidia-container-toolkit nvidia-vaapi-driver \
15-
/tmp/akmods/${NVIDIA_PACKAGE_NAME}/kmod-${NVIDIA_PACKAGE_NAME}-${KERNEL_VERSION}-${NVIDIA_AKMOD_VERSION}.fc${RELEASE}.rpm \
15+
/var/cache/akmods/${NVIDIA_PACKAGE_NAME}/kmod-${NVIDIA_PACKAGE_NAME}-${KERNEL_VERSION}-${NVIDIA_AKMOD_VERSION}.fc${RELEASE}.rpm \
1616
/tmp/ublue-os-nvidia-addons/rpmbuild/RPMS/noarch/ublue-os-nvidia-addons-*.rpm \
1717
/tmp/ublue-os/rpmbuild/RPMS/noarch/ublue-os-just-*.noarch.rpm

0 commit comments

Comments
 (0)