Skip to content

Sonarcloud-Pull

Sonarcloud-Pull #5

name: Sonarcloud-Pull
on:
workflow_run:
workflows: ["Code Analysis"]
types:
- completed
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: Initializing workflow
runs-on: ubuntu-latest
needs: retrieve-pr
outputs:
matrix: ${{ steps.init.outputs.matrix }}
repo: ${{ steps.init.outputs.repo }}
db-image: ${{ steps.init.outputs.db-image }}
steps:
- name: Clone repository
uses: actions/checkout@v3
- name: Initialize workflow
id: init
env:
BASE64_MATRIX: ${{ secrets.BASE64_MATRIX }}
BASE64_REPO: ${{ secrets.BASE64_REPO }}
BASE64_DATABASE: ${{ secrets.BASE64_DATABASE }}
run: |
tests/bin/init-workflow.sh
build:
name: Building PKI
needs: [init, retrieve-pr]
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJSON(needs.init.outputs.matrix) }}
steps:
- name: Clone 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 to master
run: |
git config --global --add safe.directory "$GITHUB_WORKSPACE"
git config user.name "GitHub Workflow Action"
git config user.email "dogtag-dev@redhat.com"
git remote add pki ${{ github.event.repository.clone_url }}
git fetch pki
git rebase pki/${{ needs.retrieve-pr.outputs.pr-base }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Cache Docker layers
id: cache-buildx
uses: actions/cache@v3
with:
key: buildx-${{ matrix.os }}-${{ hashFiles('pki.spec') }}
path: /tmp/.buildx-cache
- name: Build pki-deps image
uses: docker/build-push-action@v3
with:
context: .
build-args: |
OS_VERSION=${{ matrix.os }}
COPR_REPO=${{ needs.init.outputs.repo }}
tags: pki-deps
target: pki-deps
cache-to: type=local,dest=/tmp/.buildx-cache
if: steps.cache-buildx.outputs.cache-hit != 'true'
- name: Build pki-builder-deps image
uses: docker/build-push-action@v3
with:
context: .
build-args: |
OS_VERSION=${{ matrix.os }}
COPR_REPO=${{ needs.init.outputs.repo }}
tags: pki-builder-deps
target: pki-builder-deps
cache-to: type=local,dest=/tmp/.buildx-cache
if: steps.cache-buildx.outputs.cache-hit != 'true'
- name: Build runner image
uses: docker/build-push-action@v3
with:
context: .
build-args: |
OS_VERSION=${{ matrix.os }}
COPR_REPO=${{ needs.init.outputs.repo }}
tags: pki-runner
target: pki-runner
cache-from: type=local,src=/tmp/.buildx-cache
outputs: type=docker,dest=pki-runner.tar
- name: Store runner image
uses: actions/cache@v3
with:
key: pki-sonar-runner-${{ matrix.os }}-${{ github.event.workflow_run.id }}
path: pki-runner.tar
sonarcloud:
name: Sonar Cloud code analysis
needs: [retrieve-pr, init, build]
if: needs.retrieve-pr.outputs.pr-number != ''
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJSON(needs.init.outputs.matrix) }}
env:
SHARED: /tmp/workdir/pki
steps:
- name: Retrieve runner image
uses: actions/cache@v3
with:
key: pki-sonar-runner-${{ matrix.os }}-${{ github.event.workflow_run.id }}
path: pki-runner.tar
- name: Load runner image
run: docker load --input pki-runner.tar
- name: Checkout pulled branch
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 to master
run: |
git config user.name "GitHub Workflow Action"
git remote add pki ${{ github.event.repository.clone_url }}
git fetch pki
git rebase pki/${{ needs.retrieve-pr.outputs.pr-base }}
- name: Set up PKI container
run: |
tests/bin/runner-init.sh pki
- name: Copy build in current folder
run: docker cp pki:/usr/share/java/pki ./build
- name: Remove maven related file
run: rm -f pom.xml
- name: Start Sonar analysis
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
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 }}