-
Notifications
You must be signed in to change notification settings - Fork 1
220 lines (191 loc) · 7.85 KB
/
docker-pr.yml
File metadata and controls
220 lines (191 loc) · 7.85 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
name: Build PR Images
on:
pull_request:
paths:
- '**/Dockerfile'
env:
REGISTRY: ghcr.io
jobs:
detect-changes:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Detect changed Dockerfiles
id: changes
uses: dorny/paths-filter@v3
with:
filters: |
ttyd:
- 'ttyd/**'
streamlit:
- 'streamlit/**'
cellxgene:
- 'cellxgene/**'
shiny:
- 'shiny-simple-example/**'
marimo:
- 'marimo/**'
- name: Set matrix
id: set-matrix
run: |
CONTAINERS="[]"
if [[ "${{ steps.changes.outputs.ttyd }}" == "true" ]]; then
CONTAINERS=$(echo "$CONTAINERS" | jq -c '. + [{"name":"ttyd","path":"./ttyd"}]')
fi
if [[ "${{ steps.changes.outputs.streamlit }}" == "true" ]]; then
CONTAINERS=$(echo "$CONTAINERS" | jq -c '. + [{"name":"streamlit","path":"./streamlit"}]')
fi
if [[ "${{ steps.changes.outputs.cellxgene }}" == "true" ]]; then
CONTAINERS=$(echo "$CONTAINERS" | jq -c '. + [{"name":"cellxgene","path":"./cellxgene"}]')
fi
if [[ "${{ steps.changes.outputs.shiny }}" == "true" ]]; then
CONTAINERS=$(echo "$CONTAINERS" | jq -c '. + [{"name":"shiny","path":"./shiny-simple-example"}]')
fi
if [[ "${{ steps.changes.outputs.marimo }}" == "true" ]]; then
CONTAINERS=$(echo "$CONTAINERS" | jq -c '. + [{"name":"marimo","path":"./marimo"}]')
fi
echo "matrix={\"container\":$CONTAINERS}" >> "$GITHUB_OUTPUT"
build:
runs-on: ubuntu-latest
needs: detect-changes
if: ${{ fromJson(needs.detect-changes.outputs.matrix).container[0] != null }}
permissions:
contents: read
packages: write
pull-requests: write
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GHCR_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ github.repository }}/development
tags: |
type=ref,event=pr,prefix=${{ matrix.container.name }}-pr
- name: Build and push ${{ matrix.container.name }}
uses: docker/build-push-action@v5
with:
context: ${{ matrix.container.path }}
push: true
platforms: linux/amd64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha,scope=${{ matrix.container.name }}
cache-to: type=gha,mode=max,scope=${{ matrix.container.name }}
- name: Get image reference for scan
id: scanref
run: |
echo "ref=${{ fromJSON(steps.meta.outputs.json).tags[0] }}" >> $GITHUB_OUTPUT
- name: Run security scan
uses: aquasecurity/trivy-action@0.31.0
with:
image-ref: ${{ steps.scanref.outputs.ref }}
format: "table"
output: "trivy-results.txt"
severity: "CRITICAL,HIGH,MEDIUM"
exit-code: "0"
- name: Display security scan results
if: always()
run: |
echo "## 🔒 Security Scan Results - ${{ matrix.container.name }}" >> $GITHUB_STEP_SUMMARY
if [ -f trivy-results.txt ]; then
echo '```' >> $GITHUB_STEP_SUMMARY
cat trivy-results.txt >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
else
echo "No security scan results found" >> $GITHUB_STEP_SUMMARY
fi
- name: Upload security scan results
uses: actions/upload-artifact@v4
if: always()
with:
name: trivy-scan-${{ matrix.container.name }}-${{ github.run_id }}
path: trivy-results.txt
retention-days: 30
- name: Generate build summary
if: always()
run: |
echo "## 🐳 Build Summary - ${{ matrix.container.name }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "- **Image**: \`${{ env.REGISTRY }}/${{ github.repository }}/development\`" >> $GITHUB_STEP_SUMMARY
echo "- **Tag**: \`${{ matrix.container.name }}-pr${{ github.event.pull_request.number }}\`" >> $GITHUB_STEP_SUMMARY
echo "- **Branch**: ${{ github.head_ref }}" >> $GITHUB_STEP_SUMMARY
echo "- **Commit**: \`${{ github.sha }}\`" >> $GITHUB_STEP_SUMMARY
- name: Comment on PR
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const imageName = '${{ matrix.container.name }}';
const imageTag = `${{ env.REGISTRY }}/${{ github.repository }}/development:${imageName}-pr${{ github.event.pull_request.number }}`;
const commentId = `build-results-${imageName}`;
let securityStatus = '✅ Security scan completed';
try {
if (fs.existsSync('trivy-results.txt')) {
const results = fs.readFileSync('trivy-results.txt', 'utf8');
const lines = results.split('\n');
const criticalCount = lines.filter(line => line.includes('CRITICAL')).length;
const highCount = lines.filter(line => line.includes('HIGH')).length;
const mediumCount = lines.filter(line => line.includes('MEDIUM')).length;
if (criticalCount > 0 || highCount > 0) {
securityStatus = `⚠️ Found ${criticalCount} critical, ${highCount} high, ${mediumCount} medium vulnerabilities`;
} else if (mediumCount > 0) {
securityStatus = `✅ ${mediumCount} medium vulnerabilities found`;
} else {
securityStatus = '✅ No vulnerabilities found';
}
}
} catch (e) {
securityStatus = '⚠️ Security scan results could not be parsed';
}
const output = `## 🐳 Docker Build Results - ${imageName}
✅ Docker image built successfully
| Property | Value |
|----------|-------|
| **Image** | \`${imageTag}\` |
| **Branch** | ${{ github.head_ref }} |
| **Commit** | \`${{ github.sha }}\` |
| **Security** | ${securityStatus} |
\`\`\`bash
docker pull ${imageTag}
\`\`\`
📁 Download the full security scan report from the workflow artifacts.
<!-- ${commentId} -->`;
const comments = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
});
const existingComment = comments.data.find(comment =>
comment.body.includes(`<!-- ${commentId} -->`)
);
if (existingComment) {
await github.rest.issues.updateComment({
comment_id: existingComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
});
} else {
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
});
}