SonarCloud analysis #76
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright 2026 The kpt Authors | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| name: SonarCloud analysis | |
| on: | |
| workflow_run: | |
| workflows: [Go] | |
| types: [completed] | |
| jobs: | |
| check-artifacts: | |
| runs-on: ubuntu-latest | |
| if: github.event.workflow_run.conclusion == 'success' | |
| permissions: | |
| actions: read | |
| outputs: | |
| has-artifacts: ${{ steps.check.outputs.has-artifacts }} | |
| steps: | |
| - name: Check for coverage artifact | |
| id: check | |
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | |
| with: | |
| script: | | |
| const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: ${{ github.event.workflow_run.id }} | |
| }); | |
| const hasCoverage = artifacts.data.artifacts.some(a => a.name.startsWith('coverage-report-')); | |
| core.setOutput('has-artifacts', hasCoverage); | |
| sonarqube: | |
| needs: check-artifacts | |
| if: needs.check-artifacts.outputs.has-artifacts == 'true' | |
| runs-on: ubuntu-latest | |
| permissions: | |
| actions: read | |
| contents: read | |
| pull-requests: read | |
| steps: | |
| - name: Download PR number artifact | |
| if: github.event.workflow_run.event == 'pull_request' | |
| uses: dawidd6/action-download-artifact@b6e2e70617bc3265edd6dab6c906732b2f1ae151 # v21 | |
| with: | |
| workflow: Go | |
| run_id: ${{ github.event.workflow_run.id }} | |
| name: PR_NUMBER | |
| continue-on-error: true | |
| - name: Read PR_NUMBER.txt | |
| if: github.event.workflow_run.event == 'pull_request' && hashFiles('PR_NUMBER.txt') != '' | |
| id: pr_number | |
| uses: juliangruber/read-file-action@271ff311a4947af354c6abcd696a306553b9ec18 # v1.1.8 | |
| with: | |
| path: ./PR_NUMBER.txt | |
| - name: Request GitHub API for PR data | |
| if: github.event.workflow_run.event == 'pull_request' && hashFiles('PR_NUMBER.txt') != '' | |
| uses: octokit/request-action@b91aabaa861c777dcdb14e2387e30eddf04619ae # v3.0.0 | |
| id: get_pr_data | |
| with: | |
| route: GET /repos/{full_name}/pulls/{number} | |
| number: ${{ steps.pr_number.outputs.content }} | |
| full_name: ${{ github.event.repository.full_name }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract PR metadata | |
| if: github.event.workflow_run.event == 'pull_request' && hashFiles('PR_NUMBER.txt') != '' | |
| id: pr_meta | |
| env: | |
| PR_DATA: ${{ steps.get_pr_data.outputs.data }} | |
| run: | | |
| echo "number=$(echo "$PR_DATA" | jq -r '.number')" >> "$GITHUB_OUTPUT" | |
| echo "head_ref=$(echo "$PR_DATA" | jq -r '.head.ref')" >> "$GITHUB_OUTPUT" | |
| echo "base_ref=$(echo "$PR_DATA" | jq -r '.base.ref')" >> "$GITHUB_OUTPUT" | |
| - name: Debug - Show PR metadata | |
| if: github.event.workflow_run.event == 'pull_request' | |
| run: | | |
| echo "=== DEBUG: PR Metadata ===" | |
| echo "PR Number: ${{ steps.pr_meta.outputs.number }}" | |
| echo "Head Ref: ${{ steps.pr_meta.outputs.head_ref }}" | |
| echo "Base Ref: ${{ steps.pr_meta.outputs.base_ref }}" | |
| echo "=== END DEBUG ===" | |
| - name: Checkout PR head | |
| uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | |
| with: | |
| repository: ${{ github.event.workflow_run.head_repository.full_name }} | |
| ref: ${{ github.event.workflow_run.head_sha }} | |
| fetch-depth: 0 | |
| persist-credentials: false | |
| allow-unsafe-pr-checkout: true | |
| - name: Checkout base branch | |
| if: github.event.workflow_run.event == 'pull_request' | |
| env: | |
| BASE_REF: ${{ steps.pr_meta.outputs.base_ref }} | |
| HEAD_SHA: ${{ github.event.workflow_run.head_sha }} | |
| CLONE_URL: ${{ github.event.repository.clone_url }} | |
| run: | | |
| git remote add upstream "$CLONE_URL" | |
| git fetch upstream | |
| git checkout -B "$BASE_REF" "upstream/$BASE_REF" | |
| git checkout "$HEAD_SHA" | |
| git clean -ffdx && git reset --hard HEAD | |
| - name: Download coverage artifact (docker) | |
| uses: dawidd6/action-download-artifact@b6e2e70617bc3265edd6dab6c906732b2f1ae151 # v21 | |
| with: | |
| workflow: Go | |
| run_id: ${{ github.event.workflow_run.id }} | |
| name: coverage-report-docker | |
| use_unzip: true | |
| continue-on-error: true | |
| - name: Download coverage artifact (podman) | |
| if: hashFiles('coverage.out') == '' | |
| uses: dawidd6/action-download-artifact@b6e2e70617bc3265edd6dab6c906732b2f1ae151 # v21 | |
| with: | |
| workflow: Go | |
| run_id: ${{ github.event.workflow_run.id }} | |
| name: coverage-report-podman | |
| use_unzip: true | |
| continue-on-error: true | |
| - name: Fix Go module paths in coverage | |
| run: | | |
| sed -i 's|github.com/kptdev/kpt|.|g' coverage.out | |
| - name: Debug - Check condition variables before SonarQube scan | |
| run: | | |
| echo "=== DEBUG: Before SonarQube Scan Step ===" | |
| echo "Event type: ${{ github.event.workflow_run.event }}" | |
| echo "PR_NUMBER.txt exists: $(test -f PR_NUMBER.txt && echo 'YES' || echo 'NO')" | |
| echo "PR_NUMBER.txt contents:" | |
| cat PR_NUMBER.txt 2>/dev/null || echo "FILE NOT FOUND" | |
| echo "hashFiles result: $(ls -la PR_NUMBER.txt 2>&1 || echo 'ls failed')" | |
| echo "Current directory: $(pwd)" | |
| echo "Files in workspace:" | |
| ls -la ./ | grep -E "coverage|PR_NUMBER|sonar" || echo "No matching files" | |
| echo "=== END DEBUG ===" | |
| - name: SonarQube Scan on PR | |
| if: github.event.workflow_run.event == 'pull_request' | |
| uses: SonarSource/sonarqube-scan-action@713881670b6b3676cda39549040e2d88c70d582e # v8.2.0 | |
| env: | |
| SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
| with: | |
| args: | |
| -Dsonar.projectKey=kptdev_kpt | |
| -Dsonar.organization=kptdev | |
| -Dproject.settings=sonar.properties | |
| -Dsonar.pullrequest.key=${{ steps.pr_meta.outputs.number }} | |
| -Dsonar.pullrequest.branch=${{ steps.pr_meta.outputs.head_ref }} | |
| -Dsonar.pullrequest.base=${{ steps.pr_meta.outputs.base_ref }} | |
| - name: SonarCloud Scan on push | |
| if: >- | |
| github.event.workflow_run.event == 'push' && | |
| github.event.workflow_run.head_repository.full_name == github.event.repository.full_name | |
| uses: SonarSource/sonarqube-scan-action@713881670b6b3676cda39549040e2d88c70d582e # v8.2.0 | |
| env: | |
| SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
| with: | |
| args: | |
| -Dsonar.projectKey=kptdev_kpt | |
| -Dsonar.organization=kptdev | |
| -Dproject.settings=sonar.properties | |