Skip to content

Commit 0d35ee3

Browse files
author
Simen Andrè Vikestrand Skogum
committed
feat: add GitHub Actions workflow for PR builds of changed Dockerfiles
1 parent 31245b2 commit 0d35ee3

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

.github/workflows/pr_build.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: PR Build (changed Dockerfiles only)
2+
3+
on:
4+
pull_request:
5+
branches: [ dev ]
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
detect:
12+
runs-on: ubuntu-latest
13+
outputs:
14+
matrix: ${{ steps.set.outputs.matrix }}
15+
count: ${{ steps.set.outputs.count }}
16+
steps:
17+
- uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
21+
- id: set
22+
shell: bash
23+
run: |
24+
set -euo pipefail
25+
base="${{ github.event.pull_request.base.sha }}"
26+
head="${{ github.sha }}"
27+
28+
# List changed Dockerfiles under the monorepo
29+
mapfile -t dockerfiles < <(git diff --name-only "$base" "$head" -- 'salamicontainers/**/Dockerfile' | sort -u)
30+
31+
if [ "${#dockerfiles[@]}" -eq 0 ]; then
32+
echo "No Dockerfiles changed."
33+
echo "matrix={\"include\":[]}" >> "$GITHUB_OUTPUT"
34+
echo "count=0" >> "$GITHUB_OUTPUT"
35+
exit 0
36+
fi
37+
38+
includes="[]"
39+
for f in "${dockerfiles[@]}"; do
40+
dir="$(dirname "$f")"
41+
ver="0.0.0-dev"
42+
if [ -f "$dir/VERSION" ]; then
43+
ver="$(tr -d '\n\r\t ' < "$dir/VERSION")"
44+
fi
45+
includes="$(jq -c --arg dir "$dir" --arg df "$f" --arg ver "$ver" \
46+
'. + [{dir:$dir,dockerfile:$df,version:$ver}]' <<<"$includes")"
47+
done
48+
49+
echo "matrix=$(jq -c '{include: .}' <<<"$includes")" >> "$GITHUB_OUTPUT"
50+
echo "count=$(jq -r 'length' <<<"$includes")" >> "$GITHUB_OUTPUT"
51+
52+
- name: Show detected changes
53+
run: |
54+
echo "Count: ${{ steps.set.outputs.count }}"
55+
echo '${{ steps.set.outputs.matrix }}' | jq .
56+
57+
build:
58+
needs: detect
59+
if: ${{ fromJson(needs.detect.outputs.count) > 0 }}
60+
runs-on: ubuntu-latest
61+
strategy:
62+
fail-fast: false
63+
matrix: ${{ fromJson(needs.detect.outputs.matrix) }}
64+
steps:
65+
- uses: actions/checkout@v4
66+
with:
67+
fetch-depth: 0
68+
- uses: docker/setup-qemu-action@v3
69+
- uses: docker/setup-buildx-action@v3
70+
71+
- name: Build ${{ matrix.dockerfile }} (no push)
72+
run: |
73+
docker buildx build \
74+
--platform linux/amd64,linux/arm64 \
75+
-f "${{ matrix.dockerfile }}" \
76+
--build-arg APP_VERSION="${{ matrix.version }}" \
77+
--progress=plain \
78+
"${{ matrix.dir }}"

0 commit comments

Comments
 (0)