Skip to content

Commit 3f17d24

Browse files
committed
ci : fix failing sonar pipeline by moving sonar publish step to workflow_run
Sonar analysis is no longer working on pull requests as sonar login token is not propagated to pull requests. In order to perform analysis, split the github action into two actions: - `Sonar PR Report Request` : Will run on PR, but would only verify build is ok - `Sonar PR Report Publish` : Will run after completion of previous workflow, but on main repository that would have required secrets. Signed-off-by: Rohan Kumar <[email protected]>
1 parent 5f05141 commit 3f17d24

File tree

4 files changed

+122
-5
lines changed

4 files changed

+122
-5
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#
2+
# Copyright (C) 2015 Red Hat, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
name: Sonar PR Report Publish
18+
19+
on:
20+
workflow_run:
21+
workflows: [Sonar PR Report Request]
22+
types:
23+
- completed
24+
25+
concurrency:
26+
# Only run once for latest commit per ref and cancel other (previous) runs.
27+
group: ci-sonar-kubernetes-client-${{ github.ref }}
28+
cancel-in-progress: true
29+
30+
permissions:
31+
contents: read
32+
33+
jobs:
34+
sonar:
35+
name: Sonar
36+
runs-on: ubuntu-latest
37+
env:
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
BASE_BRANCH: main
40+
PR_BRANCH: ${{ github.event.workflow_run.head_branch }}
41+
PR_AUTHOR: ${{ github.event.sender.login }}
42+
GITHUB_REPO: ${{ github.repository }}
43+
SONAR_LOGIN_TOKEN: ${{ secrets.SONAR_LOGIN_TOKEN }}
44+
steps:
45+
- name: Setup Java 17
46+
uses: actions/setup-java@v4
47+
with:
48+
java-version: '17'
49+
distribution: 'temurin'
50+
- name: Get PR number
51+
run: |
52+
PR_QUERY_RESULT=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
53+
"https://api.github.com/repos/$GITHUB_REPO/pulls?head=$PR_AUTHOR:$PR_BRANCH&state=open" | jq '.[0].number')
54+
if [ "$PR_QUERY_RESULT" = "null" ] || [ -z "$PR_QUERY_RESULT" ]; then
55+
echo "Could not find PR number for $PR_AUTHOR:$PR_BRANCH"
56+
exit 1
57+
fi
58+
echo "PR_NUMBER=$PR_QUERY_RESULT" >> $GITHUB_ENV
59+
- name: Checkout
60+
uses: actions/checkout@v4
61+
with:
62+
ref: refs/pull/${{ env.PR_NUMBER }}/head
63+
# Shallow clones should be disabled for a better relevancy of analysis
64+
fetch-depth: 0
65+
- name: Sonar
66+
run: make sonar-pr-report
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#
2+
# Copyright (C) 2015 Red Hat, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
name: Sonar PR Report Request
18+
19+
on:
20+
pull_request:
21+
paths-ignore:
22+
- 'doc/**'
23+
- 'ide-config/**'
24+
- '**.md'
25+
26+
concurrency:
27+
# Only run once for latest commit per ref and cancel other (previous) runs.
28+
group: ci-sonar-kubernetes-client-${{ github.ref }}
29+
cancel-in-progress: true
30+
31+
permissions:
32+
contents: read
33+
34+
jobs:
35+
sonar:
36+
name: Sonar
37+
runs-on: ubuntu-latest
38+
steps:
39+
- name: Checkout
40+
uses: actions/checkout@v4
41+
- name: Setup Java 17
42+
uses: actions/setup-java@v4
43+
with:
44+
java-version: '17'
45+
distribution: 'temurin'
46+
- name: Build Project before requesting for Sonar Analysis
47+
run: mvn ${MAVEN_ARGS} install

.github/workflows/sonar.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ on:
2020
push:
2121
branches:
2222
- main
23-
# pull_request:
24-
# paths-ignore:
25-
# - 'doc/**'
26-
# - 'ide-config/**'
27-
# - '**.md'
2823

2924
concurrency:
3025
# Only run once for latest commit per ref and cancel other (previous) runs.

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ sonar: clean
6565
# $(MAVEN_ARGS) ---> -T 1C won't work with sonar analysis (yet)
6666
mvn -Psonar install sonar:sonar
6767

68+
.PHONY: sonar-pr-report
69+
sonar-pr-report: clean
70+
mvn -Psonar install sonar:sonar \
71+
-Dsonar.login=${SONAR_LOGIN_TOKEN} \
72+
-Dsonar.pullrequest.key=${PR_NUMBER} \
73+
-Dsonar.pullrequest.branch=${PR_BRANCH} \
74+
-Dsonar.pullrequest.base=${BASE_BRANCH} \
75+
-Dsonar.pullrequest.provider=GitHub
76+
6877
.PHONY: javadoc
6978
javadoc: clean
7079
mvn $(MAVEN_ARGS) install javadoc:jar -DskipTests -Pjavadoc-test

0 commit comments

Comments
 (0)