From 6239e9b892d60c85a73d8d818ff6a8d4092d7bf3 Mon Sep 17 00:00:00 2001 From: SebastienDegodez Date: Sun, 22 Dec 2024 02:00:16 +0100 Subject: [PATCH] ci: add conditional check for pull request in CI workflow Signed-off-by: SebastienDegodez --- .github/workflows/cicd.yml | 1 + .github/workflows/steps.dotnet-build-test.yml | 267 +++++++++--------- 2 files changed, 135 insertions(+), 133 deletions(-) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 9591cf9..764261d 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -60,6 +60,7 @@ jobs: uses: ./.github/workflows/steps.publish-test-reporter.yml with: runs-on: ubuntu-latest + if: ${{ github.event.pull_request.head.repo.full_name == github.repository && github.event_name == 'pull_request' }} secrets: inherit nuget_publish: diff --git a/.github/workflows/steps.dotnet-build-test.yml b/.github/workflows/steps.dotnet-build-test.yml index be47628..7979802 100644 --- a/.github/workflows/steps.dotnet-build-test.yml +++ b/.github/workflows/steps.dotnet-build-test.yml @@ -1,135 +1,136 @@ ο»Ώon: - workflow_call: - inputs: - runs-on: - required: false - type: string - default: 'ubuntu-latest' - use-sonarcloud: - required: false - type: boolean - default: false - version: - required: true - type: string - publish-package: - required: false - type: boolean - default: false - secrets: - SONAR_TOKEN: - required: false - outputs: - publish-package: - description: 'Publish package is enabled ?' - value: ${{ jobs.build_test.outputs.publish-package }} + workflow_call: + inputs: + runs-on: + required: false + type: string + default: 'ubuntu-latest' + use-sonarcloud: + required: false + type: boolean + default: false + version: + required: true + type: string + publish-package: + required: false + type: boolean + default: false + secrets: + SONAR_TOKEN: + required: false + outputs: + publish-package: + description: 'Publish package is enabled ?' + value: ${{ jobs.build_test.outputs.publish-package }} jobs: - build_test: - runs-on: ${{ inputs.runs-on }} - - outputs: - publish-package: ${{ inputs.publish-package }} - - steps: - - name: πŸ”„ Checkout - uses: actions/checkout@v4 - with: - lfs: true - fetch-depth: 0 - - - name: πŸ› οΈ Setup .NET - uses: actions/setup-dotnet@v4 - with: - global-json-file: global.json - - # We can remove this as JRE is automaticallu provisionned by Sonar - # - name: πŸ› οΈ Setup JDK 17 - # if: ${{ inputs.use-sonarcloud == true }} - # uses: actions/setup-java@v4.5.0 - # with: - # java-version: 17 - # distribution: 'zulu' - - - name: πŸ› οΈ Install SonarCloud scanner - if: ${{ inputs.use-sonarcloud == true }} - run: dotnet tool install --global dotnet-sonarscanner - - - name: πŸ”§ Restore .NET Tools - run: dotnet tool restore - - - name: πŸ”§ Restore dependencies - run: dotnet restore - - - name: πŸ” Start SonarQube Analysis - if: ${{ inputs.use-sonarcloud == true }} - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - run: | - dotnet-sonarscanner begin ` - /k:"microcks_microcks-testcontainers-dotnet" ` - /o:"microcks" ` - /d:sonar.token="${{ secrets.SONAR_TOKEN }}" ` - /d:sonar.host.url="https://sonarcloud.io" ` - /d:sonar.cs.opencover.reportsPaths="**/*.opencover.xml" ` - /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml ` - /v:"${{ inputs.version }}" - shell: pwsh - - - name: πŸ— Build - run: dotnet build --configuration Release --no-restore - - - name: πŸ§ͺ Test .NET - id: test - if: ${{ inputs.use-sonarcloud == false }} - run: | - dotnet test --no-build ` - --configuration Release ` - --logger trx ` - --collect:"XPlat Code Coverage" ` - --results-directory testresults - shell: pwsh - - - name: πŸ§ͺ Test .NET with coverage - id: test-with-coverage - if: ${{ inputs.use-sonarcloud == true }} - run: | - dotnet tool install --global dotnet-coverage - dotnet-coverage collect --output-format xml --output "coverage.xml" "dotnet test --no-build --configuration Release --logger trx --results-directory testresults" - shell: pwsh - - - name: Stop SonarQube Analysis - if: ${{ inputs.use-sonarcloud == true && (success() || steps.test-with-coverage.conclusion == 'failure') }} - id: sonar - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} - run: | - dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}" - - - name: πŸ“€ Upload Test And Coverage Results - uses: actions/upload-artifact@v4 - if: always() # run this step even if previous step failed - with: - name: ${{ inputs.runs-on }} - path: testresults - - - name: πŸ“¦ Nuget Pack - if: ${{ inputs.publish-package }} - run: | - dotnet pack ` - --include-source ` - --configuration Release ` - --no-build ` - --no-restore ` - --output ${{ github.workspace }}/nugets/ ` - -p:PackageVersion="${{ inputs.version }}" - shell: pwsh - - - name: πŸ“€ Upload Nuget Package - uses: actions/upload-artifact@v4 - if: ${{ inputs.publish-package }} - with: - if-no-files-found: error - name: nugets_${{ inputs.runs-on }} - path: nugets + build_test: + runs-on: ${{ inputs.runs-on }} + + outputs: + publish-package: ${{ inputs.publish-package }} + + steps: + - name: πŸ”„ Checkout + uses: actions/checkout@v4 + with: + lfs: true + fetch-depth: 0 + + - name: πŸ› οΈ Setup .NET + uses: actions/setup-dotnet@v4 + with: + global-json-file: global.json + + # We can remove this as JRE is automaticallu provisionned by Sonar + # - name: πŸ› οΈ Setup JDK 17 + # if: ${{ inputs.use-sonarcloud == true }} + # uses: actions/setup-java@v4.5.0 + # with: + # java-version: 17 + # distribution: 'zulu' + + - name: πŸ› οΈ Install SonarCloud scanner + if: ${{ inputs.use-sonarcloud == true }} + run: dotnet tool install --global dotnet-sonarscanner + + - name: πŸ”§ Restore .NET Tools + run: dotnet tool restore + + - name: πŸ”§ Restore dependencies + run: dotnet restore + + - name: πŸ” Start SonarQube Analysis + if: ${{ inputs.use-sonarcloud == true && (github.event.pull_request.head.repo.full_name == github.repository || github.event.pull_request.head.repo.full_name == '') }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + run: | + dotnet-sonarscanner begin ` + /k:"microcks_microcks-testcontainers-dotnet" ` + /o:"microcks" ` + /d:sonar.token="${{ secrets.SONAR_TOKEN }}" ` + /d:sonar.host.url="https://sonarcloud.io" ` + /d:sonar.cs.opencover.reportsPaths="**/*.opencover.xml" ` + /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml ` + /v:"${{ inputs.version }}" + shell: pwsh + + - name: πŸ— Build + run: dotnet build --configuration Release --no-restore + + - name: πŸ§ͺ Test .NET + id: test + if: ${{ inputs.use-sonarcloud == false }} + run: | + dotnet test --no-build ` + --configuration Release ` + --logger trx ` + --collect:"XPlat Code Coverage" ` + --results-directory testresults + shell: pwsh + + - name: πŸ§ͺ Test .NET with coverage + id: test-with-coverage + if: ${{ inputs.use-sonarcloud == true }} + run: | + dotnet tool install --global dotnet-coverage + dotnet-coverage collect --output-format xml --output "coverage.xml" "dotnet test --no-build --configuration Release --logger trx --results-directory testresults" + shell: pwsh + + - name: Stop SonarQube Analysis + if: ${{ inputs.use-sonarcloud == true && (success() || steps.test-with-coverage.conclusion == 'failure') && (github.event.pull_request.head.repo.full_name == github.repository || github.event.pull_request.head.repo.full_name == '') }} + id: sonar + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + run: | + dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}" + + - name: πŸ“€ Upload Test And Coverage Results + uses: actions/upload-artifact@v4 + if: always() # run this step even if previous step failed + with: + name: ${{ inputs.runs-on }} + path: testresults + + - name: πŸ“¦ Nuget Pack + if: ${{ inputs.publish-package }} + run: | + dotnet pack ` + --include-source ` + --configuration Release ` + --no-build ` + --no-restore ` + --output ${{ github.workspace }}/nugets/ ` + -p:PackageVersion="${{ inputs.version }}" + shell: pwsh + + - name: πŸ“€ Upload Nuget Package + uses: actions/upload-artifact@v4 + if: ${{ inputs.publish-package }} + with: + if-no-files-found: error + name: nugets_${{ inputs.runs-on }} + path: nugets +