Skip to content

Commit d4f21d3

Browse files
committed
feat: added scanning secrets in github actions and email notifications
1 parent 1fc8d09 commit d4f21d3

3 files changed

Lines changed: 207 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Gitleaks Secret Scan
2+
3+
on:
4+
push:
5+
branches: ["**"]
6+
pull_request:
7+
branches: ["**"]
8+
workflow_dispatch:
9+
10+
jobs:
11+
gitleaks:
12+
runs-on: ubuntu-latest
13+
14+
permissions:
15+
contents: read
16+
pull-requests: write
17+
18+
steps:
19+
- uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
23+
- name: Run Gitleaks
24+
id: gitleaks
25+
uses: gitleaks/gitleaks-action@v2
26+
with:
27+
config: .gitleaks.toml
28+
continue-on-error: true
29+
30+
- name: Comment on PR if secrets detected
31+
if: github.event_name == 'pull_request' && steps.gitleaks.outcome == 'failure'
32+
uses: actions/github-script@v7
33+
with:
34+
script: |
35+
await github.rest.issues.createComment({
36+
issue_number: context.issue.number,
37+
owner: context.repo.owner,
38+
repo: context.repo.repo,
39+
body: `
40+
**Gitleaks Alert – Secrets Detected**
41+
42+
@${context.actor} @${context.repo.owner}
43+
44+
Secrets were detected in this pull request.
45+
Please remove them and rotate credentials immediately.
46+
47+
**Commit:** ${context.sha}
48+
`
49+
});
50+
51+
- name: Fail workflow if secrets detected
52+
if: steps.gitleaks.outcome == 'failure'
53+
run: exit 1

.github/workflows/gitleaks-new.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Gitleaks
2+
3+
on:
4+
push:
5+
branches:
6+
- "**"
7+
pull_request:
8+
branches:
9+
- "**"
10+
workflow_dispatch:
11+
12+
jobs:
13+
gitleaks:
14+
runs-on: ubuntu-latest
15+
16+
permissions:
17+
contents: read
18+
pull-requests: read
19+
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v4
23+
with:
24+
fetch-depth: 0
25+
26+
- name: Run Gitleaks
27+
uses: gitleaks/gitleaks-action@v2
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
with:
31+
config: .gitleaks.toml
32+
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
 (0)