|
| 1 | +name: CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + tags: |
| 8 | + - 'v*' |
| 9 | + pull_request: |
| 10 | + branches: |
| 11 | + - 'main' |
| 12 | + |
| 13 | +env: |
| 14 | + REGISTRY: ghcr.io |
| 15 | + POETRY_CACHE_DIR: ~/.cache/pypoetry |
| 16 | + IMAGE_NAME: ${{ github.repository }} |
| 17 | + PYTHON_VERSION: "3.11" |
| 18 | + |
| 19 | +jobs: |
| 20 | + lint: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Install poetry |
| 26 | + run: pipx install poetry |
| 27 | + |
| 28 | + - name: Set up Python ${{ env.python-version }} |
| 29 | + uses: actions/setup-python@v5 |
| 30 | + with: |
| 31 | + python-version: ${{ env.PYTHON_VERSION }} |
| 32 | + cache: "poetry" |
| 33 | + |
| 34 | + - name: Install dependencies |
| 35 | + run: poetry install |
| 36 | + |
| 37 | + - name: run ruff |
| 38 | + run: poetry run ruff check --output-format=github |
| 39 | + |
| 40 | + - name: Run format |
| 41 | + run: poetry run ruff format --check |
| 42 | + |
| 43 | + - name: Run pyright |
| 44 | + run: poetry run pyright |
| 45 | + |
| 46 | + security: |
| 47 | + runs-on: ubuntu-latest |
| 48 | + steps: |
| 49 | + - uses: actions/checkout@v4 |
| 50 | + |
| 51 | + - name: Install poetry |
| 52 | + run: pipx install poetry |
| 53 | + |
| 54 | + - name: Set up Python ${{ env.PYTHON_VERSION }} |
| 55 | + uses: actions/setup-python@v5 |
| 56 | + with: |
| 57 | + python-version: ${{ env.PYTHON_VERSION }} |
| 58 | + cache: "poetry" |
| 59 | + |
| 60 | + - name: Install dependencies |
| 61 | + run: poetry install |
| 62 | + |
| 63 | + - name: Generate SBOM |
| 64 | + run: poetry run cyclonedx-py poetry > sbom.json |
| 65 | + |
| 66 | + - name: Generate licenses file |
| 67 | + run: | |
| 68 | + poetry run pip-licenses --order=license --format=json --with-description > licenses.txt |
| 69 | +
|
| 70 | + - name: Upload SBOM and licenses |
| 71 | + uses: actions/upload-artifact@v4 |
| 72 | + with: |
| 73 | + name: sbom-licenses-${{ github.sha }}.json |
| 74 | + path: | |
| 75 | + sbom.json |
| 76 | + licenses.txt |
| 77 | + if-no-files-found: error |
| 78 | + overwrite: true |
| 79 | + |
| 80 | + - name: Run Trivy vulnerability scanner |
| 81 | + uses: aquasecurity/trivy-action@master |
| 82 | + with: |
| 83 | + scan-type: 'fs' |
| 84 | + scan-ref: '.' |
| 85 | + trivy-config: trivy.yaml |
| 86 | + |
| 87 | + test: |
| 88 | + runs-on: ubuntu-latest |
| 89 | + strategy: |
| 90 | + matrix: |
| 91 | + python-version: ["3.10", "3.11", "3.12"] |
| 92 | + |
| 93 | + steps: |
| 94 | + - uses: actions/checkout@v4 |
| 95 | + with: |
| 96 | + fetch-depth: 0 |
| 97 | + |
| 98 | + - name: Install poetry |
| 99 | + run: pipx install poetry |
| 100 | + |
| 101 | + - name: Set up Python ${{ matrix.python-version }} |
| 102 | + uses: actions/setup-python@v5 |
| 103 | + with: |
| 104 | + python-version: ${{ matrix.python-version }} |
| 105 | + cache: "poetry" |
| 106 | + |
| 107 | + - name: Install dependencies |
| 108 | + run: poetry install |
| 109 | + |
| 110 | + - name: Run pytest |
| 111 | + run: poetry run coverage run -m pytest |
| 112 | + |
| 113 | + - name: run coverage report |
| 114 | + run: poetry run coverage report |
| 115 | + |
| 116 | + - name: run coverage html |
| 117 | + run: poetry run coverage html |
| 118 | + |
| 119 | + - name: Upload code coverage report |
| 120 | + if: matrix.python-version == '3.11' |
| 121 | + uses: actions/upload-artifact@v4 |
| 122 | + with: |
| 123 | + name: codecoverage-${{ github.sha }} |
| 124 | + path: htmlcov/ |
| 125 | + if-no-files-found: error |
| 126 | + overwrite: true |
| 127 | + |
| 128 | + - name: run coverage xml |
| 129 | + run: poetry run coverage xml |
| 130 | + |
| 131 | + - name: SonarCloud Scan |
| 132 | + if: matrix.python-version == '3.11' |
| 133 | + uses: SonarSource/sonarcloud-github-action@master |
| 134 | + env: |
| 135 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any |
| 136 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 137 | + |
| 138 | + |
| 139 | + build: |
| 140 | + needs: test |
| 141 | + runs-on: ubuntu-latest |
| 142 | + permissions: |
| 143 | + packages: write |
| 144 | + contents: read |
| 145 | + security-events: write |
| 146 | + actions: read |
| 147 | + steps: |
| 148 | + - uses: actions/checkout@v4 |
| 149 | + |
| 150 | + - name: Log in to the Container registry |
| 151 | + uses: docker/login-action@v3 |
| 152 | + with: |
| 153 | + registry: ${{ env.REGISTRY }} |
| 154 | + username: ${{ github.actor }} |
| 155 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 156 | + |
| 157 | + - name: Set up QEMU |
| 158 | + uses: docker/setup-qemu-action@v3 |
| 159 | + |
| 160 | + - name: Set up Docker Buildx |
| 161 | + uses: docker/setup-buildx-action@v3 |
| 162 | + |
| 163 | + - name: Extract metadata for Docker |
| 164 | + id: meta |
| 165 | + uses: docker/metadata-action@v5 |
| 166 | + with: |
| 167 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 168 | + |
| 169 | + - name: Build and push Docker image |
| 170 | + uses: docker/build-push-action@v5 |
| 171 | + with: |
| 172 | + context: . |
| 173 | + push: ${{ github.event_name != 'pull_request' }} |
| 174 | + tags: ${{ steps.meta.outputs.tags }} |
| 175 | + labels: ${{ steps.meta.outputs.labels }} |
| 176 | + platforms: linux/amd64,linux/arm64,darwin/amd64 |
| 177 | + |
| 178 | + - name: Run Trivy vulnerability scanner |
| 179 | + if: github.event_name != 'pull_request' |
| 180 | + uses: aquasecurity/trivy-action@master |
| 181 | + with: |
| 182 | + image-ref: ${{ steps.meta.outputs.tags }} |
| 183 | + trivy-config: trivy.yaml |
| 184 | + scan-type: image |
| 185 | + exit-code: 0 |
| 186 | + format: 'sarif' |
| 187 | + output: 'trivy-results.sarif' |
| 188 | + env: |
| 189 | + TRIVY_USERNAME: ${{ github.actor }} |
| 190 | + TRIVY_PASSWORD: ${{ secrets.GITHUB_TOKEN }} |
| 191 | + |
| 192 | + - name: Upload Trivy scan results to GitHub Security tab |
| 193 | + if: github.event_name != 'pull_request' |
| 194 | + uses: github/codeql-action/upload-sarif@v3 |
| 195 | + with: |
| 196 | + sarif_file: 'trivy-results.sarif' |
| 197 | + |
| 198 | + notifyMattermost: |
| 199 | + runs-on: ubuntu-latest |
| 200 | + needs: [lint, security, test, build ] |
| 201 | + if: ${{ always() && contains(needs.*.result, 'failure') }} |
| 202 | + steps: |
| 203 | + - uses: mattermost/action-mattermost-notify@master |
| 204 | + if: github.event_name != 'pull_request' || github.event.pull_request.draft == false |
| 205 | + with: |
| 206 | + MATTERMOST_WEBHOOK_URL: ${{ secrets.MM_WEBHOOK_URL }} |
| 207 | + MATTERMOST_CHANNEL: dev |
| 208 | + TEXT: | |
| 209 | + ${{ github.repository }} failed build @here :unamused: |
| 210 | + :rotating_light: [Pipeline](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) failed :fire: |
| 211 | + MATTERMOST_USERNAME: ${{ github.triggering_actor }} |
0 commit comments