Skip to content

Commit 0d5cb03

Browse files
authored
Merge pull request #207 from com-pas/scan_external_prs
refactor: split Sonarcloud worflow into build & analysis
2 parents e5c5e12 + f01b331 commit 0d5cb03

File tree

2 files changed

+175
-73
lines changed

2 files changed

+175
-73
lines changed

.github/workflows/sonarcloud-analysis.yml

+97-73
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,107 @@
44

55
name: SonarCloud Analysis
66

7-
on:
8-
push:
9-
branches:
10-
- '**'
11-
pull_request:
12-
branches:
13-
- 'main'
14-
- 'develop'
15-
pull_request_target:
16-
branches:
17-
- 'main'
18-
- 'develop'
7+
on:
8+
workflow_run:
9+
workflows: [SonarCloud Build]
10+
types: [completed]
1911

2012
jobs:
21-
build:
22-
name: SonarCloud
13+
sonar-check:
2314
runs-on: ubuntu-latest
15+
if: github.event.workflow_run.conclusion == 'success'
2416
timeout-minutes: 30
25-
26-
if: ${{ (github.event_name != 'pull_request_target' && github.actor != 'dependabot[bot]') || (github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target') }}
2717
steps:
28-
- name: Checkout
29-
uses: actions/checkout@v3
30-
with:
31-
fetch-depth: 0
32-
submodules: 'true'
33-
34-
- name: Cache SonarCloud packages
35-
uses: actions/cache@v3
36-
with:
37-
path: ~/.sonar/cache
38-
key: ${{ runner.os }}-sonar
39-
restore-keys: ${{ runner.os }}-sonar
40-
- name: Cache Maven packages
41-
uses: actions/cache@v3
42-
with:
43-
path: ~/.m2
44-
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
45-
restore-keys: ${{ runner.os }}-m2
18+
- name: echo event
19+
run: cat $GITHUB_EVENT_PATH
20+
- name: Download PR number artifact
21+
if: github.event.workflow_run.event == 'pull_request'
22+
uses: dawidd6/action-download-artifact@v2
23+
with:
24+
workflow: SonarCloud Build
25+
run_id: ${{ github.event.workflow_run.id }}
26+
name: PR_NUMBER
27+
- name: Read PR_NUMBER.txt
28+
if: github.event.workflow_run.event == 'pull_request'
29+
id: pr_number
30+
uses: juliangruber/read-file-action@v1
31+
with:
32+
path: ./PR_NUMBER.txt
33+
- name: Request GitHub API for PR data
34+
if: github.event.workflow_run.event == 'pull_request'
35+
uses: octokit/request-action@v2.x
36+
id: get_pr_data
37+
with:
38+
route: GET /repos/{full_name}/pulls/{number}
39+
number: ${{ steps.pr_number.outputs.content }}
40+
full_name: ${{ github.event.repository.full_name }}
41+
env:
42+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
- uses: actions/checkout@v3
44+
with:
45+
repository: ${{ github.event.workflow_run.head_repository.full_name }}
46+
ref: ${{ github.event.workflow_run.head_branch }}
47+
fetch-depth: 0
48+
submodules: 'true'
49+
- name: Checkout base branch
50+
if: github.event.workflow_run.event == 'pull_request'
51+
run: |
52+
git remote add upstream ${{ github.event.repository.clone_url }}
53+
git fetch upstream
54+
git checkout -B ${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} upstream/${{ fromJson(steps.get_pr_data.outputs.data).base.ref }}
55+
git checkout ${{ github.event.workflow_run.head_branch }}
56+
git clean -ffdx && git reset --hard HEAD
57+
- name: Cache SonarCloud packages
58+
uses: actions/cache@v3
59+
with:
60+
path: ~/.sonar/cache
61+
key: ${{ runner.os }}-sonar
62+
restore-keys: ${{ runner.os }}-sonar
63+
- name: Cache Maven packages
64+
uses: actions/cache@v3
65+
with:
66+
path: ~/.m2
67+
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
68+
restore-keys: ${{ runner.os }}-m2
4669

47-
- name: Set up JDK 17
48-
uses: actions/setup-java@v3
49-
with:
50-
distribution: 'zulu'
51-
java-version: '17'
70+
- name: Set up JDK 17
71+
uses: actions/setup-java@v3
72+
with:
73+
distribution: 'zulu'
74+
java-version: '17'
5275

53-
- name: Set Common Sonar Variables
54-
id: sonar_env
55-
run: |
56-
echo "##[set-output name=sonar_opts;]$(echo -Dsonar.host.url=https://sonarcloud.io \
57-
-Dsonar.projectKey=com-pas_compas-scl-validator \
58-
-Dsonar.organization=com-pas )"
59-
- name: Create custom Maven Settings.xml
60-
uses: whelk-io/maven-settings-xml-action@v21
61-
with:
62-
output_file: custom_maven_settings.xml
63-
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'
64-
- name: Build and analyze (Pull Request)
65-
if: ${{ github.event_name == 'pull_request' || (github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target') }}
66-
env:
67-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
68-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
69-
run: |
70-
./mvnw -B -s custom_maven_settings.xml -Psonar \
71-
${{ steps.sonar_env.outputs.sonar_opts }} \
72-
-Dsonar.pullrequest.branch=${{ github.ref_name }} \
73-
-Dsonar.pullrequest.key=${{ github.event.pull_request.number }} \
74-
-Dsonar.pullrequest.base=${{ github.base_ref }} \
75-
-Dsonar.scm.revision=${{ github.event.pull_request.head.sha }} \
76-
clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
77-
- name: Build and analyze (Push)
78-
if: ${{ github.event_name == 'push' }}
79-
env:
80-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
81-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
82-
run: |
83-
./mvnw -B -s custom_maven_settings.xml -Psonar \
84-
${{ steps.sonar_env.outputs.sonar_opts }} \
85-
-Dsonar.branch.name=${{ github.ref_name }} \
86-
clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
76+
- name: Set Common Sonar Variables
77+
id: sonar_env
78+
run: |
79+
echo "##[set-output name=sonar_opts;]$(echo -Dsonar.host.url=https://sonarcloud.io \
80+
-Dsonar.projectKey=com-pas_compas-scl-validator \
81+
-Dsonar.organization=com-pas )"
82+
- name: Create custom Maven Settings.xml
83+
uses: whelk-io/maven-settings-xml-action@v21
84+
with:
85+
output_file: custom_maven_settings.xml
86+
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'
87+
- name: Build and analyze (Pull Request)
88+
if: ${{ github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target') }}
89+
env:
90+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
91+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
92+
run: |
93+
./mvnw -B -s custom_maven_settings.xml -Psonar \
94+
${{ steps.sonar_env.outputs.sonar_opts }} \
95+
-Dsonar.pullrequest.branch=${{ fromJson(steps.get_pr_data.outputs.data).head.ref }} \
96+
-Dsonar.pullrequest.key=${{ fromJson(steps.get_pr_data.outputs.data).number }} \
97+
-Dsonar.pullrequest.base=${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} \
98+
-Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} \
99+
clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
100+
- name: Build and analyze (Push)
101+
if: ${{ github.event.workflow_run.event == 'push' }}
102+
env:
103+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
104+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
105+
run: |
106+
./mvnw -B -s custom_maven_settings.xml -Psonar \
107+
${{ steps.sonar_env.outputs.sonar_opts }} \
108+
-Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} \
109+
-Dsonar.branch.name=${{ github.event.workflow_run.head_branch }} \
110+
clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# SPDX-FileCopyrightText: 2022 Alliander N.V.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
name: SonarCloud Build
6+
7+
on:
8+
push:
9+
branches:
10+
- '**'
11+
pull_request:
12+
branches:
13+
- 'main'
14+
- 'develop'
15+
pull_request_target:
16+
branches:
17+
- 'main'
18+
- 'develop'
19+
20+
jobs:
21+
precheck-build:
22+
name: Pre Check Build
23+
runs-on: ubuntu-latest
24+
timeout-minutes: 30
25+
26+
if: ${{ (github.event_name != 'pull_request_target' && github.actor != 'dependabot[bot]') || (github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target') }}
27+
steps:
28+
- name: Checkout
29+
uses: actions/checkout@v3
30+
with:
31+
fetch-depth: 0
32+
submodules: 'true'
33+
34+
- name: Cache SonarCloud packages
35+
uses: actions/cache@v3
36+
with:
37+
path: ~/.sonar/cache
38+
key: ${{ runner.os }}-sonar
39+
restore-keys: ${{ runner.os }}-sonar
40+
- name: Cache Maven packages
41+
uses: actions/cache@v3
42+
with:
43+
path: ~/.m2
44+
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
45+
restore-keys: ${{ runner.os }}-m2
46+
47+
- name: Set up JDK 17
48+
uses: actions/setup-java@v3
49+
with:
50+
distribution: 'zulu'
51+
java-version: '17'
52+
53+
- name: Create custom Maven Settings.xml
54+
uses: whelk-io/maven-settings-xml-action@v21
55+
with:
56+
output_file: custom_maven_settings.xml
57+
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'
58+
- name: Build and analyze (Pull Request)
59+
if: ${{ github.event_name == 'pull_request' || (github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target') }}
60+
env:
61+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
62+
run: |
63+
./mvnw -B -s custom_maven_settings.xml clean verify
64+
- name: Build and analyze (Push)
65+
if: ${{ github.event_name == 'push' }}
66+
env:
67+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
68+
run: |
69+
./mvnw -B -s custom_maven_settings.xml clean verify
70+
- name: Save PR number to file
71+
if: github.event_name == 'pull_request'
72+
run: echo ${{ github.event.number }} > PR_NUMBER.txt
73+
- name: Archive PR number
74+
if: github.event_name == 'pull_request'
75+
uses: actions/upload-artifact@v3
76+
with:
77+
name: PR_NUMBER
78+
path: PR_NUMBER.txt

0 commit comments

Comments
 (0)