Skip to content

Commit 43fe7fd

Browse files
committed
fix: update sonar build and analysis
Signed-off-by: Nora Blomaard <n.blomaard@gmail.com>
1 parent 0281b85 commit 43fe7fd

2 files changed

Lines changed: 53 additions & 69 deletions

File tree

.github/workflows/sonarcloud-analysis.yml

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,28 @@ jobs:
1414
runs-on: ubuntu-latest
1515
if: github.event.workflow_run.conclusion == 'success'
1616
timeout-minutes: 15
17+
permissions:
18+
contents: read
19+
actions: read
20+
pull-requests: read
1721
steps:
1822
- name: echo event
1923
run: cat $GITHUB_EVENT_PATH
2024
- name: Download PR number artifact
21-
if: github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target')
25+
if: github.event.workflow_run.event == 'pull_request'
2226
uses: dawidd6/action-download-artifact@b6e2e70617bc3265edd6dab6c906732b2f1ae151 # v21
2327
with:
2428
workflow: SonarCloud Build
2529
run_id: ${{ github.event.workflow_run.id }}
2630
name: PR_NUMBER
2731
- name: Read PR_NUMBER.txt
28-
if: github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target')
32+
if: github.event.workflow_run.event == 'pull_request'
2933
id: pr_number
3034
uses: juliangruber/read-file-action@271ff311a4947af354c6abcd696a306553b9ec18 # v1
3135
with:
3236
path: ./PR_NUMBER.txt
3337
- name: Request GitHub API for PR data
34-
if: github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target')
38+
if: github.event.workflow_run.event == 'pull_request'
3539
uses: octokit/request-action@b91aabaa861c777dcdb14e2387e30eddf04619ae # v3.0.0
3640
id: get_pr_data
3741
with:
@@ -40,34 +44,17 @@ jobs:
4044
full_name: ${{ github.event.repository.full_name }}
4145
env:
4246
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43-
- name: Extract PR data
44-
if: github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target')
45-
id: pr_data
46-
env:
47-
DATA: ${{ steps.get_pr_data.outputs.data }}
48-
run: |
49-
echo "head_ref=$(echo "$DATA" | jq -r '.head.ref')" >> $GITHUB_OUTPUT
50-
echo "number=$(echo "$DATA" | jq -r '.number')" >> $GITHUB_OUTPUT
51-
echo "base_ref=$(echo "$DATA" | jq -r '.base.ref')" >> $GITHUB_OUTPUT
5247
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
5348
with:
54-
repository: ${{ github.event.workflow_run.head_repository.full_name }}
55-
ref: ${{ github.event.workflow_run.head_branch }}
5649
fetch-depth: 0
57-
- name: Checkout base branch
58-
if: github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target')
59-
env:
60-
CLONE_URL: ${{ github.event.repository.clone_url }}
61-
BASE_REF: ${{ steps.pr_data.outputs.base_ref }}
62-
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
63-
run: |
64-
git remote add upstream "$CLONE_URL"
65-
git fetch upstream
66-
git checkout -B "$BASE_REF" "upstream/$BASE_REF"
67-
git checkout "$HEAD_BRANCH"
68-
git clean -ffdx && git reset --hard HEAD
50+
- name: Download build artifacts
51+
uses: dawidd6/action-download-artifact@b6e2e70617bc3265edd6dab6c906732b2f1ae151 # v21
52+
with:
53+
workflow: SonarCloud Build
54+
run_id: ${{ github.event.workflow_run.id }}
55+
name: build-artifacts
6956
- name: Cache SonarCloud packages
70-
uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5
57+
uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5
7158
with:
7259
path: ~/.sonar/cache
7360
key: ${{ runner.os }}-sonar
@@ -85,46 +72,39 @@ jobs:
8572
with:
8673
distribution: 'zulu'
8774
java-version: '17'
75+
8876
- name: Set Common Sonar Variables
8977
id: sonar_env
9078
run: |
91-
echo "##[set-output name=sonar_opts;]$(echo -Dsonar.host.url=https://sonarcloud.io \
79+
echo "sonar_opts=-Dsonar.host.url=https://sonarcloud.io \
9280
-Dsonar.projectKey=com-pas_compas-scl-data-service \
93-
-Dsonar.organization=com-pas )"
81+
-Dsonar.organization=com-pas" >> $GITHUB_OUTPUT
9482
- name: Create custom Maven Settings.xml
9583
uses: whelk-io/maven-settings-xml-action@9dc09b23833fa9aa7f27b63db287951856f3433d # v22
9684
with:
9785
output_file: custom_maven_settings.xml
9886
servers: '[{ "id": "github-packages-compas", "username": "OWNER", "password": "${{ secrets.GITHUB_TOKEN }}" }]'
9987
- name: Build and analyze (Pull Request)
100-
if: ${{ github.event.workflow_run.event == 'pull_request' || (github.event.workflow_run.actor == 'dependabot[bot]' && github.event.workflow_run.event == 'pull_request_target') }}
88+
if: ${{ github.event.workflow_run.event == 'pull_request' }}
10189
env:
10290
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
10391
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
104-
SONAR_OPTS: ${{ steps.sonar_env.outputs.sonar_opts }}
105-
PR_HEAD_REF: ${{ steps.pr_data.outputs.head_ref }}
106-
PR_NUMBER: ${{ steps.pr_data.outputs.number }}
107-
PR_BASE_REF: ${{ steps.pr_data.outputs.base_ref }}
108-
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
10992
run: |
110-
./mvnw -B -s custom_maven_settings.xml -Psonar \
111-
$SONAR_OPTS \
112-
-Dsonar.pullrequest.branch="$PR_HEAD_REF" \
113-
-Dsonar.pullrequest.key="$PR_NUMBER" \
114-
-Dsonar.pullrequest.base="$PR_BASE_REF" \
115-
-Dsonar.scm.revision="$HEAD_SHA" \
116-
clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
93+
mvn -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+
org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
117100
- name: Build and analyze (Push)
118101
if: ${{ github.event.workflow_run.event == 'push' }}
119102
env:
120103
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
121104
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
122-
SONAR_OPTS: ${{ steps.sonar_env.outputs.sonar_opts }}
123-
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
124-
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
125105
run: |
126-
./mvnw -B -s custom_maven_settings.xml -Psonar \
127-
$SONAR_OPTS \
128-
-Dsonar.scm.revision="$HEAD_SHA" \
129-
-Dsonar.branch.name="$HEAD_BRANCH" \
130-
clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
106+
mvn -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+
org.sonarsource.scanner.maven:sonar-maven-plugin:sonar

