Skip to content

Commit de6776b

Browse files
author
Pearl Dsilva
committed
Deduplicate sonal CI github workflow
1 parent 522283b commit de6776b

3 files changed

Lines changed: 118 additions & 84 deletions

File tree

.github/workflows/main-sonar-check.yml

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,15 @@ on:
2323
- main
2424

2525
permissions:
26-
contents: read # to fetch code (actions/checkout)
27-
pull-requests: write # for sonar to comment on pull-request
26+
contents: read
27+
pull-requests: write
2828

2929
jobs:
30-
build:
30+
sonar:
3131
if: github.repository == 'apache/cloudstack'
32-
name: Main Sonar JaCoCo Build
33-
runs-on: ubuntu-22.04
34-
steps:
35-
- uses: actions/checkout@v6
36-
with:
37-
fetch-depth: 0
38-
39-
- name: Set up JDK17
40-
uses: actions/setup-java@v5
41-
with:
42-
distribution: 'temurin'
43-
java-version: '17'
44-
cache: 'maven'
45-
46-
- name: Cache SonarCloud packages
47-
uses: actions/cache@v5
48-
with:
49-
path: ~/.sonar/cache
50-
key: ${{ runner.os }}-sonar
51-
restore-keys: ${{ runner.os }}-sonar
52-
53-
- name: Cache local Maven repository
54-
uses: actions/cache@v5
55-
with:
56-
path: ~/.m2/repository
57-
key: ${{ runner.os }}-m2-${{ hashFiles('pom.xml', '*/pom.xml', '*/*/pom.xml', '*/*/*/pom.xml') }}
58-
restore-keys: |
59-
${{ runner.os }}-m2
60-
61-
- name: Run Tests with Coverage
62-
env:
63-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
64-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
65-
run: |
66-
git clone https://github.com/shapeblue/cloudstack-nonoss.git nonoss
67-
cd nonoss && bash -x install-non-oss.sh && cd ..
68-
mvn -T$(nproc) -P quality -Dsimulator -Dnoredist clean install org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=apache_cloudstack
32+
uses: ./.github/workflows/sonar-reusable.yml
33+
with:
34+
is_pr: false
35+
secrets:
36+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

.github/workflows/sonar-check.yml

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,54 +20,19 @@ name: Sonar Quality Check
2020
on: [pull_request]
2121

2222
permissions:
23-
contents: read # to fetch code (actions/checkout)
24-
pull-requests: write # for sonar to comment on pull-request
23+
contents: read
24+
pull-requests: write
2525

2626
concurrency:
2727
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
2828
cancel-in-progress: true
2929

