|
| 1 | +name: Generate SBOM |
| 2 | + |
| 3 | +# This workflow uses cyclonedx-py and publishes an sbom.json artifact. |
| 4 | +# It runs on manual trigger or when package files change on the target |
| 5 | +# branches, and creates a PR with the updated SBOM. |
| 6 | +# Internal documentation: go/sbom-scope |
| 7 | + |
| 8 | +on: |
| 9 | + workflow_dispatch: {} |
| 10 | + push: |
| 11 | + branches: ['main', '5.2.x'] |
| 12 | + paths: |
| 13 | + - 'pyproject.toml' |
| 14 | + - 'requirements.txt' |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: write |
| 18 | + pull-requests: write |
| 19 | + |
| 20 | +jobs: |
| 21 | + sbom: |
| 22 | + name: Generate SBOM and Create PR |
| 23 | + runs-on: ubuntu-latest |
| 24 | + concurrency: |
| 25 | + group: sbom-${{ github.ref }} |
| 26 | + cancel-in-progress: false |
| 27 | + steps: |
| 28 | + - name: Checkout repository |
| 29 | + uses: actions/checkout@v6 |
| 30 | + with: |
| 31 | + persist-credentials: false |
| 32 | + - name: Set up Python |
| 33 | + uses: actions/setup-python@v6 |
| 34 | + with: |
| 35 | + python-version: "3.x" |
| 36 | + - name: Generate SBOM |
| 37 | + run: | |
| 38 | + python -m venv .venv |
| 39 | + source .venv/bin/activate |
| 40 | + pip install . |
| 41 | + pip uninstall -y pip setuptools |
| 42 | + deactivate |
| 43 | + python -m venv .venv-sbom |
| 44 | + source .venv-sbom/bin/activate |
| 45 | + pip install cyclonedx-bom==7.2.1 |
| 46 | + cyclonedx-py environment --spec-version 1.5 --output-format JSON --output-file sbom-new.json .venv |
| 47 | + # Add PURL for django-mongodb-backend (local package doesn't get PURL automatically) |
| 48 | + jq '(.components[] | select(.name == "django-mongodb-backend" and .purl == null)) |= (. + {purl: ("pkg:pypi/django-mongodb-backend@" + .version)})' sbom-new.json > sbom.tmp.json && mv sbom.tmp.json sbom-new.json |
| 49 | + - name: Download CycloneDX CLI |
| 50 | + run: | |
| 51 | + curl -L -s -o /tmp/cyclonedx "https://github.com/CycloneDX/cyclonedx-cli/releases/download/v0.29.1/cyclonedx-linux-x64" |
| 52 | + chmod +x /tmp/cyclonedx |
| 53 | + - name: Validate SBOM |
| 54 | + run: /tmp/cyclonedx validate --input-file sbom-new.json --fail-on-errors |
| 55 | + - name: Check for changes |
| 56 | + id: check_changes |
| 57 | + run: | |
| 58 | + if [ -f sbom.json ]; then |
| 59 | + echo "Comparing new SBOM with existing sbom.json..." |
| 60 | + # Use cyclonedx diff to check for component changes |
| 61 | + DIFF_OUTPUT=$(/tmp/cyclonedx diff sbom.json sbom-new.json --component-versions) |
| 62 | +
|
| 63 | + # Check if there are meaningful changes (output contains more than just "None") |
| 64 | + if echo "$DIFF_OUTPUT" | grep -q "^None$"; then |
| 65 | + echo "No component changes detected (only metadata differs)" |
| 66 | + echo "Keeping existing sbom.json" |
| 67 | + rm sbom-new.json |
| 68 | + else |
| 69 | + echo "Component changes detected:" |
| 70 | + echo "$DIFF_OUTPUT" |
| 71 | + echo "Updating sbom.json" |
| 72 | + mv sbom-new.json sbom.json |
| 73 | + fi |
| 74 | + else |
| 75 | + echo "No existing sbom.json found, creating initial version" |
| 76 | + mv sbom-new.json sbom.json |
| 77 | + fi |
| 78 | + - name: Cleanup |
| 79 | + if: always() |
| 80 | + run: rm -rf .venv .venv-sbom |
| 81 | + - name: Upload SBOM artifact |
| 82 | + uses: actions/upload-artifact@v6 |
| 83 | + with: |
| 84 | + name: sbom |
| 85 | + path: sbom.json |
| 86 | + if-no-files-found: error |
| 87 | + - name: Create Pull Request |
| 88 | + uses: peter-evans/create-pull-request@84ae59a2cdc2258d6fa0732dd66352dddae2a412 # v7.0.9 |
| 89 | + with: |
| 90 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 91 | + commit-message: 'Update SBOM after dependency changes' |
| 92 | + branch: auto-update-sbom-${{ github.run_id }} |
| 93 | + delete-branch: true |
| 94 | + title: 'Update SBOM' |
| 95 | + body: | |
| 96 | + ## Automated SBOM Update |
| 97 | +
|
| 98 | + This PR was automatically generated because dependency manifest files changed. |
| 99 | +
|
| 100 | + ### Changes |
| 101 | + - Updated `sbom.json` to reflect current dependencies |
| 102 | +
|
| 103 | + ### Verification |
| 104 | + The SBOM was generated using cyclonedx-py with the current Python environment. |
| 105 | +
|
| 106 | + ### Triggered by |
| 107 | + - Commit: ${{ github.sha }} |
| 108 | + - Workflow run: ${{ github.run_id }} |
| 109 | +
|
| 110 | + --- |
| 111 | + _This PR was created automatically by the [SBOM workflow](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})_ |
| 112 | + labels: | |
| 113 | + sbom |
| 114 | + automated |
| 115 | + dependencies |
0 commit comments