-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.gitlab-ci.yml
More file actions
572 lines (547 loc) · 18.7 KB
/
Copy path.gitlab-ci.yml
File metadata and controls
572 lines (547 loc) · 18.7 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# GitLab CI/CD Pipeline for n8n-deploy
# Python CLI tool for n8n workflow management
# Optimized for parallel stage execution
stages:
- security
- quality
- test
- build-matrix
- release
# Docker services for caching (only used by Docker-specific jobs)
# Uncomment and configure for jobs that need Docker-in-Docker:
# services:
# - name: docker:20.10.16-dind
# command: [
# "--mtu=1300", # Fix TLS handshake timeout issues
# "--registry-mirror", "http://registry-mirror.pirouter.dev:5000" # Use local mirror (when available)
# ]
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
PYTHON_VERSION: "3.9"
# Docker caching configuration
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: "/certs"
DOCKER_BUILDKIT: 1
# Docker Registry Configuration:
# - pull_policy: if-not-present (configured in GitLab Runner config.toml)
# - registry_mirror: http://registry-mirror.pirouter.dev:5000 (configured in GitLab Runner config.toml)
# - MTU: 1300 for DinD (see services comment above)
# APT proxy for faster package installation
APT_PROXY: "192.168.76.5:3142"
# PyPI deployment configuration (set in CI/CD variables)
# PYPI_TOKEN: <set in GitLab CI/CD settings>
# TEST_PYPI_TOKEN: <set in GitLab CI/CD settings>
# Git configuration for proper versioning
GIT_DEPTH: 0 # Full clone with all tags for setuptools_scm version detection
# Global defaults for all jobs
default:
image: python:3.9-slim
tags:
- python
retry:
max: 2
when:
- runner_system_failure
- stuck_or_timeout_failure
- scheduler_failure
interruptible: true
# Workflow rules - define when pipeline runs
# Push to protected branches (master/develop): full pipeline
# MR targeting protected branches: full pipeline
# Conventional commit branches (feat/, fix/, etc.): security stage only
workflow:
rules:
# Run on master/develop (protected branches)
- if: '$CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "develop"'
when: always
# Run on all tags
- if: '$CI_COMMIT_TAG'
when: always
# Run on merge requests targeting master or develop
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop")'
when: always
# Manual workflow dispatch
- if: '$CI_PIPELINE_SOURCE == "web"'
when: always
# Run on conventional commit branches (security checks only)
- if: '$CI_COMMIT_BRANCH =~ /^(feat|fix|chore|refactor|docs|test|style|perf|ci|build)\//'
when: always
# Skip for all other cases
- when: never
# Cache for pip dependencies (default: pull-push)
cache:
key:
files:
- requirements.txt
prefix: ${CI_COMMIT_REF_SLUG}
fallback_keys:
- develop
- master
paths:
- .cache/pip/
- .venv/
# Cache template for read-only jobs (policy: pull)
.cache_pull: &cache_pull
key:
files:
- requirements.txt
prefix: ${CI_COMMIT_REF_SLUG}
fallback_keys:
- develop
- master
paths:
- .cache/pip/
- .venv/
policy: pull
# Template for Python environment setup (uses cached venv from warm-cache job)
.python_setup: &python_setup
before_script:
- echo "Acquire::http::Proxy \"http://${APT_PROXY}\";" > /etc/apt/apt.conf.d/01proxy
- apt-get -q update && apt-get -qy install git
- python -m pip install --quiet --upgrade pip
- pip install --quiet uv
- test -f .venv/bin/activate && echo "✓ Using cached venv" || echo "⚠ Cache miss - creating venv"
- test -f .venv/bin/activate || uv venv --python python3 .venv
- source .venv/bin/activate
- test -f .venv/lib/python*/site-packages/n8n_deploy.egg-info || uv pip install --quiet -e ".[test,dev]"
# ============================================
# PRE STAGE - Cache warming
# ============================================
warm-cache:
stage: .pre
# Use dualbuntu image for better performance
tags:
- cache
rules:
- when: on_success
cache:
key:
files:
- requirements.txt
prefix: ${CI_COMMIT_REF_SLUG}
fallback_keys:
- develop
- master
paths:
- .cache/pip/
- .venv/
policy: pull-push
before_script:
- echo "Acquire::http::Proxy \"http://${APT_PROXY}\";" > /etc/apt/apt.conf.d/01proxy
- apt-get -q update && apt-get -qy install git
- python -m pip install --quiet --upgrade pip
- pip install --quiet uv
script:
- echo "Warming cache..."
- uv venv --python python3 .venv
- uv pip install --quiet -e ".[test,dev]"
- echo "Cache populated successfully"
# ============================================
# SECURITY STAGE
# ============================================
# Secret detection with gitleaks
secret_detection:
stage: security
tags:
- linux
- python
rules:
- if: '$CI_COMMIT_TAG'
when: never
# Skip on MR pipelines (already ran on feature branch push)
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: never
# Run on conventional commit branches (feature branch push)
- if: '$CI_COMMIT_BRANCH =~ /^(feat|fix|chore|refactor|docs|test|style|perf|ci|build)\//'
when: on_success
# Run on protected branches (post-merge)
- if: '$CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "develop"'
when: on_success
cache:
<<: *cache_pull
needs:
- warm-cache
variables:
SECRET_DETECTION_EXCLUDED_PATHS: "tests/"
before_script:
- echo "Acquire::http::Proxy \"http://${APT_PROXY}\";" > /etc/apt/apt.conf.d/01proxy
- apt-get -q update && apt-get -qy install git wget
- wget -qO- https://github.com/gitleaks/gitleaks/releases/download/v8.29.1/gitleaks_8.29.1_linux_x64.tar.gz | tar xz -C /usr/local/bin
script:
- echo "🔍 Running secret detection with gitleaks..."
- gitleaks git . --verbose --redact --config .gitleaks.toml
- echo "✅ Secret detection completed"
# Dependency vulnerability scanning
dependency_scanning:
stage: security
tags:
- linux
- python
needs:
- warm-cache
rules:
- if: '$CI_COMMIT_TAG'
when: never
# Skip on MR pipelines (already ran on feature branch push)
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: never
# Run on conventional commit branches (feature branch push)
- if: '$CI_COMMIT_BRANCH =~ /^(feat|fix|chore|refactor|docs|test|style|perf|ci|build)\//'
when: on_success
# Run on protected branches (post-merge)
- if: '$CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "develop"'
when: on_success
cache:
<<: *cache_pull
before_script:
- echo "Acquire::http::Proxy \"http://${APT_PROXY}\";" > /etc/apt/apt.conf.d/01proxy
- apt-get -q update && apt-get -qy install git
- python -m pip install --quiet --upgrade pip
- pip install --quiet uv
- test -f .venv/bin/activate && echo "✓ Using cached venv" || echo "⚠ Cache miss - creating venv"
- test -f .venv/bin/activate || uv venv --python python3 .venv
- source .venv/bin/activate
- test -f .venv/lib/python*/site-packages/n8n_deploy.egg-info || uv pip install --quiet -e ".[test,dev]"
- uv pip install --quiet pip-audit
script:
- echo "🔍 Running dependency vulnerability scanning..."
- pip-audit -r requirements.txt
- echo "✅ Dependency scanning completed"
# Python security scanning with Bandit
security:bandit:
stage: security
tags:
- linux
- python
needs:
- warm-cache
rules:
- if: '$CI_COMMIT_TAG'
when: never
# Skip on MR pipelines (already ran on feature branch push)
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: never
# Run on conventional commit branches (feature branch push)
- if: '$CI_COMMIT_BRANCH =~ /^(feat|fix|chore|refactor|docs|test|style|perf|ci|build)\//'
when: on_success
# Run on protected branches (post-merge)
- if: '$CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "develop"'
when: on_success
cache:
<<: *cache_pull
variables:
SAST_EXCLUDED_PATHS: "tests/, .venv/"
before_script:
- echo "Acquire::http::Proxy \"http://${APT_PROXY}\";" > /etc/apt/apt.conf.d/01proxy
- apt-get -q update && apt-get -qy install git
- python -m pip install --quiet --upgrade pip
- pip install --quiet uv
- test -f .venv/bin/activate && echo "✓ Using cached venv" || echo "⚠ Cache miss - creating venv"
- test -f .venv/bin/activate || uv venv --python python3 .venv
- source .venv/bin/activate
- test -f .venv/lib/python*/site-packages/n8n_deploy.egg-info || uv pip install --quiet -e ".[test,dev]"
- pip install --quiet bandit
script:
- echo "🔍 Running Python security analysis with Bandit..."
- bandit -r api/ -f json --exclude tests/
- echo "✅ Bandit analysis completed"
# Pattern-based security scanning with Semgrep
security:semgrep:
stage: security
tags:
- linux
- python
needs:
- warm-cache
rules:
- if: '$CI_COMMIT_TAG'
when: never
# Skip on MR pipelines (already ran on feature branch push)
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: never
# Run on conventional commit branches (feature branch push)
- if: '$CI_COMMIT_BRANCH =~ /^(feat|fix|chore|refactor|docs|test|style|perf|ci|build)\//'
when: on_success
# Run on protected branches (post-merge)
- if: '$CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "develop"'
when: on_success
cache:
<<: *cache_pull
variables:
SAST_EXCLUDED_PATHS: "tests/, .venv/"
before_script:
- echo "Acquire::http::Proxy \"http://${APT_PROXY}\";" > /etc/apt/apt.conf.d/01proxy
- apt-get -q update && apt-get -qy install git
- python -m pip install --quiet --upgrade pip
- pip install --quiet uv
- test -f .venv/bin/activate && echo "✓ Using cached venv" || echo "⚠ Cache miss - creating venv"
- test -f .venv/bin/activate || uv venv --python python3 .venv
- source .venv/bin/activate
- test -f .venv/lib/python*/site-packages/n8n_deploy.egg-info || uv pip install --quiet -e ".[test,dev]"
- pip install --quiet semgrep
script:
- echo "🔍 Running pattern-based security analysis with Semgrep..."
- semgrep --config=auto api/ --json --error
- echo "✅ Semgrep analysis completed"
# ============================================
# QUALITY STAGE
# ============================================
# Type checking with mypy
quality:mypy:
stage: quality
tags:
- alpine
- linux
needs:
- warm-cache
rules:
- if: '$CI_COMMIT_TAG'
when: never
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: never
- if: '$CI_COMMIT_BRANCH =~ /^(feat|fix|chore|refactor|docs|test|style|perf|ci|build)\//'
when: on_success
- if: '$CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "develop"'
when: on_success
<<: *python_setup
cache:
<<: *cache_pull
script:
- mypy api/ --strict --show-error-codes
- echo "Type checking passed"
# Code formatting check with black
quality:black:
stage: quality
tags:
- linux
- python
needs:
- warm-cache
rules:
- if: '$CI_COMMIT_TAG'
when: never
# Skip on MR pipelines (already ran on feature branch push)
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: never
# Run on conventional commit branches (feature branch push)
- if: '$CI_COMMIT_BRANCH =~ /^(feat|fix|chore|refactor|docs|test|style|perf|ci|build)\//'
when: on_success
# Run on protected branches (post-merge)
- if: '$CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "develop"'
when: on_success
<<: *python_setup
cache:
<<: *cache_pull
script:
- black --check api/
- echo "Code formatting check passed"
# ============================================
# AFFECTED TESTING (MR pipelines only)
# Uses pytest-testmon for coverage-based test selection
# ============================================
# Affected tests for merge request pipelines
test:affected:
stage: test
tags:
- heavy
- tests
cache:
# Use multiple caches: venv (branch-specific) + testmondata (shared)
- key:
files:
- requirements.txt
prefix: ${CI_COMMIT_REF_SLUG}
fallback_keys:
- develop
- master
paths:
- .cache/pip/
- .venv/
policy: pull
- key: testmondata-baseline
paths:
- .testmondata
#- .pytest_cache
- .hypothesis
policy: pull-push
needs:
- warm-cache
rules:
- if: '$CI_COMMIT_TAG'
when: on_success
# Run on conventional commit branches (feature branch push)
- if: '$CI_COMMIT_BRANCH =~ /^(feat|fix|chore|refactor|docs|test|style|perf|ci|build)\//'
when: on_success
# Run affected tests on MR pipelines (fast validation)
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop")'
when: on_success
# Run on protected branches (post-merge)
- if: '$CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "develop"'
when: on_success
before_script:
- echo "Acquire::http::Proxy \"http://${APT_PROXY}\";" > /etc/apt/apt.conf.d/01proxy
- apt-get -q update && apt-get -qy install git sqlite3
- python -m pip install --quiet --upgrade pip
- pip install --quiet uv
- test -f .venv/bin/activate && echo "✓ Using cached venv" || echo "⚠ Cache miss - creating venv"
- test -f .venv/bin/activate || uv venv --python python3 .venv
- source .venv/bin/activate
- test -f .venv/lib/python*/site-packages/n8n_deploy.egg-info || uv pip install --quiet -e ".[test,dev]"
script:
- export N8N_DEPLOY_TESTING=1
- test -f .testmondata && echo "Using cached testmon database" || echo "No testmon baseline found"
- ./run_tests.py --affected --no-deps-check
- echo "Affected tests completed"
after_script:
# Flush SQLite WAL to main database before caching
# This ensures .testmondata contains all data without needing -wal/-shm files
- sqlite3 .testmondata "PRAGMA wal_checkpoint(TRUNCATE);" 2>/dev/null || true
artifacts:
paths:
- .testmondata
expire_in: 7 days
when: always
# ============================================
# BUILD-MATRIX STAGE (runs on merge/push to protected branches)
# ============================================
# Multi-version package builds (Python 3.9-3.13)
build:python-matrix:
stage: build-matrix
image: python:${PYTHON_VERSION}-slim
interruptible: false
tags:
- linux
- python
cache:
key:
files:
- requirements.txt
prefix: py${PYTHON_VERSION}-${CI_COMMIT_REF_SLUG}
paths:
- .cache/pip/
- .venv/
policy: pull-push
needs:
- job: test:affected
optional: true
rules:
- if: '$CI_COMMIT_TAG'
when: on_success
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: never
- if: '$CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "develop"'
when: on_success
parallel:
matrix:
- PYTHON_VERSION: ["3.9", "3.10", "3.11", "3.12", "3.13"]
before_script:
- echo "Acquire::http::Proxy \"http://${APT_PROXY}\";" > /etc/apt/apt.conf.d/01proxy
- apt-get -q update && apt-get -qy install git
- python -m pip install --quiet --upgrade pip
- pip install --quiet uv
- test -f .venv/bin/activate && echo "✓ Using cached venv for Python ${PYTHON_VERSION}" || echo "⚠ Cache miss - creating venv for Python ${PYTHON_VERSION}"
- test -f .venv/bin/activate || uv venv --python python3 .venv
- source .venv/bin/activate
- test -f .venv/lib/python*/site-packages/n8n_deploy.egg-info || uv pip install --quiet -e ".[test,dev]"
- uv pip install --quiet build
script:
- echo "Building package for Python ${PYTHON_VERSION}..."
- python -m build --wheel --sdist
- pip install dist/*.whl
- n8n-deploy --version
- echo "Build successful for Python ${PYTHON_VERSION}"
artifacts:
paths:
- dist/
expire_in: 1 hour
# ============================================
# RELEASE STAGE (automatic versioning with go-semrel-gitlab)
# ============================================
# Release candidate tags on develop branch
release:rc:
stage: release
image: registry.gitlab.com/juhani/go-semrel-gitlab:v0.21.1
tags:
- linux
- python
cache: {} # Disable cache - not needed for release
needs:
- job: build:python-matrix
artifacts: false
rules:
# Run after MR merge to develop (not on tags or MR events)
- if: '$CI_COMMIT_TAG'
when: never
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: never
- if: '$CI_COMMIT_BRANCH == "develop"'
when: on_success
- when: never
variables:
GIT_SSL_NO_VERIFY: "1"
GITLAB_URL: "https://192.168.76.5:8443/api/v4"
GSG_TOKEN: $GITLAB_TOKEN
GSG_RELEASE_BRANCHES: master
GSG_PRE_TMPL: 'rc,{{ seq }}'
script:
- echo "Calculating next RC version..."
- NEXT_VERSION=$(release --skip-ssl-verify next-version --allow-current)
- echo "Next version - ${NEXT_VERSION}"
- |
if [ -n "$NEXT_VERSION" ]; then
echo "Creating RC tag v${NEXT_VERSION}..."
if ! release --skip-ssl-verify tag 2>&1 | tee /tmp/release.log; then
if grep -q "no changes found" /tmp/release.log; then
echo "No releasable changes (ci/docs/chore commits only) - skipping tag"
else
echo "Release failed with error:"
cat /tmp/release.log
exit 1
fi
fi
else
echo "No version bump needed"
fi
# Production release tags on master branch
release:production:
stage: release
image: registry.gitlab.com/juhani/go-semrel-gitlab:v0.21.1
tags:
- linux
- python
cache: {} # Disable cache - not needed for release
needs:
- job: build:python-matrix
artifacts: false
rules:
# Run after MR merge to master (not on tags or MR events)
- if: '$CI_COMMIT_TAG'
when: never
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: never
- if: '$CI_COMMIT_BRANCH == "master"'
when: on_success
- when: never
variables:
GIT_SSL_NO_VERIFY: "1"
GITLAB_URL: "https://192.168.76.5:8443/api/v4"
GSG_TOKEN: $GITLAB_TOKEN
script:
- echo "Calculating next production version..."
- NEXT_VERSION=$(release --skip-ssl-verify next-version --allow-current)
- echo "Next version - ${NEXT_VERSION}"
- |
if [ -n "$NEXT_VERSION" ]; then
echo "Creating release tag v${NEXT_VERSION}..."
if ! release --skip-ssl-verify tag 2>&1 | tee /tmp/release.log; then
if grep -q "no changes found" /tmp/release.log; then
echo "No releasable changes (ci/docs/chore commits only) - skipping tag"
else
echo "Release failed with error:"
cat /tmp/release.log
exit 1
fi
fi
else
echo "No version bump needed"
fi