-
-
Notifications
You must be signed in to change notification settings - Fork 24
179 lines (158 loc) · 5.83 KB
/
docker-image.yml
File metadata and controls
179 lines (158 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
name: Build and Push Multi-Arch Docker Images
on:
push:
branches: [ main ]
paths-ignore:
- '**.md'
- 'docs/**'
release:
types: [ published ]
env:
DOCKER_TAG: ${{ github.ref_type == 'tag' && github.ref_name || 'latest' }}
jobs:
detect-changes:
runs-on: ubuntu-latest
outputs:
services: ${{ steps.set-services.outputs.services }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- id: set-services
run: |
set -e
SERVICES=()
BASE_REF="${{ github.event.before }}"
if [ -z "$BASE_REF" ]; then
BASE_REF="$(git rev-parse HEAD~1)"
fi
CHANGED=$(git diff --name-only "$BASE_REF" HEAD)
echo "Changed files:"
echo "$CHANGED"
declare -A MAP=(
["plc"]="plc/"
["router"]="router/"
["workstation"]="workstation/"
["simulation"]="simulation/"
["scadalts"]="scadalts/"
["attacker"]="attacker/"
["caldera"]="caldera/"
)
# rebuild everything on release
if [ "${{ github.event_name }}" = "release" ]; then
SERVICES=("${!MAP[@]}")
else
for SERVICE in "${!MAP[@]}"; do
if echo "$CHANGED" | grep -q "^${MAP[$SERVICE]}"; then
SERVICES+=("$SERVICE")
fi
done
# If shared files changed, rebuild everything
if echo "$CHANGED" | grep -Eq "^(docker-compose.yml|\.github/)"; then
SERVICES=("${!MAP[@]}")
fi
fi
echo "services=$(printf '%s\n' "${SERVICES[@]}" | jq -R . | jq -cs .)" >> "$GITHUB_OUTPUT"
# ================================================================
# Build AMD64 images
# ================================================================
build-amd64:
needs: detect-changes
if: needs.detect-changes.outputs.services != '[]'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
lfs: true
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push amd64 images
run: |
SERVICES=$(echo '${{ needs.detect-changes.outputs.services }}' | jq -r '.[]')
set -euo pipefail
for SERVICE in $SERVICES; do
if [ ! -d "./${SERVICE}" ]; then
echo "⚠️ Skipping ${SERVICE}: directory does not exist"
continue
fi
IMAGE="${{ secrets.DOCKERHUB_USERNAME }}/grfics-${SERVICE}"
echo "🚀 Building ${IMAGE}:${DOCKER_TAG} (amd64)"
docker buildx build \
--platform linux/amd64 \
--tag "${IMAGE}:amd64" \
--push \
--cache-from type=gha \
--cache-to type=gha,mode=max \
"./${SERVICE}"
echo "🧹 Cleaning local build cache to free space..."
docker builder prune -af || true
docker system prune -af || true
done
# ================================================================
# Build ARM64 images
# ================================================================
build-arm64:
needs: detect-changes
if: needs.detect-changes.outputs.services != '[]'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
lfs: true
- uses: docker/setup-qemu-action@v3 # enables ARM emulation
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push arm64 images
run: |
SERVICES=$(echo '${{ needs.detect-changes.outputs.services }}' | jq -r '.[]')
set -euo pipefail
for SERVICE in $SERVICES; do
if [ ! -d "./${SERVICE}" ]; then
echo "⚠️ Skipping ${SERVICE}: directory does not exist"
continue
fi
IMAGE="${{ secrets.DOCKERHUB_USERNAME }}/grfics-${SERVICE}"
echo "🚀 Building ${IMAGE}:${DOCKER_TAG} (arm64)"
docker buildx build \
--platform linux/arm64 \
--tag "${IMAGE}:arm64" \
--push \
--cache-from type=gha \
--cache-to type=gha,mode=max \
"./${SERVICE}"
echo "🧹 Cleaning local build cache to free space..."
docker builder prune -af || true
docker system prune -af || true
done
# ================================================================
# Combine manifests once both architectures exist
# ================================================================
create-manifests:
runs-on: ubuntu-latest
needs: [ detect-changes, build-amd64, build-arm64 ]
if: needs.detect-changes.outputs.services != '[]'
steps:
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Create and push multi-arch manifests
run: |
set -euo pipefail
SERVICES=$(echo '${{ needs.detect-changes.outputs.services }}' | jq -r '.[]')
for SERVICE in $SERVICES; do
IMAGE="${{ secrets.DOCKERHUB_USERNAME }}/grfics-${SERVICE}:${DOCKER_TAG}"
echo "🔗 Creating multi-arch manifest for ${IMAGE}"
docker buildx imagetools create \
-t "${IMAGE}" \
"${IMAGE%:*}:amd64" \
"${IMAGE%:*}:arm64"
echo "✅ Multi-arch manifest published for ${IMAGE}"
done