|
| 1 | +name: Self Repo Scanner |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + - master |
| 9 | + pull_request: |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + issues: write |
| 15 | + |
| 16 | +jobs: |
| 17 | + scan: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Checkout code |
| 21 | + uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + - name: Install gitleaks |
| 26 | + run: | |
| 27 | + wget https://github.com/gitleaks/gitleaks/releases/download/v8.18.2/gitleaks_8.18.2_linux_x64.tar.gz |
| 28 | + tar -xzf gitleaks_8.18.2_linux_x64.tar.gz |
| 29 | + sudo mv gitleaks /usr/local/bin/ |
| 30 | + gitleaks version |
| 31 | +
|
| 32 | + - name: Run gitleaks scan |
| 33 | + id: scan |
| 34 | + continue-on-error: true |
| 35 | + run: | |
| 36 | + gitleaks detect --report-format json --report-path gitleaks-report.json --verbose |
| 37 | +
|
| 38 | + - name: Process scan results |
| 39 | + if: always() |
| 40 | + uses: actions/github-script@v7 |
| 41 | + with: |
| 42 | + script: | |
| 43 | + const fs = require('fs'); |
| 44 | + |
| 45 | + let scanResults = '# Security Scan Report\n\n'; |
| 46 | + scanResults += `Generated: ${new Date().toISOString()}\n`; |
| 47 | + scanResults += `Repository: ${context.repo.owner}/${context.repo.repo}\n`; |
| 48 | + scanResults += `Branch: ${context.ref}\n\n`; |
| 49 | +
|
| 50 | + if (fs.existsSync('gitleaks-report.json')) { |
| 51 | + const report = JSON.parse(fs.readFileSync('gitleaks-report.json', 'utf8')); |
| 52 | + |
| 53 | + if (report.length > 0) { |
| 54 | + scanResults += `## ⚠️ Gitleaks Scan Results\n\n`; |
| 55 | + scanResults += `Found **${report.length}** potential secret(s):\n\n`; |
| 56 | + |
| 57 | + report.forEach((finding, i) => { |
| 58 | + scanResults += `### ${i+1}. ${finding.RuleID}\n`; |
| 59 | + scanResults += `- **File:** \`${finding.File}\`\n`; |
| 60 | + scanResults += `- **Line:** ${finding.StartLine}\n`; |
| 61 | + scanResults += `- **Commit:** ${finding.Commit?.substring(0, 7) || 'N/A'}\n`; |
| 62 | + if (finding.Secret) { |
| 63 | + scanResults += `- **Match:** \`${finding.Secret.substring(0, 20)}...\`\n`; |
| 64 | + } |
| 65 | + scanResults += `\n`; |
| 66 | + }); |
| 67 | +
|
| 68 | + core.setFailed(`Found ${report.length} potential secret(s) in the repository`); |
| 69 | + } else { |
| 70 | + scanResults += `## ✅ No Issues Found\n\nNo secrets detected in the repository.\n`; |
| 71 | + } |
| 72 | + } else { |
| 73 | + scanResults += `## ✅ No Issues Found\n\nNo secrets detected in the repository.\n`; |
| 74 | + } |
| 75 | +
|
| 76 | + await core.summary |
| 77 | + .addRaw(scanResults) |
| 78 | + .write(); |
| 79 | +
|
| 80 | + fs.writeFileSync('SECURITY_SCAN_REPORT.md', scanResults); |
| 81 | +
|
| 82 | + - name: Upload scan report |
| 83 | + if: always() |
| 84 | + uses: actions/upload-artifact@v4 |
| 85 | + with: |
| 86 | + name: security-scan-report |
| 87 | + path: | |
| 88 | + gitleaks-report.json |
| 89 | + SECURITY_SCAN_REPORT.md |
| 90 | + retention-days: 30 |
0 commit comments