Add new CI for forks to allow Sonar analysis #13
Workflow file for this run
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
| name: CI on forks - build and tests | |
| on: | |
| pull_request: | |
| permissions: {} | |
| jobs: | |
| build: | |
| name: Build OS ${{ matrix.os }} | |
| runs-on: ${{ matrix.os }} | |
| strategy: | |
| matrix: | |
| os: [ubuntu-latest] | |
| steps: | |
| - name: Checkout sources | |
| uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@387ac29b308b003ca37ba93a6cab5eb57c8f5f93 # v4.0.0 | |
| with: | |
| distribution: 'temurin' | |
| java-version: '17' | |
| - name: Build with Maven (Ubuntu) | |
| if: matrix.os == 'ubuntu-latest' | |
| run: ./mvnw --batch-mode -Dpowsybl.docker-unit-tests.skip=false -Pjacoco install | |
| - name: Regroup dependencies in target folders | |
| if: matrix.os == 'ubuntu-latest' | |
| run: ./mvnw dependency:copy-dependencies | |
| - name: Save classes and Jacoco report | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: data-for-sonar-analysis-${{ github.event.pull_request.number }} | |
| path: | | |
| */target/classes | |
| */*/target/classes | |
| */*/*/target/classes | |
| */target/generated-sources | |
| */*/target/generated-sources | |
| */*/*/target/generated-sources | |
| distribution-core/target/dependency | |
| distribution-core/target/site/jacoco-aggregate/jacoco.xml | |
| sonar: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout sources | |
| uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 | |
| with: | |
| ref: nro/forks | |
| repository: powsybl/powsybl-core | |
| fetch-depth: 0 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@387ac29b308b003ca37ba93a6cab5eb57c8f5f93 # v4.0.0 | |
| with: | |
| distribution: 'temurin' | |
| java-version: '17' | |
| - name: Download artifact | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
| owner: "powsybl", | |
| repo: "powsybl-core", | |
| run_id: 15162448074, | |
| }); | |
| let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { | |
| return artifact.name.startsWith("data-for-sonar-analysis-") | |
| })[0]; | |
| if (!matchArtifact) { | |
| core.setFailed("❌ No matching artifact found"); | |
| return; | |
| } | |
| const prNumber = matchArtifact.name.replace("data-for-sonar-analysis-", ""); | |
| console.log(`PR number: ${prNumber}`); | |
| core.exportVariable('PR_NUMBER', prNumber); | |
| let download = await github.rest.actions.downloadArtifact({ | |
| owner: "powsybl", | |
| repo: "powsybl-core", | |
| artifact_id: matchArtifact.id, | |
| archive_format: 'zip', | |
| }); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const temp = '${{ runner.temp }}/artifacts'; | |
| if (!fs.existsSync(temp)){ | |
| fs.mkdirSync(temp); | |
| } | |
| fs.writeFileSync(path.join(temp, 'sonar-data.zip'), Buffer.from(download.data)); | |
| - name: Extract Sonar Analysis Data | |
| run: | | |
| mkdir -p ${{ runner.temp }}/extracted | |
| unzip -q ${{ runner.temp }}/artifacts/sonar-data.zip -d ${{ runner.temp }}/extracted | |
| cp -r ${{ runner.temp }}/extracted/* . | |
| ls -la distribution-core/target/site/jacoco-aggregate/ || echo "Jacoco report directory not found" | |
| - name: Run Sonar Analysis | |
| env: | |
| SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
| run: | | |
| wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-7.1.0.4889-linux-x64.zip | |
| unzip sonar-scanner-cli-*.zip | |
| echo "Checking required directories..." | |
| if [ -f "distribution-core/target/site/jacoco-aggregate/jacoco.xml" ]; then | |
| echo "Jacoco report found" | |
| else | |
| echo "Warning: Jacoco report not found at expected location" | |
| fi | |
| echo "Finding sources and binaries..." | |
| SOURCES=$(find . -type d -path "*/main/java" | sort -u | paste -sd "," -) | |
| if [ -z "$SOURCES" ]; then | |
| echo "Warning: No source directories found!" | |
| else | |
| echo "SOURCES : $SOURCES" | |
| fi | |
| GENERATED=$(find . -type d -path "*/target/generated-sources" | sort -u | paste -sd "," -) | |
| if [ -z "$GENERATED" ]; then | |
| echo "Warning: No generated source directories found!" | |
| else | |
| echo "GENERATED : $GENERATED" | |
| fi | |
| TESTS=$(find . -type d -path "*/test/java" | sort -u | paste -sd "," -) | |
| if [ -z "$TESTS" ]; then | |
| echo "Warning: No test directories found!" | |
| else | |
| echo "TESTS : $TESTS" | |
| fi | |
| BINARIES=$(find . -type d -path "*/target/classes" | sort -u | paste -sd "," -) | |
| if [ -z "$BINARIES" ]; then | |
| echo "Warning: No binaries directories found!" | |
| else | |
| echo "BINARIES : $BINARIES" | |
| fi | |
| LIBRARIES="distribution-core/target/dependency" | |
| if [ -z "$LIBRARIES" ]; then | |
| echo "Warning: No libraries directory found!" | |
| else | |
| echo "LIBRARIES : $LIBRARIES" | |
| fi | |
| echo "Running Sonar analysis..." | |
| ./sonar-scanner-*/bin/sonar-scanner \ | |
| -Dsonar.projectKey=com.powsybl:powsybl-core \ | |
| -Dsonar.organization=powsybl-ci-github \ | |
| -Dsonar.sources="$SOURCES" \ | |
| -Dsonar.generatedSources="$GENERATED" \ | |
| -Dsonar.tests="$TESTS" \ | |
| -Dsonar.java.binaries="$BINARIES" \ | |
| -Dsonar.java.libraries="$LIBRARIES" \ | |
| -Dsonar.java.test.libraries="$LIBRARIES" \ | |
| -Dsonar.coverage.jacoco.xmlReportPaths="distribution-core/target/site/jacoco-aggregate/jacoco.xml" \ | |
| -Dsonar.pullrequest.key="${{ env.PR_NUMBER }}" \ | |
| -Dsonar.pullrequest.branch="$(git rev-parse --abbrev-ref HEAD)" \ | |
| -Dsonar.pullrequest.base="main" \ | |
| -Dsonar.host.url=https://sonarcloud.io \ | |
| -Dsonar.scm.provider=git \ | |
| -Dsonar.log.level=debug |