Skip to content

Commit cd99691

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 599e895 + 0128eb7 commit cd99691

File tree

9 files changed

+318
-74
lines changed

9 files changed

+318
-74
lines changed

.github/workflows/build.yaml renamed to .github/workflows/build

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ name: Build
33

44
on:
55
push:
6-
pull_request:
76

87
jobs:
98
build:
@@ -17,6 +16,9 @@ jobs:
1716

1817
steps:
1918
- uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
21+
2022
- name: Use Node.js ${{ matrix.node-version }}
2123
uses: actions/setup-node@v4
2224
with:
@@ -48,3 +50,20 @@ jobs:
4850

4951
- name: Build
5052
run: pnpm run build
53+
54+
- name: Get Commit SHA (short)
55+
id: get_version
56+
run: |
57+
# Get the short 8-character commit SHA
58+
VERSION=$(git rev-parse --short=8 HEAD)
59+
echo "Commit SHA is $VERSION"
60+
echo "tag=$VERSION" >> $GITHUB_OUTPUT
61+
62+
- name: SonarQube Analysis (Branch)
63+
uses: SonarSource/sonarqube-scan-action@v6
64+
env:
65+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
66+
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
67+
with:
68+
args: >
69+
-Dsonar.projectVersion=${{ steps.get_version.outputs.tag }}

.github/workflows/build-pr

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
name: Build
3+
4+
on:
5+
pull_request:
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
11+
strategy:
12+
matrix:
13+
node-version: ['20.17.0']
14+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
15+
16+
environment:
17+
name: build-pr
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Use Node.js ${{ matrix.node-version }}
25+
uses: actions/setup-node@v4
26+
with:
27+
node-version: ${{ matrix.node-version }}
28+
29+
- name: Install pnpm
30+
uses: pnpm/action-setup@v2
31+
with:
32+
version: 8
33+
run_install: false
34+
35+
- name: Get pnpm store directory
36+
shell: bash
37+
run: |
38+
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
39+
40+
- name: Setup pnpm cache
41+
uses: actions/cache@v4
42+
with:
43+
path: |
44+
${{ env.STORE_PATH }}
45+
${{ github.workspace }}/.next/cache
46+
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}-${{ hashFiles('**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx') }}
47+
restore-keys: |
48+
${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}-
49+
50+
- name: Install dependencies
51+
run: pnpm install
52+
53+
- name: Build
54+
run: pnpm run build
55+
56+
- name: Get Commit SHA (short)
57+
id: get_version
58+
run: |
59+
# Get the short 8-character commit SHA
60+
VERSION=$(git rev-parse --short=8 HEAD)
61+
echo "Commit SHA is $VERSION"
62+
echo "tag=$VERSION" >> $GITHUB_OUTPUT
63+
64+
- name: SonarQube Analysis (Pull Request)
65+
uses: SonarSource/sonarqube-scan-action@v6
66+
with:
67+
args: >
68+
-Dsonar.projectVersion=${{ steps.get_version.outputs.tag }}
69+
-Dsonar.pullrequest.key=${{ github.event.pull_request.number }}
70+
-Dsonar.pullrequest.branch=${{ github.event.pull_request.head.ref }}
71+
-Dsonar.pullrequest.base=${{ github.event.pull_request.base.ref }}

.github/workflows/pr-docker-build.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ permissions: write-all
99
jobs:
1010
build-and-publish:
1111
runs-on: ubuntu-latest
12-
12+
13+
environment:
14+
name: build-pr
15+
1316
steps:
1417
- name: Checkout code
1518
uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
21+
ref: ${{ github.event.pull_request.head.sha }}
1622

1723
- name: Log in to GitHub Container Registry
1824
uses: docker/login-action@v3

