forked from inno-devops-labs/DevOps-Core-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
183 lines (155 loc) · 5.44 KB
/
python-ci.yml
File metadata and controls
183 lines (155 loc) · 5.44 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
# GitHub Actions CI/CD Pipeline for Python DevOps Info Service
# Triggers: push/PR to master/lab03 branches (only for app_python changes)
# Features: linting, testing, Docker build/push with CalVer versioning
name: Python CI
on:
push:
branches:
- master
- lab03
paths:
- "app_python/**"
- ".github/workflows/python-ci.yml"
pull_request:
branches:
- master
paths:
- "app_python/**"
- ".github/workflows/python-ci.yml"
# Permissions: read-only for security
permissions:
contents: read
# Cancel previous runs on same branch
concurrency:
group: python-ci-${{ github.ref }}
cancel-in-progress: true
env:
PIP_DISABLE_PIP_VERSION_CHECK: "1"
DOCKER_IMAGE: pepegx/devops-info-service
jobs:
# ========================================
# Job 1: Lint and Test (Matrix Build)
# ========================================
lint-test:
name: Lint & Test (Python ${{ matrix.python-version }})
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
python-version: ["3.11", "3.12"]
defaults:
run:
working-directory: app_python
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"
cache-dependency-path: app_python/requirements.txt
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
- name: Lint with ruff
run: python -m ruff check .
- name: Run unit tests with coverage
run: python -m pytest tests/
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
file: app_python/coverage.xml
flags: unittests
name: codecov-${{ matrix.python-version }}
fail_ci_if_error: false
token: ${{ secrets.CODECOV_TOKEN }}
# ========================================
# Job 2: Security Scanning with Snyk
# ========================================
security:
name: Security Scan (Snyk)
runs-on: ubuntu-latest
needs: lint-test
defaults:
run:
working-directory: app_python
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
- name: Run Snyk security scan
uses: snyk/actions/python@master
continue-on-error: true
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
with:
args: --file=app_python/requirements.txt --severity-threshold=medium
# ========================================
# Job 3: Build and Push Docker Image
# ========================================
docker-build-push:
name: Build & Push Docker Image
runs-on: ubuntu-latest
needs: [lint-test, security]
# Only push on actual commits to master/lab03, not PRs
if: github.event_name == 'push'
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
steps:
- name: Check Docker Hub credentials
id: check-secrets
run: |
if [ -z "$DOCKERHUB_USERNAME" ] || [ -z "$DOCKERHUB_TOKEN" ]; then
echo "has_secrets=false" >> $GITHUB_OUTPUT
echo "⚠️ Docker Hub credentials not configured. Skipping Docker push."
echo "ℹ️ To enable Docker push, add DOCKERHUB_USERNAME and DOCKERHUB_TOKEN secrets."
else
echo "has_secrets=true" >> $GITHUB_OUTPUT
echo "✅ Docker Hub credentials found."
fi
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
if: steps.check-secrets.outputs.has_secrets == 'true'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Generate CalVer version
id: version
run: |
# CalVer format: YYYY.MM.BUILD_NUMBER
CALVER=$(date +"%Y.%m")
VERSION="${CALVER}.${{ github.run_number }}"
echo "calver=${CALVER}" >> $GITHUB_OUTPUT
echo "version=${VERSION}" >> $GITHUB_OUTPUT
echo "Generated version: ${VERSION}"
- name: Build Docker image
uses: docker/build-push-action@v6
with:
context: app_python
file: app_python/Dockerfile
push: ${{ steps.check-secrets.outputs.has_secrets == 'true' }}
load: ${{ steps.check-secrets.outputs.has_secrets != 'true' }}
tags: |
${{ env.DOCKER_IMAGE }}:${{ steps.version.outputs.version }}
${{ env.DOCKER_IMAGE }}:${{ steps.version.outputs.calver }}
${{ env.DOCKER_IMAGE }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
labels: |
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.created=${{ github.event.head_commit.timestamp }}