Skip to content

Commit fd969d9

Browse files
Add CI/CD pipeline with lint, test and Docker build
1 parent 60f5164 commit fd969d9

1 file changed

Lines changed: 79 additions & 17 deletions

File tree

.github/workflows/ci-cd.yml

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,104 @@
11
name: CI/CD Pipeline
22

33
on:
4-
release:
5-
types: [ published ]
64
push:
7-
branches:
8-
- '**'
9-
tags-ignore:
10-
- '**'
5+
branches: ["**"]
6+
tags: ["v*.*.*"]
7+
pull_request:
8+
branches: ["main"]
9+
10+
env:
11+
REGISTRY: ghcr.io
12+
IMAGE_NAME: ${{ github.repository }}
1113

1214
jobs:
1315
lint:
14-
name: Lint Code
16+
name: Lint with Ruff
1517
runs-on: ubuntu-latest
16-
permissions:
17-
contents: read
18+
1819
steps:
1920
- name: Checkout code
2021
uses: actions/checkout@v4
2122

23+
- name: Set up Python
24+
uses: actions/setup-python@v5
25+
with:
26+
python-version: "3.11"
27+
28+
- name: Install Ruff
29+
run: pip install ruff==0.8.4
30+
31+
- name: Run Ruff linter
32+
run: ruff check app/ tests/
33+
34+
- name: Run Ruff formatter check
35+
run: ruff format --check app/ tests/
36+
2237
test:
23-
name: Run Tests
38+
name: Run Tests with Coverage
2439
runs-on: ubuntu-latest
25-
permissions:
26-
contents: read
40+
needs: lint
41+
2742
steps:
2843
- name: Checkout code
2944
uses: actions/checkout@v4
3045

31-
build:
32-
name: Build Docker Image
46+
- name: Set up Python
47+
uses: actions/setup-python@v5
48+
with:
49+
python-version: "3.11"
50+
51+
- name: Install dependencies
52+
run: pip install -r requirements.txt
53+
54+
- name: Run tests with coverage
55+
run: pytest --cov=app --cov-report=html --cov-fail-under=80
56+
57+
- name: Upload coverage report
58+
uses: actions/upload-artifact@v4
59+
with:
60+
name: coverage-report
61+
path: htmlcov/
62+
63+
build-and-push:
64+
name: Build and Push Docker Image
3365
runs-on: ubuntu-latest
34-
needs: [lint, test]
35-
if: github.event_name == 'push' || github.event_name == 'release'
66+
needs: test
3667
permissions:
3768
contents: read
38-
actions: read
3969
packages: write
70+
4071
steps:
4172
- name: Checkout code
4273
uses: actions/checkout@v4
74+
75+
- name: Log in to GitHub Container Registry
76+
uses: docker/login-action@v3
77+
with:
78+
registry: ${{ env.REGISTRY }}
79+
username: ${{ github.actor }}
80+
password: ${{ secrets.GITHUB_TOKEN }}
81+
82+
- name: Extract metadata for Docker
83+
id: meta
84+
uses: docker/metadata-action@v5
85+
with:
86+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
87+
tags: |
88+
# Tag with branch name on every push
89+
type=ref,event=branch
90+
# Tag with PR number on pull requests
91+
type=ref,event=pr
92+
# Tag with version and 'latest' on releases (e.g. v1.0.0)
93+
type=semver,pattern={{version}}
94+
type=semver,pattern={{major}}.{{minor}}
95+
type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
96+
97+
- name: Build and push Docker image
98+
uses: docker/build-push-action@v6
99+
with:
100+
context: .
101+
# Only push on tags (releases) or pushes to main
102+
push: ${{ github.event_name != 'pull_request' }}
103+
tags: ${{ steps.meta.outputs.tags }}
104+
labels: ${{ steps.meta.outputs.labels }}

0 commit comments

Comments
 (0)