Jenkins/Build.Jenkinsfile

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Declarative Pipeline for building Node.js application and running SonarQube analysis triggered by a push event.
2+
pipeline {
3+
// Defines the execution environment. Using 'agent any' to ensure an agent is available.
4+
agent any
5+
6+
// Global environment block removed to prevent Groovy scoping issues with manual path calculation.
7+
8+
stages {
9+
// Stage 1: Checkout the code (Relies on the initial SCM checkout done by Jenkins)
10+
stage('Source Checkout') {
11+
steps {
12+
echo "Workspace already populated by the initial SCM checkout. Proceeding."
13+
}
14+
}
15+
16+
// Stage 2: Setup Node.js v20 and install pnpm
17+
stage('Setup Environment and Tools') {
18+
steps {
19+
sh '''
20+
echo "Ensuring required utilities and Node.js are installed..."
21+
sudo apt-get update
22+
sudo apt-get install -y curl unzip nodejs
23+
24+
# 1. Install Node.js v20
25+
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
26+
sudo apt-get install -y nodejs
27+
echo "Node.js version: \$(node -v)"
28+
29+
# 2. Install pnpm globally (version 8)
30+
npm install -g pnpm@8
31+
echo "pnpm version: \$(pnpm -v)"
32+
'''
33+
}
34+
}
35+
36+
// Stage 3: Install dependencies and build the application
37+
stage('Install and Build') {
38+
steps {
39+
sh 'pnpm install'
40+
sh 'pnpm run build'
41+
}
42+
}
43+
44+
// Stage 4: Run SonarQube analysis: Install scanner, get version, and execute.
45+
stage('SonarQube Analysis') {
46+
steps {
47+
script {
48+
// 1. Get the short 8-character commit SHA for project versioning
49+
def commitShaShort = sh(returnStdout: true, script: 'git rev-parse --short=8 HEAD').trim()
50+
echo "Commit SHA (short) is: ${commitShaShort}"
51+
52+
// --- 2. MANUALLY INSTALL THE SONAR SCANNER CLI LOCALLY IN THIS STAGE ---
53+
sh """
54+
echo "Manually downloading and installing Sonar Scanner CLI..."
55+
56+
# Download the stable scanner CLI package
57+
curl -sS -o sonar-scanner.zip \
58+
"https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.7.0.2747.zip"
59+
60+
# Added -o flag to force overwrite and prevent interactive prompt failure
61+
unzip -o -q sonar-scanner.zip -d .
62+
"""
63+
64+
// 3. Find the extracted directory name and capture the full absolute bin path in Groovy
65+
// This is defined locally and used directly, avoiding environment variable issues.
66+
def scannerBinPath = sh(
67+
returnStdout: true,
68+
script: '''
69+
SCANNER_DIR=$(find . -maxdepth 1 -type d -name "sonar-scanner*" | head -n 1)
70+
# Get the full absolute path to the executable file
71+
echo \$(pwd)/\${SCANNER_DIR}/bin/sonar-scanner
72+
'''
73+
).trim()
74+
75+
echo "Scanner executable path captured: ${scannerBinPath}"
76+
77+
// 4. Use withSonarQubeEnv to set up the secure variables (HOST and TOKEN)
78+
withSonarQubeEnv(installationName: 'SonarQube-Server') {
79+
// 5. Execute the scanner using the Groovy variable directly.
80+
sh """
81+
echo "Starting SonarQube Analysis for project version: ${commitShaShort}"
82+
83+
# Execute the full, absolute path captured in the Groovy variable.
84+
'${scannerBinPath}' \\
85+
-Dsonar.projectVersion=${commitShaShort} \\
86+
-Dsonar.sources=.
87+
88+
# SONAR_HOST_URL and SONAR_TOKEN are automatically passed as environment variables
89+
# by the withSonarQubeEnv block.
90+
"""
91+
}
92+
}
93+
}
94+
}
95+
}
96+
}

