forked from benjojo/alertmanager-discord
-
Notifications
You must be signed in to change notification settings - Fork 0
284 lines (240 loc) Β· 8.89 KB
/
docker-validate.yml
File metadata and controls
284 lines (240 loc) Β· 8.89 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
---
name: Docker Validate
on:
workflow_dispatch:
push:
branches:
- main
paths:
- "**/Dockerfile"
- ".dockerignore"
- "**/*.go"
- "**/go.mod"
- "**/go.sum"
- ".github/workflows/docker-validate.yml"
pull_request:
branches:
- main
paths:
- "**/Dockerfile"
- ".dockerignore"
- "**/*.go"
- "**/go.mod"
- "**/go.sum"
- ".github/workflows/docker-validate.yml"
concurrency:
group: docker-validate-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
CI: true
GO_VERSION: '1.20'
permissions:
contents: read
jobs:
validate-dockerfile:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: β±οΈ Start timer
id: timer
run: echo "start_time=$(date +%s)" >> "$GITHUB_OUTPUT"
- name: π·οΈ Set lowercase image name
id: image
run: |
echo "IMAGE_NAME=$(echo "${{ github.repository }}" | tr "[:upper:]" "[:lower:]")" >> "$GITHUB_ENV"
- name: π Checkout repository
uses: actions/checkout@v6
- name: π Validate Dockerfile with hadolint
uses: hadolint/hadolint-action@2332a7b74a6de0dda2e2221d575162eba76ba5e5 # v3.3.0
with:
dockerfile: Dockerfile
failure-threshold: error
ignore: DL3018 # Pinning versions in apk is impractical for Alpine packages
- name: π Collect metrics
if: always()
id: metrics
run: |
end_time=$(date +%s)
duration=$((end_time - ${{ steps.timer.outputs.start_time }}))
echo "duration=${duration}" >> "$GITHUB_OUTPUT"
echo "::notice title=Hadolint Metrics::Duration: ${duration}s"
- name: π’ Send notification to Discord
uses: sarisia/actions-status-discord@b8381b25576cb341b2af39926ab42c5056cc44ed # v1.15.5
if: always()
with:
title: "Dockerfile Validation - ${{ env.IMAGE_NAME }}"
description: |
Duration: ${{ steps.metrics.outputs.duration || 'N/A' }}s
Workflow: Docker Validate (Hadolint)
webhook: ${{ secrets.DISCORD_WEBHOOK }}
test-docker-build:
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
matrix:
stage: [builder, final]
steps:
- name: β±οΈ Start timer
id: timer
run: echo "start_time=$(date +%s)" >> "$GITHUB_OUTPUT"
- name: π·οΈ Set lowercase image name
id: image
run: |
echo "IMAGE_NAME=$(echo "${{ github.repository }}" | tr "[:upper:]" "[:lower:]")" >> "$GITHUB_ENV"
- name: π Checkout repository
uses: actions/checkout@v6
- name: π§ Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: π³ Test Docker build - ${{ matrix.stage }} stage
id: build
run: |
echo "π¨ Testing build for ${{ matrix.stage }} stage..."
if [ "${{ matrix.stage }}" = "final" ]; then
# Build the complete multi-stage image
BUILD_DATE="$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
docker buildx build \
--build-arg GO_VERSION=${{ env.GO_VERSION }} \
--build-arg BUILD_DATE="$BUILD_DATE" \
--build-arg BUILD_VERSION=test \
--build-arg VCS_REF=${{ github.sha }} \
--load \
-t ${{ env.IMAGE_NAME }}:test \
.
echo "β
Final stage built successfully"
# Test the built image
echo "π§ͺ Testing built image..."
docker run --rm ${{ env.IMAGE_NAME }}:test -healthcheck || true
echo "β
Image health check passed"
else
# Build only the builder stage
docker buildx build \
--build-arg GO_VERSION=${{ env.GO_VERSION }} \
--target builder \
--load \
-t ${{ env.IMAGE_NAME }}:builder-test \
.
echo "β
Builder stage built successfully"
fi
- name: π Inspect image metadata
if: matrix.stage == 'final'
run: |
echo "π Image labels:"
docker inspect ${{ env.IMAGE_NAME }}:test | jq '.[0].Config.Labels'
echo "π Image size:"
docker images ${{ env.IMAGE_NAME }}:test --format "{{.Size}}"
echo "π Image layers:"
docker history ${{ env.IMAGE_NAME }}:test --no-trunc
- name: π‘οΈ Check security best practices
if: matrix.stage == 'final'
run: |
echo "π Checking for non-root user..."
user=$(docker inspect ${{ env.IMAGE_NAME }}:test | jq -r '.[0].Config.User')
if [ "$user" != "notifier" ]; then
echo "β Container should run as 'notifier' user, found: $user"
exit 1
fi
echo "β
Container runs as non-root user: $user"
echo "π Checking for health check..."
healthcheck=$(docker inspect ${{ env.IMAGE_NAME }}:test | jq -r '.[0].Config.Healthcheck')
if [ "$healthcheck" = "null" ]; then
echo "β Container should have a health check configured"
exit 1
fi
echo "β
Health check is configured"
echo "π Checking exposed ports..."
ports=$(docker inspect ${{ env.IMAGE_NAME }}:test | jq -r '.[0].Config.ExposedPorts | keys[]')
if [ "$ports" != "9094/tcp" ]; then
echo "β Container should expose port 9094/tcp, found: $ports"
exit 1
fi
echo "β
Correct port exposed: $ports"
- name: π Collect metrics
if: always()
id: metrics
run: |
end_time=$(date +%s)
duration=$((end_time - ${{ steps.timer.outputs.start_time }}))
echo "duration=${duration}" >> "$GITHUB_OUTPUT"
echo "::notice title=Docker Build Metrics (${{ matrix.stage }})::Duration: ${duration}s"
- name: π’ Send notification to Discord
uses: sarisia/actions-status-discord@b8381b25576cb341b2af39926ab42c5056cc44ed # v1.15.5
if: always()
with:
title: "Docker Build Test - ${{ matrix.stage }} - ${{ env.IMAGE_NAME }}"
description: |
Duration: ${{ steps.metrics.outputs.duration || 'N/A' }}s
Workflow: Docker Validate (Build Test)
Stage: ${{ matrix.stage }}
Go Version: ${{ env.GO_VERSION }}
webhook: ${{ secrets.DISCORD_WEBHOOK }}
validate-dockerignore:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: β±οΈ Start timer
id: timer
run: echo "start_time=$(date +%s)" >> "$GITHUB_OUTPUT"
- name: π Checkout repository
uses: actions/checkout@v6
- name: π Validate .dockerignore exists
run: |
if [ ! -f .dockerignore ]; then
echo "β οΈ Warning: .dockerignore file not found"
echo "Creating recommended .dockerignore..."
cat > .dockerignore << 'EOF'
# Git
.git
.gitignore
.gitattributes
# GitHub
.github
# Documentation
*.md
LICENSE
# IDE
.vscode
.idea
*.swp
*.swo
# CI/CD
.pre-commit-config.yaml
# Build artifacts
alertmanager-discord
alertmanager-discord.darwin
alertmanager-discord.linux
# Test files
*_test.go
EOF
echo "β Please add the .dockerignore file to your repository"
exit 1
fi
echo "β
.dockerignore file exists"
- name: π Check for common exclusions
run: |
if ! grep -q "\.git" .dockerignore; then
echo "β οΈ Warning: .dockerignore should exclude .git directory"
fi
if ! grep -q "\.github" .dockerignore; then
echo "β οΈ Warning: .dockerignore should exclude .github directory"
fi
if ! grep -q "\*\.md" .dockerignore; then
echo "β οΈ Warning: .dockerignore should exclude markdown files"
fi
echo "β
.dockerignore validation complete"
- name: π Collect metrics
if: always()
id: metrics
run: |
end_time=$(date +%s)
duration=$((end_time - ${{ steps.timer.outputs.start_time }}))
echo "duration=${duration}" >> "$GITHUB_OUTPUT"
echo "::notice title=Dockerignore Metrics::Duration: ${duration}s"
- name: π’ Send notification to Discord
uses: sarisia/actions-status-discord@b8381b25576cb341b2af39926ab42c5056cc44ed # v1.15.5
if: always()
with:
title: "Dockerignore Validation - ${{ github.event.repository.name }}"
description: |
Duration: ${{ steps.metrics.outputs.duration || 'N/A' }}s
Workflow: Docker Validate (Dockerignore)
webhook: ${{ secrets.DISCORD_WEBHOOK }}