Skip to content

Commit ebe222d

Browse files
ToreMerkelyclaude
andcommitted
Add SonarCloud PR scan workflow and scheduled trigger
The PR scan workflow runs SonarCloud on pull requests for CLI testing. The trigger workflow creates a throwaway PR every 2 weeks to ensure there is always a recent PR scan in SonarCloud (which auto-deletes PR results after 4 weeks). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c9bbaa1 commit ebe222d

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Periodically creates a throwaway PR to trigger a SonarCloud PR scan.
2+
# This ensures there is always at least one PR scan in SonarCloud,
3+
# which is used in CLI testing. The PR is never merged — it is closed
4+
# and the branch deleted once the scan workflow has started.
5+
6+
name: SonarCloud PR Trigger
7+
8+
on:
9+
schedule:
10+
- cron: '0 2 1,15 * *' # 1st and 15th of each month at 02:00 UTC
11+
workflow_dispatch:
12+
13+
permissions:
14+
contents: write
15+
pull-requests: write
16+
17+
jobs:
18+
19+
trigger-sonar-pr-scan:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Harden Runner
23+
uses: step-security/harden-runner@v2
24+
with:
25+
egress-policy: audit
26+
27+
- uses: actions/checkout@v6
28+
with:
29+
fetch-depth: 1
30+
31+
- name: Create branch with temporary file
32+
run: |
33+
BRANCH="sonar-trigger/$(date +%Y%m%d%H%M%S)"
34+
echo "${BRANCH}" > .sonar-trigger
35+
git checkout -b "${BRANCH}"
36+
git add .sonar-trigger
37+
git commit -m "Trigger SonarCloud PR scan"
38+
git push origin "${BRANCH}"
39+
echo "BRANCH=${BRANCH}" >> "${GITHUB_ENV}"
40+
41+
- name: Create PR
42+
env:
43+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44+
run: |
45+
gh pr create \
46+
--title "DO NOT MERGE - Automated SonarCloud PR scan trigger" \
47+
--body "This PR was automatically created to trigger a SonarCloud PR scan for CLI testing. It will be closed automatically." \
48+
--head "${BRANCH}"
49+
50+
# Wait up to 5 minutes to ensure the SonarCloud PR Scan workflow
51+
# has started before we close the PR and delete the branch.
52+
# Once started, the workflow run completes even if the PR is closed.
53+
- name: Wait for scan workflow to start
54+
env:
55+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
56+
run: |
57+
for i in $(seq 1 10); do
58+
RUNS=$(gh run list --branch "${BRANCH}" --workflow "SonarCloud PR Scan" --json status --jq 'length')
59+
if [ "${RUNS}" -gt 0 ]; then
60+
echo "SonarCloud PR Scan workflow has started"
61+
exit 0
62+
fi
63+
echo "Waiting for workflow to start (attempt ${i}/10)..."
64+
sleep 30
65+
done
66+
echo "Timed out waiting, proceeding to close PR"
67+
68+
- name: Close PR and delete branch
69+
env:
70+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
71+
run: |
72+
gh pr close "${BRANCH}" --delete-branch

.github/workflows/sonar-pr.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This workflow runs SonarCloud PR scans for use in CLI testing.
2+
# SonarCloud automatically deletes PR scan results after 4 weeks.
3+
4+
name: SonarCloud PR Scan
5+
6+
on:
7+
pull_request:
8+
9+
env:
10+
SONARCLOUD_PROJECT_KEY: ${{ github.repository_owner }}_${{ github.event.repository.name }}
11+
12+
jobs:
13+
14+
sonarcloud-scan:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Harden Runner
18+
uses: step-security/harden-runner@v2
19+
with:
20+
egress-policy: audit
21+
22+
- uses: actions/checkout@v6
23+
with:
24+
fetch-depth: 0
25+
26+
- name: Run SonarCloud scan
27+
env:
28+
SONAR_TOKEN: ${{ secrets.SONARCLOUD_TOKEN }}
29+
uses: SonarSource/sonarqube-scan-action@v6.0.0

0 commit comments

Comments
 (0)