.github/workflows/sonarcloud-build.yml

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@ on:
1212
branches:
1313
- 'main'
1414
- 'develop'
15-
pull_request_target:
16-
branches:
17-
- 'main'
18-
- 'develop'
1915

2016
jobs:
2117
precheck-build:
2218
name: Pre Check Build
2319
runs-on: ubuntu-latest
2420
timeout-minutes: 30
21+
permissions:
22+
contents: read
2523

26-
if: ${{ (github.event_name != 'pull_request_target' && github.actor != 'dependabot[bot]') || (github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target') }}
2724
steps:
2825
- name: Checkout
2926
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
@@ -61,25 +58,32 @@ jobs:
6158
with:
6259
output_file: custom_maven_settings.xml
6360
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') }}
61+
- name: Build
6662
env:
6763
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6864
run: |
69-
./mvnw -B -s custom_maven_settings.xml clean verify
70-
- name: Build and analyze (Push)
71-
if: ${{ github.event_name == 'push' }}
72-
env:
73-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
74-
run: |
75-
./mvnw -B -s custom_maven_settings.xml clean verify
65+
./mvnw -B -s custom_maven_settings.xml -Psonar clean verify
66+
- name: Upload build artifacts
67+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
68+
with:
69+
name: build-artifacts
70+
path: |
71+
api/src/
72+
app/src/
73+
repository/src/
74+
repository-postgresql/src/
75+
service/src/
76+
api/target/
77+
app/target/
78+
repository/target/
79+
repository-postgresql/target/
80+
service/target/
81+
retention-days: 1
7682
- name: Save PR number to file
77-
if: ${{ github.event_name == 'pull_request' || (github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target') }}
78-
env:
79-
PR_NUMBER: ${{ github.event.pull_request.number }}
80-
run: echo "$PR_NUMBER" > PR_NUMBER.txt
83+
if: github.event_name == 'pull_request'
84+
run: echo ${{ github.event.pull_request.number }} > PR_NUMBER.txt
8185
- name: Archive PR number
82-
if: ${{ github.event_name == 'pull_request' || (github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target') }}
86+
if: github.event_name == 'pull_request'
8387
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
8488
with:
8589
name: PR_NUMBER

0 commit comments

Comments
 (0)