From ac46c8831bb2952957c155350f80f0f27aa185ee Mon Sep 17 00:00:00 2001 From: Laz Date: Sun, 9 Jul 2023 20:58:35 +0100 Subject: [PATCH] cicd: correct workflow to download latest artifact --- .github/workflows/sonar-build.yml | 2 +- .github/workflows/sonar-scan.yml | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/.github/workflows/sonar-build.yml b/.github/workflows/sonar-build.yml index 14c715b96..aee326159 100644 --- a/.github/workflows/sonar-build.yml +++ b/.github/workflows/sonar-build.yml @@ -59,5 +59,5 @@ jobs: - name: Upload Sonar Build uses: actions/upload-artifact@v3 with: - name: sonar-build-PR + name: sonar-build path: ${{ env.BUILD_WRAPPER_OUT_DIR }} \ No newline at end of file diff --git a/.github/workflows/sonar-scan.yml b/.github/workflows/sonar-scan.yml index 134395b4a..7bd7b34c1 100644 --- a/.github/workflows/sonar-scan.yml +++ b/.github/workflows/sonar-scan.yml @@ -10,17 +10,40 @@ on: jobs: build: runs-on: windows-latest + if: github.event.workflow_run.conclusion == 'success' env: SONAR_SCANNER_VERSION: 4.6.1.2450 SONAR_SERVER_URL: "https://sonarcloud.io" BUILD_WRAPPER_OUT_DIR: build_wrapper_output_directory COVERAGE_RESULTS: coverage_results steps: - - name: Download Sonar Build - uses: actions/download-artifact@v3 + - uses: actions/checkout@v3 with: - name: sonar-build-PR - path: ${{ env.BUILD_WRAPPER_OUT_DIR }} + repository: ${{ github.event.workflow_run.head_repository.full_name }} + ref: ${{ github.event.workflow_run.head_branch }} + fetch-depth: 0 + - name: 'Download Artifacts' + uses: actions/github-script@v6 + with: + script: | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: context.payload.workflow_run.id, + }); + let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { + return artifact.name == "sonar-build" + })[0]; + let download = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: matchArtifact.id, + archive_format: 'zip', + }); + let fs = require('fs'); + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/sonar-build.zip`, Buffer.from(download.data)); + - name: 'Unzip Sonar Build' + run: Expand-Archive -Force sonar-build.zip ${{ env.BUILD_WRAPPER_OUT_DIR }} - name: Run sonar-scanner env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}