Skip to content

Sonarcloud-Pull

Sonarcloud-Pull #127

name: Sonarcloud-Pull
on:
workflow_run:
workflows: ["Code Analysis"]
types:
- completed
env:
COPR_REPO: ${{ vars.COPR_REPO || '@pki/master' }}
jobs:
retrieve-pr:
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
outputs:
pr-number: ${{ steps.pr-artifact-script.outputs.result }}
pr-base: ${{ steps.pr-base-script.outputs.result }}
steps:
- name: Download PR artifact
uses: actions/github-script@v3.1.0
id: download-pr
with:
result-encoding: string
script: |
var artifacts = await github.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr"
})[0];
if (matchArtifact == null){
core.setFailed("No PR artifact");
return "False";
}
var download = await github.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
var fs = require('fs');
fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data));
return "True";
- name: Unzip the PR
if: steps.download-pr.outputs.result == 'True'
run: unzip pr.zip
- name: Retrieve the PR number
if: success()
id: pr-artifact-script
uses: actions/github-script@v3.1.0
with:
result-encoding: string
script: |
var fs = require('fs');
var pr_number = Number(fs.readFileSync('./NR'));
return pr_number;
- name: Retrieve the PR base
if: success()
id: pr-base-script
uses: actions/github-script@v3.1.0
with:
result-encoding: string
script: |
var fs = require('fs');
var pr_base = fs.readFileSync('./BaseBranch');
return pr_base;
init:
name: Initialization
needs: retrieve-pr
uses: ./.github/workflows/init.yml
secrets: inherit
build:
name: Building TomcatJSS
needs: [init, retrieve-pr]
runs-on: ubuntu-latest
steps:
- name: Clone the repository
uses: actions/checkout@v3
with:
repository: ${{ github.event.workflow_run.head_repository.full_name }}
ref: ${{ github.event.workflow_run.head_branch }}
fetch-depth: 0
- name: Rebase PR
run: |
git config user.name "GitHub Workflow Action"
git remote add tomcatjss ${{ github.event.repository.clone_url }}
git fetch tomcatjss
git rebase tomcatjss/${{ needs.retrieve-pr.outputs.pr-base }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build runner image
uses: docker/build-push-action@v2
with:
context: .
build-args: |
BASE_IMAGE=${{ needs.init.outputs.base-image }}
COPR_REPO=${{ env.COPR_REPO }}
tags: tomcatjss-runner
target: tomcatjss-runner
outputs: type=docker,dest=sonar-runner.tar
- name: Store runner image
uses: actions/cache@v3
with:
key: sonar-runner-${{ github.event.workflow_run.id }}
path: sonar-runner.tar
sonarcloud:
name: SonarCloud
needs: [retrieve-pr, init, build]
if: needs.retrieve-pr.outputs.pr-number != ''
runs-on: ubuntu-latest
env:
SHARED: /tmp/workdir/ldapjdk
steps:
- name: Retrieve runner image
uses: actions/cache@v3
with:
key: sonar-runner-${{ github.event.workflow_run.id }}
path: sonar-runner.tar
- name: Load runner image
run: docker load --input sonar-runner.tar
- name: Clone the repository
uses: actions/checkout@v3
with:
repository: ${{ github.event.workflow_run.head_repository.full_name }}
ref: ${{ github.event.workflow_run.head_branch }}
fetch-depth: 0
- name: Rebase PR
run: |
git config user.name "GitHub Workflow Action"
git remote add tomcatjss ${{ github.event.repository.clone_url }}
git fetch tomcatjss
git rebase tomcatjss/${{ needs.retrieve-pr.outputs.pr-base }}
- name: Run container
run: |
IMAGE=tomcatjss-runner \
NAME=pki \
tests/bin/runner-init.sh
- name: Copy builds in current folder
run: |
mkdir build
docker cp pki:/usr/share/java/tomcatjss.jar build/
- name: Remove maven related file
run: rm -f pom.xml
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
args: >
-Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }}
-Dsonar.pullrequest.key=${{ needs.retrieve-pr.outputs.pr-number }}
-Dsonar.pullrequest.branch=${{ github.event.workflow_run.head_branch }}
-Dsonar.pullrequest.base=${{ github.event.workflow_run.pull_requests[0].base.ref }}