Repository navigation
255 lines (222 loc) Β· 7.82 KB
/
Copy path07-python-cicd.yml
File metadata and controls
255 lines (222 loc) Β· 7.82 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
# Demo 7 β CI/CD: Python Application Pipeline
# Full pipeline: lint β test β build artifact β deploy (staging + production)
# Ties together concepts from all previous demos
name: "Demo 7 β Python CI/CD"
on:
push:
paths:
- "demos/demo7/**"
- ".github/workflows/07-python-cicd.yml"
branches: [main]
pull_request:
paths:
- "demos/demo7/**"
- ".github/workflows/07-python-cicd.yml"
workflow_dispatch:
permissions:
contents: read
defaults:
run:
working-directory: demos/demo7
jobs:
# βββ Job 1: Lint with Ruff ββββββββββββββββββββββββββββββββββββββ
lint:
name: "Lint (Ruff)"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Cache pip
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: pip-${{ runner.os }}-lint-${{ hashFiles('demos/demo7/requirements.txt') }}
restore-keys: pip-${{ runner.os }}-lint-
- name: Install Ruff
run: pip install ruff
- name: Run Ruff linter
run: |
echo "::group::π Ruff lint results"
ruff check . --output-format=text
echo "::endgroup::"
- name: Run Ruff formatter check
run: |
ruff format --check .
- name: Lint summary
if: always()
run: |
cat >> "$GITHUB_STEP_SUMMARY" <<'EOF'
## π Lint Results
- **Tool:** Ruff
- **Status:** ${{ job.status == 'success' && 'β
Passed' || 'β Failed' }}
EOF
# βββ Job 2: Test with Pytest ββββββββββββββββββββββββββββββββββββ
test:
name: "Test (Python ${{ matrix.python-version }})"
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11", "3.12"]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Cache pip
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: pip-${{ runner.os }}-py${{ matrix.python-version }}-${{ hashFiles('demos/demo7/requirements.txt') }}
restore-keys: pip-${{ runner.os }}-py${{ matrix.python-version }}-
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run tests
run: |
echo "::group::π§ͺ Pytest output"
pytest test_app.py -v --tb=short --junitxml=test-results.xml 2>&1 | tee pytest-output.txt
echo "::endgroup::"
- name: Test summary
if: always()
run: |
TESTS=$(grep -c "PASSED\|FAILED" pytest-output.txt 2>/dev/null || echo "?")
PASSED=$(grep -c "PASSED" pytest-output.txt 2>/dev/null || echo "0")
cat >> "$GITHUB_STEP_SUMMARY" <<EOF
## π§ͺ Test Results (Python ${{ matrix.python-version }})
- **Total tests:** ${TESTS}
- **Passed:** ${PASSED}
- **Status:** ${{ job.status == 'success' && 'β
All passed' || 'β Some failed' }}
EOF
- name: Upload test results
if: always()
uses: actions/upload-artifact@v4
with:
name: test-results-py${{ matrix.python-version }}
path: |
demos/demo7/test-results.xml
demos/demo7/pytest-output.txt
# βββ Job 3: Build artifact βββββββββββββββββββββββββββββββββββββ
build:
name: "Build"
runs-on: ubuntu-latest
needs: [lint, test]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Create build artifact
run: |
echo "::group::π¦ Building application package"
BUILD_DIR="build"
mkdir -p "$BUILD_DIR"
# Copy application code
cp app.py "$BUILD_DIR/"
cp requirements.txt "$BUILD_DIR/"
# Add build metadata
cat > "$BUILD_DIR/BUILD_INFO.txt" <<EOF
Build Date: $(date -u)
Commit SHA: ${{ github.sha }}
Commit Short: ${{ github.sha }}
Branch: ${{ github.ref_name }}
Run ID: ${{ github.run_id }}
Run Number: ${{ github.run_number }}
Python: 3.12
EOF
echo "::endgroup::"
echo "β
Build artifact created"
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: app-build-${{ github.run_number }}
path: demos/demo7/build/
retention-days: 5
- name: Build summary
run: |
cat >> "$GITHUB_STEP_SUMMARY" <<EOF
## π¦ Build Artifact
- **Artifact name:** \`app-build-${{ github.run_number }}\`
- **Commit:** \`${{ github.sha }}\`
- **Contents:** app.py, requirements.txt, BUILD_INFO.txt
EOF
# βββ Job 4: Deploy to Staging βββββββββββββββββββββββββββββββββββ
deploy-staging:
name: "Deploy β Staging"
runs-on: ubuntu-latest
needs: [build]
environment: staging
steps:
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: app-build-${{ github.run_number }}
path: deploy/
- name: Verify artifact
run: |
echo "::group::π Artifact contents"
ls -la deploy/
cat deploy/BUILD_INFO.txt
echo "::endgroup::"
working-directory: .
- name: Simulate staging deployment
run: |
echo "π Deploying to STAGING..."
echo " β Extracting artifact"
echo " β Installing dependencies"
echo " β Starting application"
sleep 2
echo " β Running smoke test"
python deploy/app.py
echo ""
echo "β
Staging deployment complete!"
working-directory: .
- name: Staging summary
run: |
cat >> "$GITHUB_STEP_SUMMARY" <<EOF
## π Staging Deployment
- **Environment:** staging
- **Artifact:** \`app-build-${{ github.run_number }}\`
- **Status:** β
Deployed and smoke-tested
EOF
working-directory: .
# βββ Job 5: Deploy to Production (with approval gate) ββββββββββ
deploy-production:
name: "Deploy β Production"
runs-on: ubuntu-latest
needs: [deploy-staging]
environment: production
steps:
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: app-build-${{ github.run_number }}
path: deploy/
- name: Simulate production deployment
run: |
echo "π Deploying to PRODUCTION..."
echo " β Same artifact that passed staging"
cat deploy/BUILD_INFO.txt
sleep 2
echo ""
echo " β Running smoke test"
python deploy/app.py
echo ""
echo "β
Production deployment complete!"
working-directory: .
- name: Production summary
run: |
cat >> "$GITHUB_STEP_SUMMARY" <<EOF
## π Production Deployment
- **Environment:** production
- **Artifact:** \`app-build-${{ github.run_number }}\`
- **Status:** β
Deployed
- **Same artifact as staging:** Yes β build once, deploy many
EOF
working-directory: .