|
| 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 |
0 commit comments