Skip to content

Commit ead7ab2

Browse files
authored
Update sonar evidence example (#80)
1 parent 82022c9 commit ead7ab2

File tree

5 files changed

+104
-518
lines changed

5 files changed

+104
-518
lines changed

.github/workflows/sonar-evidence-example.yml

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ on:
66
branches:
77
- CCS-2-Additional_evidence_examples
88
pull_request:
9-
branches:
10-
- CCS-2-Additional_evidence_examples
9+
branches:
10+
- CCS-2-Additional_evidence_examples
1111
permissions:
1212
id-token: write
1313
contents: read
@@ -25,56 +25,30 @@ jobs:
2525
echo "Job name: $GITHUB_JOB"
2626
2727
- name: Install jfrog cli
28-
id: setup-cli
28+
id: setup-cli
2929
uses: jfrog/setup-jfrog-cli@v4
3030
env:
3131
JF_URL: ${{ vars.ARTIFACTORY_URL }}
3232
with:
33-
oidc-provider-name: jfrog-github-oidc
33+
oidc-provider-name: jfrog-github-oidc
34+
version: latest
3435

3536
- uses: actions/checkout@v4
3637

37-
- name: Install SonarQube Scanner
38-
run: |
39-
curl -sL -sSLo sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-6.2.1.4610.zip
40-
unzip sonar-scanner.zip
41-
export PATH=$PATH:$PWD/sonar-scanner-6.2.1.4610/bin
42-
pwd
43-
ls -l $PWD/sonar-scanner-6.2.1.4610/bin/
44-
echo "$PWD/sonar-scanner-6.2.1.4610/bin"
45-
- name: Set up JDK 17
46-
uses: actions/setup-java@v4
47-
with:
48-
java-version: '21' # Specify the desired Java version here
49-
distribution: 'temurin' # You can also use 'temurin', 'zulu', etc.
50-
51-
- name: Run SonarScanner
52-
id: run-sonar-scanner
38+
- name: Sonar scan
39+
uses: SonarSource/sonarqube-scan-action@v5
40+
continue-on-error: true # Don't fail build on SonarCloud quality gate issues
5341
env:
5442
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
55-
run: |
56-
$PWD/sonar-scanner-6.2.1.4610/bin/sonar-scanner \
57-
-Dsonar.projectKey=test-evidence \
58-
-Dsonar.organization=my-evidence-test-org \
59-
-Dsonar.host.url=https://sonarcloud.io \
60-
-Dsonar.java.jdkHome=$JAVA_HOME \
61-
-Dsonar.verbose=true \
62-
-Dsonar.token=$SONAR_TOKEN
63-
# create evidence from sonar-scan analysis
64-
set +e
65-
# --FailOnAnalysisFailure causes a failure on gateway-failed sonar analysis
66-
./examples/sonar-scan/bin/sonar-scan-extractor-linux-amd64 --reportTaskFile=$PWD/.scannerwork/report-task.txt > predicate.json
67-
EXIT_CODE=$?
68-
set -e
69-
# write the exit code to the github output so that it can be used in the evidence creation step
70-
echo "------predicate.json------"
71-
cat predicate.json
72-
echo "------sonar-scan.log------"
73-
cat sonar-scan.log
74-
echo "------EXIT------"
75-
echo "create-sonar-evidence=$EXIT_CODE"
76-
echo "create-sonar-evidence=$EXIT_CODE" >> $GITHUB_OUTPUT
77-
43+
with:
44+
args: >
45+
-Dsonar.projectKey=my-evidence-test-org1_evidence-example
46+
-Dsonar.organization=my-evidence-test-org1
47+
-Dsonar.projectBaseDir=examples/sonar-scan
48+
-Dsonar.sources=.
49+
-Dsonar.inclusions=**/*.go
50+
-Dsonar.java.jdkHome=$JAVA_HOME
51+
-Dsonar.verbose=true
7852
7953
- name: Log in to Artifactory Docker Registry
8054
uses: docker/login-action@v3
@@ -109,14 +83,14 @@ jobs:
10983
jf rt build-publish
11084
11185
- name: Create evidence
112-
if: ${{ steps.run-sonar-scanner.outputs.create-sonar-evidence == 0 }}
113-
run: |
86+
env:
87+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
88+
SONAR_REPORT_TASK_FILE: ${{ github.workspace }}/examples/sonar-scan/.scannerwork/report-task.txt
89+
run: |
11490
# Attach evidence onto build using JFrog CLI
11591
jf evd create \
92+
--integration sonar \
11693
--build-name $GITHUB_WORKFLOW \
11794
--build-number "${{ github.run_number }}" \
118-
--predicate ./predicate.json \
119-
--predicate-type https://jfrog.com/evidence/sonar-scan/v1 \
120-
--provider-id "sonar" \
121-
--key "${{ secrets.JIRA_TEST_PKEY }}" \
122-
--key-alias ${{ vars.JIRA_TEST_KEY }}
95+
--key "${{ secrets.EVIDENCE_SIGNING_KEY }}" \
96+
--key-alias "${{ vars.EVIDENCE_PUBLIC_KEY_ALIAS }}"

examples/sonar-scan/README.md

Lines changed: 75 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,75 @@
1-
# Create Sonar Scan Evidence predicate from the build CI and attach it to the build info
2-
Sonar is a code scanning tool that helps to identify bugs, vulnerabilities, and code smells in your code.
3-
It is important to track the code quality and security of the code changes done and released.
4-
To allow automation of proper code quality and security checks, we create an evidence of the Sonar scan results
5-
during the build with confirmation that the code quality and security checks passed before the code was committed.
6-
using the `FailOnAnalysisFailure` argument the customer can decide if to create the sonar scan evidence if the scan did not pass
7-
sonar quality gates, or fail the predicate creation with exist status 1.
8-
If the Analysis status is not 'OK', but `FailOnAnalysisFailure` was not set, then the predicate is created with analysis.status = 'ERROR' which
9-
should be checked using a policy.
10-
11-
## Environment variables
12-
- `SONAR_TOKEN` - The sonar server token.
13-
- `SONAR_TYPE` - Should be Either SAAS or SELFHOSTED, defaulting to SAAS.
14-
- `SONAR_HOST_URL` - The sonar server host name, for example https://mysonar.mycorp.com, for example sonar.myconpany.org. required for SELFHOSTED type, if not provided for SAAS type sonarcloud.io is used as default.
15-
- `SONAR_PROXY_URL` - The proxy server URL, in the format of http://your-proxy-server:port. or https://username:password@your-proxy-server:port
16-
17-
## Arguments
18-
`--reportTaskFile=<path>` - The path to the sonar report task file.
19-
`--FailOnAnalysisFailure` - Fail with exit code 1 if the sonar analysis failed in sonar quality gate.
20-
`--WaitTime=<seconds>` - between sonar analysis results checks>
21-
`--MaxRetries=<number>` - The maximum number of retries to check the sonar analysis results.
22-
`--UseProxy` - Use a proxy server URL, requires PROXY_URL environment variable to be set.
23-
24-
## The example is based on the following steps:
25-
1. set sonar token as an environment variable
26-
2. call sonar scan
27-
for example:
28-
``
29-
$PWD/sonar-scanner-6.2.1.4610/bin/sonar-scanner \
30-
-Dsonar.projectKey=my-sonar-project-key \
31-
-Dsonar.organization=my-sonar-org \
32-
-Dsonar.host.url=https://sonarcloud.io \
33-
-Dsonar.java.jdkHome=$JAVA_HOME \
34-
-Dsonar.verbose=true \
35-
-Dsonar.token=$SONAR_TOKEN
36-
``
37-
3. call the jira-transition-checker utility (use the binary for your build platform) with these arguments: "transition name" JIRA-ID [,JIRA-ID]
38-
for example:
39-
``./examples/sonar-scan/bin/sonar-scan-extractor-linux-amd64 --reportTaskFile=$PWD/.scannerwork/report-task.txt --FailOnAnalysisFailure > predicate.json
40-
``
41-
4. call the evidence create cli with the predicate.json file
42-
for example:
43-
``
44-
jf evd create \
45-
--build-name $GITHUB_WORKFLOW \
46-
--build-number "${{ github.run_number }}" \
47-
--predicate ./predicate.json \
48-
--predicate-type https://jfrog.com/evidence/sonar-scan/v1 \
49-
--provider-id "sonar" \
50-
--key "${{ secrets.JIRA_TEST_PKEY }}" \
51-
--key-alias ${{ vars.JIRA_TEST_KEY }}
52-
``
53-
54-
## Additional considerations
55-
```plaintext
56-
It is advised to decide if to create an evidence with sonar analysis failure scan or refrain from creating the evidence.
57-
to create the evidence with an analysis gateway failure content, do **not** add the `--FailOnAnalysisFailure` argument.
58-
59-
to refrain from creating the evidence with an analysis gateway failure content, add the `--FailOnAnalysisFailure` argument.
60-
then check the exit code of the script and decide if to create the evidence or not.
61-
```
1+
## Sonar evidence creation
2+
3+
This example shows how to create and attach Sonar analysis evidence using the JFrog CLI.
4+
5+
### Prerequisites
6+
- SONAR_TOKEN: SonarCloud/SonarQube token.
7+
- A completed Sonar scan that produced a `report-task.txt` file.
8+
9+
### Default report-task.txt discovery
10+
When you run:
11+
```bash
12+
jf evd create --integration sonar
13+
```
14+
the tool auto-detects the Sonar task file using these paths (in order):
15+
- target/sonar/report-task.txt (Maven)
16+
- build/sonar/report-task.txt (Gradle)
17+
- .scannerwork/report-task.txt (CLI scanner)
18+
- .sonarqube/out/.sonar/report-task.txt (MSBuild)
19+
20+
If the file is not found, configure its location via YAML or env var (see below).
21+
22+
### Minimal workflow step (example)
23+
```yaml
24+
- name: Create evidence
25+
env:
26+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
27+
run: |
28+
jf evd create \
29+
--build-name $GITHUB_WORKFLOW \
30+
--build-number "${{ github.run_number }}" \
31+
--key "${{ secrets.SIGNING_KEY }}" \
32+
--key-alias ${{ vars.SIGNING_KEY_ALIAS }} \
33+
--integration sonar
34+
```
35+
36+
### Configuration
37+
You can configure the integration via YAML or environment variables. YAML keys have 1:1 env equivalents.
38+
39+
1) YAML: .jfrog/evidence/evidence.yml
40+
```yaml
41+
sonar:
42+
url: https://sonarcloud.io
43+
reportTaskFile: .scannerwork/report-task.txt
44+
pollingMaxRetries: 30
45+
pollingRetryIntervalMs: 5000
46+
```
47+
48+
2) Environment variables
49+
- SONAR_URL
50+
- SONAR_REPORT_TASK_FILE
51+
- SONAR_POLLING_MAX_RETRIES
52+
- SONAR_POLLING_RETRY_INTERVAL_MS
53+
54+
### Parameters reference
55+
Evidence creation:
56+
57+
- --integration sonar
58+
- Selects the Sonar integration.
59+
60+
Sonar resolution (via YAML/env):
61+
62+
- sonar.url / SONAR_URL
63+
- Sonar base URL. By default url is parsed from report-task.txt. If not found there, defaults to https://sonarcloud.io.
64+
65+
- sonar.reportTaskFile / SONAR_REPORT_TASK_FILE
66+
- Path to report-task.txt. If omitted, auto-detection (see order above) is used.
67+
68+
- sonar.pollingMaxRetries / SONAR_POLLING_MAX_RETRIES
69+
- Maximum polling attempts to wait for analysis results.
70+
71+
- sonar.pollingRetryIntervalMs / SONAR_POLLING_RETRY_INTERVAL_MS
72+
- Milliseconds to wait between polling attempts.
73+
74+
### Behavior
75+
Evidence is created for both successful and failed Sonar analyses (including failed quality gates).

examples/sonar-scan/build-binary.sh

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)