|
| 1 | +name: DevSecOps Pipeline |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + branches: |
| 5 | + - main |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + security-events: write |
| 10 | + actions: read |
| 11 | + |
| 12 | +jobs: |
| 13 | + |
| 14 | + # Build Docker Image |
| 15 | + build: |
| 16 | + name: Build Docker Image |
| 17 | + runs-on: ubuntu-latest |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout code |
| 21 | + uses: actions/checkout@v4 |
| 22 | + |
| 23 | + - name: Build Docker Image |
| 24 | + run: | |
| 25 | + docker build -t awesome-fastapi:${{ github.sha }} . |
| 26 | +
|
| 27 | +
|
| 28 | + # Run Pylint and Black formatter |
| 29 | + lint_format: |
| 30 | + name: Run lint and formatting checks with pylint and black |
| 31 | + runs-on: ubuntu-latest |
| 32 | + strategy: |
| 33 | + matrix: |
| 34 | + python-version: ["3.12", "3.13"] |
| 35 | + |
| 36 | + steps: |
| 37 | + - uses: actions/checkout@v4 |
| 38 | + - name: 'Setup Python ${{ matrix.python-version}}' |
| 39 | + uses: actions/setup-python@v3 |
| 40 | + with: |
| 41 | + python-version: '${{ matrix.python-version}}' |
| 42 | + |
| 43 | + - name: Install dependencies |
| 44 | + run: | |
| 45 | + python -m pip install --upgrade pip |
| 46 | + pip install pylint |
| 47 | + pip install pylint black |
| 48 | +
|
| 49 | + - name: Run pylint |
| 50 | + run: pylint $(git ls-files '*.py') |
| 51 | + |
| 52 | + - name: Run black |
| 53 | + run: black --check . |
| 54 | + |
| 55 | + |
| 56 | + # Run unit test cases for the Docker image |
| 57 | + testing_phase: |
| 58 | + name: Run unit test |
| 59 | + runs-on: ubuntu-latest |
| 60 | + |
| 61 | + steps: |
| 62 | + - name: Checkout repository |
| 63 | + uses: actions/checkout@v4 |
| 64 | + |
| 65 | + - name: Install dependencies |
| 66 | + run: pip install -r requirements.txt |
| 67 | + |
| 68 | + - name: Run tests |
| 69 | + run: pytest |
| 70 | + |
| 71 | + |
| 72 | + # Run snyk code scanning for vulnerabilities |
| 73 | + snyk_scan: |
| 74 | + permissions: |
| 75 | + contents: read |
| 76 | + security-events: write |
| 77 | + actions: read |
| 78 | + runs-on: ubuntu-latest |
| 79 | + steps: |
| 80 | + - uses: actions/checkout@v4 |
| 81 | + - name: Set up Python |
| 82 | + uses: actions/setup-python@v4 |
| 83 | + with: |
| 84 | + python-version: ["3.12", "3.13"] |
| 85 | + |
| 86 | + |
| 87 | + - name: Install dependencies |
| 88 | + run: pip install -r requirements.txt |
| 89 | + |
| 90 | + - name: Install Snyk CLI |
| 91 | + uses: snyk/actions/setup@806182742461562b67788a64410098c9d9b96adb |
| 92 | + |
| 93 | + env: |
| 94 | + SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} |
| 95 | + - name: Snyk Code test |
| 96 | + run: snyk code test --sarif > snyk-cide.sarif |
| 97 | + |
| 98 | + - name: Snyk Test Dependencies |
| 99 | + run: snyk test |
| 100 | + |
| 101 | + |
| 102 | + # Scan the contianer and lists all security vulnerabilities |
| 103 | + trivy_scans: |
| 104 | + name: Run Trivy security scanner against the image |
| 105 | + runs-on: ubuntu-latest |
| 106 | + steps: |
| 107 | + - name: Checkout code |
| 108 | + uses: actions/checkout@v4 |
| 109 | + |
| 110 | + - name: Run Trivy vulnerability scanner |
| 111 | + uses: aquasecurity/trivy-action@7b7aa264d83dc58691451798b4d117d53d21edfe |
| 112 | + with: |
| 113 | + image-ref: 'awesome-fastapi:${{ github.sha }}' |
| 114 | + format: 'template' |
| 115 | + template: '@/contrib/sarif.tpl' |
| 116 | + output: 'GitHub Actions/Trivy Automation' |
| 117 | + severity: 'CRITICAL,HIGH' |
| 118 | + |
| 119 | + - name: Upload Trivy scan results to GitHub Security tab |
| 120 | + uses: github/codeql-action/upload-sarif@v3 |
| 121 | + with: |
| 122 | + sarif_file: 'trivy-results.sarif' |
| 123 | + |
| 124 | + |
0 commit comments