|
1 | 1 | name: CI/CD Pipeline |
2 | 2 |
|
3 | 3 | on: |
4 | | - release: |
5 | | - types: [ published ] |
6 | 4 | 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 }} |
11 | 13 |
|
12 | 14 | jobs: |
13 | 15 | lint: |
14 | | - name: Lint Code |
| 16 | + name: Lint with Ruff |
15 | 17 | runs-on: ubuntu-latest |
16 | | - permissions: |
17 | | - contents: read |
| 18 | + |
18 | 19 | steps: |
19 | 20 | - name: Checkout code |
20 | 21 | uses: actions/checkout@v4 |
21 | 22 |
|
| 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 | + |
22 | 37 | test: |
23 | | - name: Run Tests |
| 38 | + name: Run Tests with Coverage |
24 | 39 | runs-on: ubuntu-latest |
25 | | - permissions: |
26 | | - contents: read |
| 40 | + needs: lint |
| 41 | + |
27 | 42 | steps: |
28 | 43 | - name: Checkout code |
29 | 44 | uses: actions/checkout@v4 |
30 | 45 |
|
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 |
33 | 65 | runs-on: ubuntu-latest |
34 | | - needs: [lint, test] |
35 | | - if: github.event_name == 'push' || github.event_name == 'release' |
| 66 | + needs: test |
36 | 67 | permissions: |
37 | 68 | contents: read |
38 | | - actions: read |
39 | 69 | packages: write |
| 70 | + |
40 | 71 | steps: |
41 | 72 | - name: Checkout code |
42 | 73 | 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