|
| 1 | +name: Default Ruleset |
| 2 | +on: |
| 3 | + workflow_dispatch: # Manually |
| 4 | + schedule: # Every Monday at 06:00 UTC |
| 5 | + - cron: '0 6 * * 1' |
| 6 | + |
| 7 | +permissions: read-all |
| 8 | + |
| 9 | + |
| 10 | +jobs: |
| 11 | + update: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + name: Update |
| 14 | + outputs: |
| 15 | + mutation-happened: ${{ steps.detect.outputs.mutation-happened }} |
| 16 | + steps: |
| 17 | + - name: Checkout |
| 18 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 19 | + - name: Setup Go |
| 20 | + uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # v5.3.0 |
| 21 | + with: |
| 22 | + go-version: oldstable |
| 23 | + cache-dependency-path: _tools/ruleset-updater/go.mod |
| 24 | + - name: Generate a GitHub token |
| 25 | + id: generate-token |
| 26 | + uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6 |
| 27 | + with: |
| 28 | + app-id: ${{ vars.DD_K9_LIBRARY_GO_APP_ID }} |
| 29 | + private-key: ${{ secrets.DD_K9_LIBRARY_GO_APP_PRIVATE_KEY }} |
| 30 | + - name: Update Default Ruleset |
| 31 | + run: go run -C _tools/ruleset-updater run . -output=${{ github.workspace }}/internal/ruleset/recommended.json.gz |
| 32 | + env: |
| 33 | + GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} |
| 34 | + - name: Detect Mutation |
| 35 | + id: detect |
| 36 | + run: |- |
| 37 | + git add . |
| 38 | + git diff --staged --patch --exit-code > ${{ runner.temp }}/repo.patch || echo "mutation-happened=true" >> "${GITHUB_OUTPUT}" |
| 39 | + - name: Upload Patch |
| 40 | + if: steps.detect.outputs.mutation_happened |
| 41 | + uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0 |
| 42 | + with: |
| 43 | + name: repo.patch |
| 44 | + path: ${{ runner.temp }}/repo.patch |
| 45 | + |
| 46 | + pr: |
| 47 | + runs-on: ubuntu-latest |
| 48 | + name: Create PR |
| 49 | + needs: update |
| 50 | + if: needs.update.outputs.mutation-happened |
| 51 | + permissions: |
| 52 | + contents: write |
| 53 | + steps: |
| 54 | + - name: Checkout |
| 55 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 56 | + - name: Download Patch |
| 57 | + uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 |
| 58 | + with: |
| 59 | + name: repo.patch |
| 60 | + path: ${{ runner.temp }} |
| 61 | + - name: Apply Patch |
| 62 | + id: apply |
| 63 | + run: |- |
| 64 | + git apply ${{ runner.temp }}/repo.patch |
| 65 | + echo "version=$(jq -r '.metadata.rules_version' < ./appsec/rules.json)" >> $GITHUB_OUTPUT |
| 66 | +
|
| 67 | + - name: Create PR Branch |
| 68 | + id: create-branch |
| 69 | + run: |- |
| 70 | + branch="automation/default-ruleset-update/${VERSION}" |
| 71 | + git push origin "${{ github.sha }}":"refs/heads/${branch}" |
| 72 | + echo "branch=${branch}" >> "${GITHUB_OUTPUT}" |
| 73 | + git fetch origin "${branch}" |
| 74 | + env: |
| 75 | + VERSION: ${{ steps.apply.outputs.version }} |
| 76 | + - name: Generate a GitHub token |
| 77 | + id: generate-token |
| 78 | + uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6 |
| 79 | + with: |
| 80 | + app-id: ${{ vars.DD_K9_LIBRARY_GO_APP_ID }} |
| 81 | + private-key: ${{ secrets.DD_K9_LIBRARY_GO_APP_PRIVATE_KEY }} |
| 82 | + # We use ghcommit to create signed commits directly using the GitHub API |
| 83 | + - name: Create Commit on PR Branch |
| 84 | + uses: planetscale/ghcommit-action@6a383e778f6620afde4bf4b45069d3c6983c1ae2 # v0.2.15 |
| 85 | + with: |
| 86 | + commit_message: "chore: update default ruleset to ${{ steps.apply.outputs.version }}" |
| 87 | + branch: ${{ steps.create-branch.outputs.branch }} |
| 88 | + repo: ${{ github.repository }} |
| 89 | + env: |
| 90 | + GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} |
| 91 | + - name: Create PR |
| 92 | + run: |- |
| 93 | + git fetch origin "${{ steps.create-branch.outputs.branch }}" |
| 94 | + git reset --hard HEAD |
| 95 | + git switch "${{ steps.create-branch.outputs.branch }}" |
| 96 | + gh pr create --title "chore: update default ruleset to ${VERSION}" \ |
| 97 | + --body "Updated default ruleset to ${VERSION}." \ |
| 98 | + --head="${{ steps.create-branch.outputs.branch }}" |
| 99 | + env: |
| 100 | + GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} |
| 101 | + VERSION: ${{ steps.apply.outputs.version }} |
0 commit comments