|
| 1 | +name: Notification with issues |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - "**" |
| 7 | + pull_request: |
| 8 | + branches: |
| 9 | + - "**" |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +jobs: |
| 13 | + gitleaks: |
| 14 | + name: Gitleaks Scan |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + permissions: |
| 18 | + contents: read |
| 19 | + issues: write |
| 20 | + pull-requests: write |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Checkout code |
| 24 | + uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: Run Gitleaks |
| 29 | + id: gitleaks |
| 30 | + uses: gitleaks/gitleaks-action@v2 |
| 31 | + with: |
| 32 | + config: .gitleaks.toml |
| 33 | + continue-on-error: true |
| 34 | + |
| 35 | + # ----------------------------- |
| 36 | + # PR comment (if PR triggered) |
| 37 | + # ----------------------------- |
| 38 | + - name: Comment on PR if secrets detected |
| 39 | + if: github.event_name == 'pull_request' && steps.gitleaks.outcome == 'failure' |
| 40 | + uses: actions/github-script@v7 |
| 41 | + with: |
| 42 | + script: | |
| 43 | + await github.rest.issues.createComment({ |
| 44 | + issue_number: context.issue.number, |
| 45 | + owner: context.repo.owner, |
| 46 | + repo: context.repo.repo, |
| 47 | + body: ` |
| 48 | + **Gitleaks Alert – Secrets Detected** |
| 49 | +
|
| 50 | + @${context.actor} @${context.repo.owner} |
| 51 | +
|
| 52 | + Gitleaks has detected secrets in this pull request. |
| 53 | +
|
| 54 | + **Repository:** ${context.repo.owner}/${context.repo.repo} |
| 55 | + **PR:** #${context.issue.number} |
| 56 | + **Commit:** ${context.sha} |
| 57 | +
|
| 58 | + Please remove the secret immediately and rotate any exposed credentials. |
| 59 | + ` |
| 60 | + }); |
| 61 | +
|
| 62 | + - name: Create GitHub Issue for secrets |
| 63 | + if: steps.gitleaks.outcome == 'failure' |
| 64 | + uses: actions/github-script@v7 |
| 65 | + with: |
| 66 | + script: | |
| 67 | + const title = 'Secrets detected by Gitleaks'; |
| 68 | +
|
| 69 | + const body = ` |
| 70 | + **Gitleaks Secret Detection Alert** |
| 71 | +
|
| 72 | + @${context.actor} @${context.repo.owner} |
| 73 | +
|
| 74 | + Secrets were detected in the repository. |
| 75 | +
|
| 76 | + **Repository:** ${context.repo.owner}/${context.repo.repo} |
| 77 | + **Branch:** ${context.ref.replace('refs/heads/', '')} |
| 78 | + **Commit:** ${context.sha} |
| 79 | + **Triggered by:** ${context.actor} |
| 80 | +
|
| 81 | + Action required: |
| 82 | + - Remove the secret from the codebase |
| 83 | + - Rotate the exposed credentials immediately |
| 84 | +
|
| 85 | + --- |
| 86 | + _This issue was automatically created by github-actions[bot]._ |
| 87 | + `; |
| 88 | +
|
| 89 | + const { data: issues } = await github.rest.issues.listForRepo({ |
| 90 | + owner: context.repo.owner, |
| 91 | + repo: context.repo.repo, |
| 92 | + state: 'open', |
| 93 | + labels: 'security,gitleaks' |
| 94 | + }); |
| 95 | +
|
| 96 | + const existingIssue = issues.find(issue => issue.title === title); |
| 97 | +
|
| 98 | + if (!existingIssue) { |
| 99 | + await github.rest.issues.create({ |
| 100 | + owner: context.repo.owner, |
| 101 | + repo: context.repo.repo, |
| 102 | + title, |
| 103 | + body, |
| 104 | + labels: ['security', 'gitleaks', 'automated'] |
| 105 | + }); |
| 106 | + } else { |
| 107 | + await github.rest.issues.createComment({ |
| 108 | + issue_number: existingIssue.number, |
| 109 | + owner: context.repo.owner, |
| 110 | + repo: context.repo.repo, |
| 111 | + body: ` |
| 112 | + New secret detected |
| 113 | +
|
| 114 | + **Commit:** ${context.sha} |
| 115 | + **Triggered by:** @${context.actor} |
| 116 | + ` |
| 117 | + }); |
| 118 | + } |
| 119 | +
|
| 120 | + - name: Fail workflow if secrets detected |
| 121 | + if: steps.gitleaks.outcome == 'failure' |
| 122 | + run: exit 1 |
0 commit comments