@@ -254,40 +254,44 @@ jobs:
254254 # - name: Fix paths in coverage file
255255 # run: |
256256 # sed -i 's/\/home\/runner\/work\/anonymizer\/anonymizer/\/github\/workspace\//g' coverage.xml
257- - name : " Extract and export repo owner/name"
257+ - name : Prepare SonarCloud args
258+ # Run only for pull requests or push to main
259+ if : >
260+ ${{ !cancelled() &&
261+ hashFiles('**/coverage.xml') != '' &&
262+ (github.event_name == 'pull_request' ||
263+ (github.event_name == 'push' && github.ref_name =='main')
264+ )}}
258265 shell : bash
259266 run : |
267+ PR_NUMBER=${{ github.event.number }}
268+ PR_HEAD_SHA=${{ github.event.pull_request.head.sha }}
260269 # 1. Read the full slug (owner/repo)
261270 REPO_SLUG="${GITHUB_REPOSITORY}"
262271 # 2. Split into owner and repo
263272 IFS="/" read -r REPO_OWNER REPO_NAME <<< "$REPO_SLUG"
264273 # 3. Export to the workflow environment
265274 echo "REPO_OWNER=$REPO_OWNER" >> $GITHUB_ENV
266275 echo "REPO_NAME=$REPO_NAME" >> $GITHUB_ENV
267-
268- - name : SonarCloud scan (pull request)
269- if : ${{ !cancelled() && hashFiles('**/coverage.xml') != '' && github.event_name == 'pull_request' }}
270- uses : SonarSource/sonarqube-scan-action@v6
271- env :
272- SONAR_TOKEN : ${{ secrets.CICD_ORG_SONAR_TOKEN_CICD_BOT || secrets.AAP_ORG_SONAR_TOKEN_ANSIBLE_CICD_BOT }}
273- with :
274- args : >
275- -Dsonar.projectKey=${{ env.REPO_OWNER }}_${{ env.REPO_NAME }}
276- -Dsonar.organization=${{ env.REPO_OWNER }}
277- -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }}
278- -Dsonar.pullrequest.key=${{ env.PR_NUMBER }}
279- -Dsonar.pullrequest.branch=${{ env.PR_HEAD }}
280- continue-on-error : true
281-
282- - name : SonarCloud scan (push)
283- if : ${{ !cancelled() && hashFiles('**/coverage.xml') != '' && github.event_name == 'push' && github.ref_name == 'main' }}
276+ SONAR_ARGS="-Dsonar.projectKey=${REPO_OWNER}_${REPO_NAME} -Dsonar.organization=${REPO_OWNER}"
277+ if [[ -n "$PR_NUMBER" ]]; then
278+ SONAR_ARGS="${SONAR_ARGS} -Dsonar.pullrequest.key=${PR_NUMBER} -Dsonar.pullrequest.branch=${PR_HEAD_SHA}"
279+ fi
280+ echo SONAR_ARGS="$SONAR_ARGS" >> $GITHUB_ENV
281+
282+ - name : SonarCloud scan
283+ # Run only for pull requests or push to main
284+ if : >
285+ ${{ !cancelled() &&
286+ hashFiles('**/coverage.xml') != '' &&
287+ (github.event_name == 'pull_request' ||
288+ (github.event_name == 'push' && github.ref_name =='main')
289+ )}}
284290 uses : SonarSource/sonarqube-scan-action@v6
285291 env :
286292 SONAR_TOKEN : ${{ secrets.CICD_ORG_SONAR_TOKEN_CICD_BOT || secrets.AAP_ORG_SONAR_TOKEN_ANSIBLE_CICD_BOT }}
287293 with :
288- args : >
289- -Dsonar.projectKey=${{ env.REPO_OWNER }}_${{ env.REPO_NAME }}
290- -Dsonar.organization=${{ env.REPO_OWNER }}
294+ args : ${{ env.SONAR_ARGS }}
291295 continue-on-error : true
292296
293297 check :
0 commit comments