-
Notifications
You must be signed in to change notification settings - Fork 0
320 lines (289 loc) · 10.9 KB
/
python-ci-build.yml
File metadata and controls
320 lines (289 loc) · 10.9 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
name: Build and Release Python
# Multi-stage Docker Build Support:
# This workflow supports building multiple containers from a single multi-stage Dockerfile.
# To use this feature, provide a comma-separated list of container names via the 'container_names' input.
#
# Example usage:
# container_names: "api-server,mcp-server,community-worker"
#
# Each container will be built with the corresponding target stage and published as separate packages:
# - ghcr.io/repo-api-server:v1.0.0
# - ghcr.io/repo-mcp-server:v1.0.0
# - ghcr.io/repo-community-worker:v1.0.0
#
# All containers share the same version/tag but are published as separate packages.
# If container_names is not provided, the workflow builds a single container without a target stage.
on:
workflow_call:
inputs:
python_version:
required: false
type: string
default: '3.11'
skip_ci:
required: false
type: boolean
default: false
skip_tests:
required: false
type: boolean
default: true
skip_security_scan:
required: false
type: boolean
default: true
commit_sha:
required: true
type: string
docker_image_scanning:
required: false
type: boolean
default: true
install_dev_dependencies:
required: false
type: boolean
default: true
container_names:
required: false
type: string
description: 'Comma-separated list of container names for multi-stage builds (e.g., "api-server,mcp-server,community-worker")'
permissions:
contents: write
packages: write
security-events: write
jobs:
validate-commit:
name: 'Validate Commit'
runs-on: ubuntu-latest
outputs:
is_merge_commit: ${{ steps.check_merge_commit.outputs.is_merge_commit }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ inputs.commit_sha }}
fetch-depth: 2
- id: check_merge_commit
name: Check if merge commit
run: |
PARENT_COUNT=$(git cat-file -p HEAD | grep '^parent ' | wc -l)
if [ $PARENT_COUNT -gt 1 ]; then
echo "is_merge_commit=true" >> $GITHUB_OUTPUT
else
echo "is_merge_commit=false" >> $GITHUB_OUTPUT
fi
python-ci:
name: 'Python CI'
runs-on: ubuntu-latest
needs: validate-commit
if: ${{ !inputs.skip_ci }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ inputs.commit_sha }}
- name: Set up Python ${{ inputs.python_version }}
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python_version }}
- name: Install UV
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
- name: Install Python dependencies
run: |
if [ "${{ inputs.install_dev_dependencies }}" = "true" ]; then
uv sync --extra dev
else
uv sync
fi
# - name: Run Python checks
# run: |
# uv run ruff check
# uv run pyright .
# if [ "${{ inputs.skip_tests }}" = "false" ]; then
# uv run pytest || if [ $? -eq 5 ]; then echo "No tests found"; else exit $?; fi
# fi
# if [ "${{ inputs.skip_security_scan }}" = "false" ]; then
# uv run bandit -r . -f json -o bandit-report.json
# fi
prepare-matrix:
name: 'Prepare Build Matrix'
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- id: set-matrix
name: Set matrix
run: |
if [ -n "${{ inputs.container_names }}" ]; then
# Convert comma-separated list to JSON array
containers=$(echo "${{ inputs.container_names }}" | tr ',' '\n' | sed 's/^/"/;s/$/"/' | paste -sd, -)
echo "matrix={\"container\":[$containers]}" >> $GITHUB_OUTPUT
else
echo "matrix={\"container\":[\"\"]}" >> $GITHUB_OUTPUT
fi
build-and-publish:
name: 'Build and Publish'
runs-on: ubuntu-latest
needs: [validate-commit, python-ci, prepare-matrix]
if: ${{ always() && (inputs.skip_ci || needs.python-ci.result == 'success') }}
strategy:
matrix: ${{ fromJson(needs.prepare-matrix.outputs.matrix) }}
outputs:
image_tag: ${{ steps.get_tag.outputs.image_tag }}
image_digest: ${{ steps.docker_push.outputs.digest }}
container_tags: ${{ steps.collect_tags.outputs.container_tags }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python ${{ inputs.python_version }}
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python_version }}
- name: Install UV
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
- name: Install Python dependencies
run: |
if [ "${{ inputs.install_dev_dependencies }}" = "true" ]; then
uv sync --extra dev
else
uv sync
fi
- id: get_tag
name: Get Git Tag
run: |
# Check if the current commit has a tag
TAG=$(git describe --tags --exact-match ${{ inputs.commit_sha }} 2>/dev/null || echo "")
if [ -n "$TAG" ]; then
echo "image_tag=$TAG" >> $GITHUB_OUTPUT
else
# Use short commit hash if no tag
SHORT_SHA=$(echo ${{ inputs.commit_sha }} | cut -c1-7)
echo "image_tag=main-${SHORT_SHA}" >> $GITHUB_OUTPUT
fi
- name: Python Semantic Release
if: ${{ needs.validate-commit.outputs.is_merge_commit == 'false' }}
run: uv run semantic-release version
env:
GH_TOKEN: ${{ secrets.GHCR_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GHCR_TOKEN }}
- id: docker_push
name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ matrix.container != '' && format('ghcr.io/{0}-{1}:{2}', github.repository, matrix.container, steps.get_tag.outputs.image_tag) || format('ghcr.io/{0}:{1}', github.repository, steps.get_tag.outputs.image_tag) }}
platforms: linux/amd64,linux/arm64
cache-from: type=gha
cache-to: type=gha,mode=max
target: ${{ matrix.container != '' && matrix.container || '' }}
- id: collect_tags
name: Collect container tags
run: |
if [ -n "${{ matrix.container }}" ]; then
echo "container_tags=${{ steps.get_tag.outputs.image_tag }}" >> $GITHUB_OUTPUT
else
echo "container_tags=${{ steps.get_tag.outputs.image_tag }}" >> $GITHUB_OUTPUT
fi
scan-image:
name: 'Scan Docker Image'
runs-on: ubuntu-latest
needs: [build-and-publish, prepare-matrix]
if: ${{ inputs.docker_image_scanning && always() && needs.build-and-publish.result == 'success' }}
continue-on-error: true
strategy:
matrix: ${{ fromJson(needs.prepare-matrix.outputs.matrix) }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ inputs.commit_sha }}
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GHCR_TOKEN }}
- name: Generate image reference for scanning
id: scan_image
run: |
# Check if the current commit has a tag
TAG=$(git describe --tags --exact-match ${{ inputs.commit_sha }} 2>/dev/null || echo "")
if [ -n "$TAG" ]; then
IMAGE_TAG="$TAG"
else
# Use short commit hash if no tag
SHORT_SHA=$(echo ${{ inputs.commit_sha }} | cut -c1-7)
IMAGE_TAG="main-${SHORT_SHA}"
fi
# Generate image reference based on container type
if [ -n "${{ matrix.container }}" ]; then
echo "image_ref=ghcr.io/${{ github.repository }}-${{ matrix.container }}:${IMAGE_TAG}" >> $GITHUB_OUTPUT
else
echo "image_ref=ghcr.io/${{ github.repository }}:${IMAGE_TAG}" >> $GITHUB_OUTPUT
fi
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
#continue-on-error: true
with:
image-ref: '${{ steps.scan_image.outputs.image_ref }}'
format: 'sarif'
output: 'trivy-results.sarif'
security: 'CRITICAL'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
if: always()
continue-on-error: true
with:
sarif_file: 'trivy-results.sarif'
branch-mapping:
name: 'Branch to Environment'
runs-on: ubuntu-latest
outputs:
environment: ${{ steps.environment.outputs.environment }}
steps:
- name: Branch Mapping
id: environment
run: |
if [ "${{ github.ref }}" = "refs/heads/develop" ]; then
echo "environment=devnet" >> "$GITHUB_OUTPUT"
elif [ "${{ github.ref }}" = "refs/heads/main" ] || [ "${{ github.ref }}" = "refs/heads/master" ]; then
echo "environment=testnet" >> "$GITHUB_OUTPUT"
else
echo "::error::Unsupported branch"
exit 1
fi
release-ixoworld-workaround:
name: 'Release Helm Chart(s)'
needs: [build-and-publish, scan-image, branch-mapping]
if: github.repository_owner == 'ixoworld' && (always() && needs.build-and-publish.result == 'success' && (needs.scan-image.result == 'success' || needs.scan-image.result == 'skipped' || needs.scan-image.result == 'failure'))
uses: ixoworld/ixo-github-actions/.github/workflows/node-ci-deploy.yml@main
secrets: inherit
with:
environment: ${{ needs.branch-mapping.outputs.environment }}
version: ${{ needs.build-and-publish.outputs.image_tag }}
release-helm:
name: 'Release Helm Chart'
needs: [build-and-publish, scan-image, branch-mapping]
if: github.repository_owner == 'ixofoundation' && (always() && needs.build-and-publish.result == 'success' && (needs.scan-image.result == 'success' || needs.scan-image.result == 'skipped' || needs.scan-image.result == 'failure'))
uses: ixofoundation/ixo-github-actions/.github/workflows/node-ci-deploy.yml@main
secrets: inherit
with:
environment: ${{ needs.branch-mapping.outputs.environment }}
version: ${{ needs.build-and-publish.outputs.image_tag }}
# Notes for Deployment
# TestNet will be deployed using the latest release image ^
# Main will require deployment review and if approved use the latest release image ^