Repository navigation
247 lines (215 loc) · 8.14 KB
/
Copy pathci.yml
File metadata and controls
247 lines (215 loc) · 8.14 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
name: CI
on:
push:
branches: [master]
pull_request:
branches: [master]
# Manual rebuild, e.g. to pick up Debian security updates when nothing in the repo changed
workflow_dispatch:
inputs:
no_cache:
description: "Build without layer cache (re-runs apt-get upgrade)"
type: boolean
default: true
env:
POETRY_VERSION: "2.5.1"
POETRY_VIRTUALENVS_IN_PROJECT: true
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v7
- name: Set up Python
id: setup-python
uses: actions/setup-python@v7
with:
python-version: "3.14"
# pipx is preinstalled on GitHub runners; avoids a third-party install action
- name: Install Poetry
run: pipx install "poetry==${{ env.POETRY_VERSION }}" --python "${{ steps.setup-python.outputs.python-path }}"
- name: Load cached venv
id: cached-venv
uses: actions/cache@v6
with:
path: .venv
key: venv-lint-${{ runner.os }}-py${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-lint-${{ runner.os }}-py${{ steps.setup-python.outputs.python-version }}-
- name: Install dependencies
if: steps.cached-venv.outputs.cache-hit != 'true'
run: poetry install --only dev --no-interaction
- name: Run Ruff linter
run: poetry run ruff check .
- name: Run Ruff formatter check
run: poetry run ruff format --check .
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v7
- name: Set up Python 3.14
id: setup-python
uses: actions/setup-python@v7
with:
python-version: "3.14"
# pipx is preinstalled on GitHub runners; avoids a third-party install action
- name: Install Poetry
run: pipx install "poetry==${{ env.POETRY_VERSION }}" --python "${{ steps.setup-python.outputs.python-path }}"
- name: Load cached venv
id: cached-venv
uses: actions/cache@v6
with:
path: .venv
key: venv-test-${{ runner.os }}-py${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-test-${{ runner.os }}-py${{ steps.setup-python.outputs.python-version }}-
- name: Install dependencies
if: steps.cached-venv.outputs.cache-hit != 'true'
run: poetry install --no-interaction
- name: Run tests with coverage
run: |
poetry run pytest \
--cov=pybot \
--cov-report=xml \
--cov-report=term-missing \
-v \
--tb=short
env:
SLACK_TOKEN: "xoxb-test-token"
SLACK_ADMIN_TOKEN: "xoxb-admin-test-token"
AIRTABLE_API_KEY: "test-key"
AIRTABLE_BASE_ID: "test-base"
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v7
with:
files: ./coverage.xml
fail_ci_if_error: false
verbose: true
security:
name: Security Scan
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v7
- name: Set up Python 3.14
id: setup-python
uses: actions/setup-python@v7
with:
python-version: "3.14"
# pipx is preinstalled on GitHub runners; avoids a third-party install action
- name: Install Poetry
run: pipx install "poetry==${{ env.POETRY_VERSION }}" --python "${{ steps.setup-python.outputs.python-path }}"
- name: Load cached venv
id: cached-venv
uses: actions/cache@v6
with:
path: .venv
key: venv-security-${{ runner.os }}-py${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('poetry.lock') }}
restore-keys: |
venv-security-${{ runner.os }}-py${{ steps.setup-python.outputs.python-version }}-
- name: Install dependencies
if: steps.cached-venv.outputs.cache-hit != 'true'
run: poetry install --no-interaction
- name: Run Bandit security linter
run: poetry run bandit -r pybot -x pybot/_vendor --skip B101 -f json -o bandit-report.json || true
- name: Display Bandit results
run: poetry run bandit -r pybot -x pybot/_vendor --skip B101 -f txt || true
docker-build-push:
name: Build and Push Docker Image
runs-on: ubuntu-24.04-arm
# Run on push to master (build+push), manual dispatch (build+push) and on PRs (build only)
if: github.event_name == 'push' || github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch'
# Wait for CI checks to pass
needs: [ci-success]
permissions:
id-token: write # Required for OIDC authentication
contents: read # Required to checkout code
steps:
- name: Checkout code
uses: actions/checkout@v7
- name: Determine push eligibility
id: can-push
run: |
if [ "${{ github.event_name }}" == "push" ] || [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "push=true" >> "$GITHUB_OUTPUT"
elif [ "${{ github.event.pull_request.head.repo.full_name }}" == "${{ github.repository }}" ]; then
echo "push=true" >> "$GITHUB_OUTPUT"
else
echo "push=false" >> "$GITHUB_OUTPUT"
fi
- name: Determine Docker tags
id: docker-tag
run: |
ECR=633607774026.dkr.ecr.us-east-2.amazonaws.com/pybot
if [ "${{ github.ref }}" == "refs/heads/master" ]; then
echo "images=${ECR}:prod,${ECR}:latest" >> "$GITHUB_OUTPUT"
echo "environment=Production" >> "$GITHUB_OUTPUT"
else
echo "images=${ECR}:staging" >> "$GITHUB_OUTPUT"
echo "environment=Staging" >> "$GITHUB_OUTPUT"
fi
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
with:
platforms: linux/arm64
- name: Configure AWS credentials
if: steps.can-push.outputs.push == 'true'
uses: aws-actions/configure-aws-credentials@v6
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
role-session-name: GitHubActions-DockerBuild-${{ steps.docker-tag.outputs.environment }}
aws-region: us-east-2
- name: Login to Amazon ECR
id: login-ecr
if: steps.can-push.outputs.push == 'true'
uses: aws-actions/amazon-ecr-login@v2
- name: Build and push Docker image
uses: docker/build-push-action@v7
with:
context: .
file: docker/Dockerfile
target: prod
platforms: linux/arm64
push: ${{ steps.can-push.outputs.push == 'true' }}
tags: ${{ steps.docker-tag.outputs.images }}
provenance: false
no-cache: ${{ inputs.no_cache == true }}
pull: ${{ inputs.no_cache == true }}
cache-from: type=gha,scope=arm64
cache-to: type=gha,mode=max,scope=arm64
- name: Output image URI
if: steps.can-push.outputs.push == 'true'
run: |
echo "Successfully pushed ${{ steps.docker-tag.outputs.environment }} images:"
echo "${{ steps.docker-tag.outputs.images }}"
# Final status check for branch protection
ci-success:
name: CI Success
needs: [lint, test, security]
runs-on: ubuntu-latest
# Always run to satisfy docker-build-push dependency
if: always()
steps:
- name: Check all jobs passed
run: |
if [[ "${{ needs.lint.result }}" == "skipped" ]]; then
echo "Lint job was skipped"
exit 1
fi
if [[ "${{ needs.lint.result }}" != "success" ]]; then
echo "Lint job failed"
exit 1
fi
if [[ "${{ needs.test.result }}" == "skipped" ]]; then
echo "Test job was skipped"
exit 1
fi
if [[ "${{ needs.test.result }}" != "success" ]]; then
echo "Test job failed"
exit 1
fi
# Security is informational, doesn't fail CI
echo "All required jobs passed!"