Jenkins/BuildPR.Jenkinsfile

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Declarative Pipeline for building Node.js application and running SonarQube analysis for a Pull Request.
2+
pipeline {
3+
// Defines the execution environment. Using 'agent any' to ensure an agent is available.
4+
agent any
5+
6+
// Environment variables that hold PR details, provided by Jenkins Multibranch setup.
7+
environment {
8+
// FIX: Environment variables must be quoted or wrapped in a function call.
9+
// We quote the 'env.CHANGE_ID' reference to fix the compilation error.
10+
PR_KEY = "${env.CHANGE_ID}"
11+
PR_BRANCH = "${env.CHANGE_BRANCH}"
12+
PR_BASE = "${env.CHANGE_TARGET}"
13+
}
14+
15+
stages {
16+
// Stage 1: Checkout the code (Relies on the initial SCM checkout done by Jenkins)
17+
stage('Source Checkout') {
18+
steps {
19+
echo "Workspace already populated by the initial SCM checkout. Proceeding."
20+
}
21+
}
22+
23+
// Stage 2: Setup Node.js v20, install pnpm, and install required tools (curl, unzip)
24+
stage('Setup Environment and Tools') {
25+
steps {
26+
sh '''
27+
echo "Ensuring required utilities and Node.js are installed..."
28+
sudo apt-get update
29+
sudo apt-get install -y curl unzip nodejs
30+
31+
# 1. Install Node.js v20 (closest matching the specified version '20.17.0')
32+
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
33+
sudo apt-get install -y nodejs
34+
echo "Node.js version: \$(node -v)"
35+
36+
# 2. Install pnpm globally (version 8)
37+
npm install -g pnpm@8
38+
echo "pnpm version: \$(pnpm -v)"
39+
'''
40+
}
41+
}
42+
43+
// Stage 3: Install dependencies and build the application
44+
stage('Install and Build') {
45+
steps {
46+
sh 'pnpm install'
47+
sh 'pnpm run build'
48+
}
49+
}
50+
51+
// Stage 4: Run SonarQube PR analysis: Install scanner locally, get version, and execute.
52+
stage('SonarQube Pull Request Analysis') {
53+
steps {
54+
script {
55+
// 1. Get the short 8-character commit SHA for project versioning
56+
def commitShaShort = sh(returnStdout: true, script: 'git rev-parse --short=8 HEAD').trim()
57+
echo "Commit SHA (short) is: ${commitShaShort}"
58+
59+
// --- 2. MANUALLY INSTALL THE SONAR SCANNER CLI LOCALLY ---
60+
sh """
61+
echo "Manually downloading and installing Sonar Scanner CLI..."
62+
curl -sS -o sonar-scanner.zip \
63+
"https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.7.0.2747.zip"
64+
unzip -o -q sonar-scanner.zip -d .
65+
"""
66+
67+
// 3. Find the extracted directory name and capture the full absolute executable path.
68+
def scannerBinPath = sh(
69+
returnStdout: true,
70+
script: '''
71+
SCANNER_DIR=$(find . -maxdepth 1 -type d -name "sonar-scanner*" | head -n 1)
72+
# Get the full absolute path to the executable file
73+
echo \$(pwd)/\${SCANNER_DIR}/bin/sonar-scanner
74+
'''
75+
).trim()
76+
77+
echo "Scanner executable path captured: ${scannerBinPath}"
78+
79+
// 4. Use withSonarQubeEnv to set up the secure variables (HOST and TOKEN)
80+
withSonarQubeEnv(installationName: 'SonarQube-Server') {
81+
// 5. Execute the scanner using the Groovy variable directly with PR parameters.
82+
sh """
83+
echo "Starting SonarQube Pull Request Analysis for PR #${PR_KEY}"
84+
85+
'${scannerBinPath}' \\
86+
-Dsonar.projectVersion=${commitShaShort} \\
87+
-Dsonar.sources=. \\
88+
-Dsonar.pullrequest.key=${PR_KEY} \\
89+
-Dsonar.pullrequest.branch=${PR_BRANCH} \\
90+
-Dsonar.pullrequest.base=${PR_BASE}
91+
92+
# SONAR_HOST_URL and SONAR_TOKEN are automatically passed as environment variables
93+
# by the withSonarQubeEnv block.
94+
"""
95+
}
96+
}
97+
}
98+
}
99+
}
100+
}

Jenkinsfile

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

0 commit comments

Comments
 (0)