|
1 | 1 | name: Sonar
|
2 | 2 | on:
|
3 |
| - push: |
4 |
| - branches: |
5 |
| - - main |
6 |
| - pull_request: |
7 |
| - types: [opened, synchronize, reopened] |
| 3 | + workflow_run: |
| 4 | + workflows: [Build] |
| 5 | + types: [completed] |
8 | 6 | jobs:
|
9 | 7 | sonar:
|
10 | 8 | name: Sonar
|
11 | 9 | runs-on: ubuntu-latest
|
| 10 | + if: github.event.workflow_run.conclusion == 'success' |
12 | 11 | steps:
|
| 12 | + - name: Create artifacts directory |
| 13 | + run: mkdir -p ${{ runner.temp }}/artifacts |
| 14 | + - name: Download PR number artifact |
| 15 | + if: github.event.workflow_run.event == 'pull_request' |
| 16 | + uses: dawidd6/action-download-artifact@v6 |
| 17 | + with: |
| 18 | + workflow: Build |
| 19 | + run_id: ${{ github.event.workflow_run.id }} |
| 20 | + path: ${{ runner.temp }}/artifacts |
| 21 | + name: PR_NUMBER |
| 22 | + - name: Read PR_NUMBER.txt |
| 23 | + if: github.event.workflow_run.event == 'pull_request' |
| 24 | + id: pr_number |
| 25 | + uses: juliangruber/read-file-action@v1 |
| 26 | + with: |
| 27 | + path: ${{ runner.temp }}/artifacts/PR_NUMBER.txt |
| 28 | + - name: Request GitHub API for PR data |
| 29 | + if: github.event.workflow_run.event == 'pull_request' |
| 30 | + |
| 31 | + id: get_pr_data |
| 32 | + with: |
| 33 | + route: GET /repos/{full_name}/pulls/{number} |
| 34 | + number: ${{ steps.pr_number.outputs.content }} |
| 35 | + full_name: ${{ github.event.repository.full_name }} |
| 36 | + env: |
| 37 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
13 | 38 | - uses: actions/checkout@v4
|
14 | 39 | with:
|
15 |
| - fetch-depth: 0 |
16 |
| - - name: SonarCloud Scan |
| 40 | + repository: ${{ github.event.workflow_run.head_repository.full_name }} |
| 41 | + ref: ${{ github.event.workflow_run.head_branch }} |
| 42 | + fetch-depth: 0 |
| 43 | + - name: Checkout base branch |
| 44 | + if: github.event.workflow_run.event == 'pull_request' |
| 45 | + env: |
| 46 | + HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} |
| 47 | + run: | |
| 48 | + git remote add upstream ${{ github.event.repository.clone_url }} |
| 49 | + git fetch upstream |
| 50 | + git checkout -B ${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} upstream/${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} |
| 51 | + git checkout $HEAD_BRANCH |
| 52 | + git clean -ffdx && git reset --hard HEAD |
| 53 | + - name: SonarCloud Scan on PR |
| 54 | + if: github.event.workflow_run.event == 'pull_request' |
| 55 | + uses: SonarSource/sonarqube-scan-action@master |
| 56 | + env: |
| 57 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 59 | + with: |
| 60 | + args: > |
| 61 | + -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} |
| 62 | + -Dsonar.pullrequest.key=${{ fromJson(steps.get_pr_data.outputs.data).number }} |
| 63 | + -Dsonar.pullrequest.branch=${{ fromJson(steps.get_pr_data.outputs.data).head.ref }} |
| 64 | + -Dsonar.pullrequest.base=${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} |
| 65 | + - name: SonarCloud Scan on push |
| 66 | + if: github.event.workflow_run.event == 'push' && github.event.workflow_run.head_repository.full_name == github.event.repository.full_name |
17 | 67 | uses: SonarSource/sonarqube-scan-action@master
|
18 | 68 | env:
|
| 69 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
19 | 70 | SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
| 71 | + with: |
| 72 | + args: > |
| 73 | + -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} |
| 74 | + -Dsonar.branch.name=${{ github.event.workflow_run.head_branch }} |
0 commit comments