pipeline {
agent any
environment {
VERSION = "1.1.1"
RELEASE_DATE = "15-05-2025"
TARGET_ENV = 'dev'
SVN_SERVER_PROD = 'https://SW-RTP-SVNv.smartwiregrid.com/svn/'
SVN_CREDENTIALS_PROD = '844d4b46-bdfb-49e7-8c82-cc89a20dd5bc'
SVN_SERVER_DEV = 'https://vsvn-server-lhr/svn/'
SVN_CREDENTIALS_DEV = '9d53e576-2e3a-4713-bb22-4fd2d7dde432'
SVN_BASE_PATH = 'RTPProjects/CBM/Frontend/source_code_testing'
WORKSPACE_DIR = "D:/testing jenkins"
}
stages {
stage('Prepare Environment') {
steps {
script {
env.JOB_RESULTS_FILE = "${env.WORKSPACE_DIR}/pipeline_logs.txt"
echo "Workspace folder: ${env.WORKSPACE_DIR}"
echo "Log File Path: ${env.JOB_RESULTS_FILE}"
appendToFile("********** Pipeline Initiated v${env.VERSION} for
${env.TARGET_ENV} environment **********\n")
}
}
}
stage('SVN Checkout') {
steps {
script {
def SVN_PATH = "${env.SVN_BASE_PATH}"
def REVISION = "head"
checkoutProject('Frontend', SVN_PATH, REVISION)
}
}
}
stage('Install Dependencies') {
steps {
dir("${env.WORKSPACE_DIR}/Frontend/source_code_testing") {
script {
def nodeModulesExists = fileExists('node_modules')
if (nodeModulesExists) {
echo "📦 Skipping npm install"
appendToFile("Skipped npm install as node_modules is
already present.\n")
} else {
bat 'npm install'
appendToFile("npm install completed.\n")
}
}
}
}
}
stage('Run Unit Tests & Generate Report') {
steps {
dir("${env.WORKSPACE_DIR}/Frontend/source_code_testing") {
script {
bat 'npx rimraf "jest-stare"'
bat 'npx rimraf "test-report"'
def testStatus = bat(script: 'npm run test:report',
returnStatus: true)
echo "📂 Debug: test-report folder"
bat 'if exist test-report (dir test-report) else (echo
test-report NOT FOUND)'
echo "📂 Debug: jest-stare folder"
bat 'if exist jest-stare (dir jest-stare) else (echo jest-
stare NOT FOUND)'
if (testStatus == 0) {
appendToFile("Tests completed successfully.\n")
} else {
appendToFile("Test execution failed.\n")
}
junit allowEmptyResults: true, testResults:
'jest-stare/junit.xml'
}
}
}
}
stage('Build Production') {
steps {
dir("${env.WORKSPACE_DIR}/Frontend/source_code_testing") {
script {
def buildExists = fileExists('build')
if (buildExists) {
echo "🚀 Skipping build"
appendToFile("Skipped build as folder already exists.\
n")
} else {
def result = bat(script: 'npm run winBuild',
returnStatus: true)
if (result != 0) {
def buildCheck = bat(script: 'if exist build\\*
(echo BUILD_SUCCESS) else (echo BUILD_FAILED)', returnStdout: true).trim()
if (buildCheck.contains('BUILD_SUCCESS')) {
appendToFile("Build completed with warnings.\
n")
} else {
appendToFile("Build failed after patch error.\
n")
error "Build actually failed"
}
} else {
appendToFile("Build completed successfully.\n")
}
}
}
}
}
}
}
post {
always {
dir("${env.WORKSPACE_DIR}/Frontend/source_code_testing") {
script {
echo "📦 Pipeline execution finished"
def reportPath1 = 'test-report'
def reportPath2 = 'jest-stare'
if (fileExists(reportPath1)) {
archiveArtifacts artifacts: "${reportPath1}/**"
publishHTML(target: [
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: reportPath1,
reportFiles: 'executive-summary-report.html',
reportName: 'Executive Summary Test Report'
])
} else {
echo "❌ test-report folder not found"
}
if (fileExists(reportPath2)) {
archiveArtifacts artifacts: "${reportPath2}/**"
publishHTML(target: [
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true,
reportDir: reportPath2,
reportFiles: 'index.html',
reportName: 'Jest-Stare Report'
])
} else {
echo "❌ jest-stare folder not found"
}
}
}
}
}
}
def appendToFile(String newText) {
def existingContent = ''
def filePath = "${env.JOB_RESULTS_FILE}"
if (fileExists(filePath)) {
existingContent = readFile(filePath)
}
def timestamp = new Date().format("yyyy-MM-dd HH:mm:ss")
def updatedContent = existingContent + "\n\n[${timestamp}] ${newText}"
env.BUILD_RESULTS = (env.BUILD_RESULTS ?: '') + "[${timestamp}] ${newText}"
writeFile file: filePath, text: updatedContent
}
def checkoutProject(String projectName, String svnPath, String revision) {
def svnUrl = (env.TARGET_ENV == 'prod') ? env.SVN_SERVER_PROD :
env.SVN_SERVER_DEV
def svnCredentials = (env.TARGET_ENV == 'prod') ? env.SVN_CREDENTIALS_PROD :
env.SVN_CREDENTIALS_DEV
def buildFolder = "${env.WORKSPACE_DIR}/${projectName}"
dir(buildFolder) {
try {
checkout([
$class: 'SubversionSCM',
locations: [[credentialsId: svnCredentials, remote: "${svnUrl}$
{svnPath}@HEAD"]]
])
echo "${projectName} checked out successfully"
appendToFile("${projectName} checked out successfully in $
{buildFolder}")
} catch (Exception e) {
echo "SVN checkout failed: ${e.message}"
appendToFile("SVN checkout failed: ${e.message}")
}
}
}