3030
jobs:
31-
build:
31+
sonar:
3232
if: github.repository == 'apache/cloudstack' && github.event.pull_request.head.repo.full_name == github.repository
33-
name: Sonar JaCoCo Coverage
34-
runs-on: ubuntu-22.04
35-
steps:
36-
- uses: actions/checkout@v6
37-
with:
38-
ref: "refs/pull/${{ github.event.number }}/merge"
39-
fetch-depth: 0
40-
41-
- name: Set up JDK17
42-
uses: actions/setup-java@v5
43-
with:
44-
distribution: 'temurin'
45-
java-version: '17'
46-
cache: 'maven'
47-
48-
- name: Cache SonarCloud packages
49-
uses: actions/cache@v5
50-
with:
51-
path: ~/.sonar/cache
52-
key: ${{ runner.os }}-sonar
53-
restore-keys: ${{ runner.os }}-sonar
54-
55-
- name: Cache local Maven repository
56-
uses: actions/cache@v5
57-
with:
58-
path: ~/.m2/repository
59-
key: ${{ runner.os }}-m2-${{ hashFiles('pom.xml', '*/pom.xml', '*/*/pom.xml', '*/*/*/pom.xml') }}
60-
restore-keys: |
61-
${{ runner.os }}-m2
62-
63-
- name: Run Build and Tests with Coverage
64-
id: coverage
65-
env:
66-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
67-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
68-
PR_ID: ${{ github.event.pull_request.number }}
69-
HEADREF: ${{ github.event.pull_request.head.ref }}
70-
run: |
71-
git clone https://github.com/shapeblue/cloudstack-nonoss.git nonoss
72-
cd nonoss && bash -x install-non-oss.sh && cd ..
73-
mvn -T$(nproc) -P quality -Dsimulator -Dnoredist clean install org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=apache_cloudstack -Dsonar.pullrequest.key="$PR_ID" -Dsonar.pullrequest.branch="$HEADREF" -Dsonar.pullrequest.github.repository=apache/cloudstack -Dsonar.pullrequest.provider=GitHub -Dsonar.pullrequest.github.summary_comment=true
33+
uses: ./.github/workflows/sonar-reusable.yml
34+
with:
35+
is_pr: true
36+
secrets:
37+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
38+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
name: Sonar Quality Check (Reusable)
19+
20+
on:
21+
workflow_call:
22+
inputs:
23+
is_pr:
24+
description: 'true when called from a pull_request trigger'
25+
type: boolean
26+
required: true
27+
secrets:
28+
GITHUB_TOKEN:
29+
required: true
30+
SONAR_TOKEN:
31+
required: false
32+
33+
permissions:
34+
contents: read
35+
pull-requests: write
36+
37+
jobs:
38+
build:
39+
name: Sonar JaCoCo Coverage
40+
runs-on: ubuntu-22.04
41+
steps:
42+
# PR callers check out the merge commit; branch callers use the pushed SHA.
43+
- uses: actions/checkout@v6
44+
with:
45+
ref: ${{ inputs.is_pr && format('refs/pull/{0}/merge', github.event.number) || github.sha }}
46+
fetch-depth: 0
47+
48+
- name: Set up JDK17
49+
uses: actions/setup-java@v5
50+
with:
51+
distribution: 'temurin'
52+
java-version: '17'
53+
cache: 'maven'
54+
55+
- name: Cache SonarCloud packages
56+
uses: actions/cache@v5
57+
with:
58+
path: ~/.sonar/cache
59+
key: ${{ runner.os }}-sonar
60+
restore-keys: ${{ runner.os }}-sonar
61+
62+
- name: Cache local Maven repository
63+
uses: actions/cache@v5
64+
with:
65+
path: ~/.m2/repository
66+
key: ${{ runner.os }}-m2-${{ hashFiles('pom.xml', '*/pom.xml', '*/*/pom.xml', '*/*/*/pom.xml') }}
67+
restore-keys: |
68+
${{ runner.os }}-m2
69+
70+
- name: Install Non-OSS
71+
run: |
72+
git clone https://github.com/shapeblue/cloudstack-nonoss.git nonoss
73+
cd nonoss && bash -x install-non-oss.sh && cd ..
74+
75+
- name: Run Build and Tests with Coverage (PR)
76+
if: inputs.is_pr
77+
env:
78+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
79+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
80+
PR_ID: ${{ github.event.pull_request.number }}
81+
HEADREF: ${{ github.event.pull_request.head.ref }}
82+
run: >
83+
mvn -T$(nproc) -P quality -Dsimulator -Dnoredist clean install
84+
org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
85+
-Dsonar.projectKey=apache_cloudstack
86+
-Dsonar.pullrequest.key="$PR_ID"
87+
-Dsonar.pullrequest.branch="$HEADREF"
88+
-Dsonar.pullrequest.github.repository=apache/cloudstack
89+
-Dsonar.pullrequest.provider=GitHub
90+
-Dsonar.pullrequest.github.summary_comment=true
91+
92+
- name: Run Tests with Coverage (Main)
93+
if: "!inputs.is_pr"
94+
env:
95+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
96+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
97+
run: >
98+
mvn -T$(nproc) -P quality -Dsimulator -Dnoredist clean install
99+
org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
100+
-Dsonar.projectKey=apache_cloudstack

0 commit comments

Comments
 (0)