Skip to content

Commit 66b974e

Browse files
committed
refactor: split SC worflow into build & analysis
Signed-off-by: Juan Muñoz <[email protected]>
1 parent 5993c78 commit 66b974e

File tree

2 files changed

+172
-71
lines changed

2 files changed

+172
-71
lines changed

.github/workflows/sonarcloud-analysis.yml

+95-71
Original file line numberDiff line numberDiff line change
@@ -4,82 +4,106 @@
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:
2113
sonar-check:
22-
name: Sonar Check
2314
runs-on: ubuntu-latest
15+
if: github.event.workflow_run.conclusion == 'success'
2416
timeout-minutes: 15
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-
33-
- name: Cache SonarCloud packages
34-
uses: actions/cache@v3
35-
with:
36-
path: ~/.sonar/cache
37-
key: ${{ runner.os }}-sonar
38-
restore-keys: ${{ runner.os }}-sonar
39-
- name: Cache Maven packages
40-
uses: actions/cache@v3
41-
with:
42-
path: ~/.m2
43-
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
44-
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/[email protected]
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+
- name: Checkout base branch
49+
if: github.event.workflow_run.event == 'pull_request'
50+
run: |
51+
git remote add upstream ${{ github.event.repository.clone_url }}
52+
git fetch upstream
53+
git checkout -B ${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} upstream/${{ fromJson(steps.get_pr_data.outputs.data).base.ref }}
54+
git checkout ${{ github.event.workflow_run.head_branch }}
55+
git clean -ffdx && git reset --hard HEAD
56+
- name: Cache SonarCloud packages
57+
uses: actions/cache@v3
58+
with:
59+
path: ~/.sonar/cache
60+
key: ${{ runner.os }}-sonar
61+
restore-keys: ${{ runner.os }}-sonar
62+
- name: Cache Maven packages
63+
uses: actions/cache@v3
64+
with:
65+
path: ~/.m2
66+
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
67+
restore-keys: ${{ runner.os }}-m2
4568

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

52-
- name: Set Common Sonar Variables
53-
id: sonar_env
54-
run: |
55-
echo "##[set-output name=sonar_opts;]$(echo -Dsonar.host.url=https://sonarcloud.io \
56-
-Dsonar.projectKey=com-pas_compas-scl-data-service \
57-
-Dsonar.organization=com-pas )"
58-
- name: Create custom Maven Settings.xml
59-
uses: whelk-io/maven-settings-xml-action@v21
60-
with:
61-
output_file: custom_maven_settings.xml
62-
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'
63-
- name: Build and analyze (Pull Request)
64-
if: ${{ github.event_name == 'pull_request' || (github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target') }}
65-
env:
66-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
67-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
68-
run: |
69-
./mvnw -B -s custom_maven_settings.xml -Psonar \
70-
${{ steps.sonar_env.outputs.sonar_opts }} \
71-
-Dsonar.pullrequest.branch=${{ github.ref_name }} \
72-
-Dsonar.pullrequest.key=${{ github.event.pull_request.number }} \
73-
-Dsonar.pullrequest.base=${{ github.base_ref }} \
74-
-Dsonar.scm.revision=${{ github.event.pull_request.head.sha }} \
75-
clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
76-
- name: Build and analyze (Push)
77-
if: ${{ github.event_name == 'push' }}
78-
env:
79-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
80-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
81-
run: |
82-
./mvnw -B -s custom_maven_settings.xml -Psonar \
83-
${{ steps.sonar_env.outputs.sonar_opts }} \
84-
-Dsonar.branch.name=${{ github.ref_name }} \
85-
clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
75+
- name: Set Common Sonar Variables
76+
id: sonar_env
77+
run: |
78+
echo "##[set-output name=sonar_opts;]$(echo -Dsonar.host.url=https://sonarcloud.io \
79+
-Dsonar.projectKey=com-pas_compas-scl-data-service \
80+
-Dsonar.organization=com-pas )"
81+
- name: Create custom Maven Settings.xml
82+
uses: whelk-io/maven-settings-xml-action@v21
83+
with:
84+
output_file: custom_maven_settings.xml
85+
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'
86+
- name: Build and analyze (Pull Request)
87+
if: ${{ github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target') }}
88+
env:
89+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
90+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
91+
run: |
92+
./mvnw -B -s custom_maven_settings.xml -Psonar \
93+
${{ steps.sonar_env.outputs.sonar_opts }} \
94+
-Dsonar.pullrequest.branch=${{ fromJson(steps.get_pr_data.outputs.data).head.ref }} \
95+
-Dsonar.pullrequest.key=${{ fromJson(steps.get_pr_data.outputs.data).number }} \
96+
-Dsonar.pullrequest.base=${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} \
97+
-Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} \
98+
clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
99+
- name: Build and analyze (Push)
100+
if: ${{ github.event.workflow_run.event == 'push' }}
101+
env:
102+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
103+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
104+
run: |
105+
./mvnw -B -s custom_maven_settings.xml -Psonar \
106+
${{ steps.sonar_env.outputs.sonar_opts }} \
107+
-Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} \
108+
-Dsonar.branch.name=${{ github.event.workflow_run.head_branch }} \
109+
clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
33+
- name: Cache SonarCloud packages
34+
uses: actions/cache@v3
35+
with:
36+
path: ~/.sonar/cache
37+
key: ${{ runner.os }}-sonar
38+
restore-keys: ${{ runner.os }}-sonar
39+
- name: Cache Maven packages
40+
uses: actions/cache@v3
41+
with:
42+
path: ~/.m2
43+
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
44+
restore-keys: ${{ runner.os }}-m2
45+
46+
- name: Set up JDK 17
47+
uses: actions/setup-java@v3
48+
with:
49+
distribution: 'zulu'
50+
java-version: '17'
51+
52+
- name: Create custom Maven Settings.xml
53+
uses: whelk-io/maven-settings-xml-action@v21
54+
with:
55+
output_file: custom_maven_settings.xml
56+
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'
57+
- name: Build and analyze (Pull Request)
58+
if: ${{ github.event_name == 'pull_request' || (github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target') }}
59+
env:
60+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
61+
run: |
62+
./mvnw -B -s custom_maven_settings.xml clean verify
63+
- name: Build and analyze (Push)
64+
if: ${{ github.event_name == 'push' }}
65+
env:
66+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
67+
run: |
68+
./mvnw -B -s custom_maven_settings.xml clean verify
69+
- name: Save PR number to file
70+
if: github.event_name == 'pull_request'
71+
run: echo ${{ github.event.number }} > PR_NUMBER.txt
72+
- name: Archive PR number
73+
if: github.event_name == 'pull_request'
74+
uses: actions/upload-artifact@v3
75+
with:
76+
name: PR_NUMBER
77+
path: PR_NUMBER.txt

0 commit comments

Comments
 (0)