AS400_API_DOTNET/Jenkinsfile

214 lines
7.1 KiB
Plaintext
Raw Normal View History

2025-10-17 17:19:55 +07:00
pipeline {
2025-10-21 00:08:03 +07:00
agent any
options {
ansiColor('xterm')
timestamps()
}
2025-10-17 17:19:55 +07:00
environment {
2025-10-21 00:08:03 +07:00
// ถ้าใช้ local bare repo
GIT_URL = 'file:///repos/AS400API.git'
// Path ติดตั้ง dotnet ชั่วคราวใน pipeline
DOTNET_ROOT = "${WORKSPACE}/.dotnet"
PATH = "${DOTNET_ROOT}:${PATH}"
// Dependency-Check cache (แมพกับ volume/โฟลเดอร์บน Jenkins)
DC_DATA = "${JENKINS_HOME}/.dc-cache"
// ถ้าจะใช้ SonarQube ให้ตั้งค่าตามระบบจริง
// SONAR_HOST_URL = 'http://sonarqube:9000'
// SONAR_TOKEN = credentials('SONAR_TOKEN')
2025-10-17 17:19:55 +07:00
}
stages {
2025-10-21 00:08:03 +07:00
stage('Checkout') {
2025-10-20 12:15:23 +07:00
steps {
2025-10-21 00:08:03 +07:00
checkout([$class: 'GitSCM',
branches: [[name: '*/main']],
userRemoteConfigs: [[url: "${GIT_URL}"]]
])
2025-10-20 12:15:23 +07:00
}
}
2025-10-21 00:08:03 +07:00
stage('Install prerequisites') {
2025-10-17 17:19:55 +07:00
steps {
sh '''
2025-10-21 00:08:03 +07:00
set -euo pipefail
if command -v apt-get >/dev/null 2>&1; then
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y --no-install-recommends \
ca-certificates curl unzip jq git openjdk-21-jre-headless
2025-10-21 00:30:32 +07:00
# --- Install ICU runtime (Debian uses versioned package names) ---
if ! ldconfig -p | grep -qi libicu; then
PKG="$(apt-cache search '^libicu[0-9]+$' | awk '{print $1}' | head -n1 || true)"
if [ -n "$PKG" ]; then
echo "Installing ICU package: ${PKG}"
apt-get install -y --no-install-recommends "${PKG}"
else
# Fallback: dev package also provides the libs (heavier)
echo "Falling back to libicu-dev..."
apt-get install -y --no-install-recommends libicu-dev
fi
fi
2025-10-20 12:21:18 +07:00
fi
2025-10-21 00:30:32 +07:00
# Install .NET SDK locally for the build user
mkdir -p "${WORKSPACE}/.dotnet"
2025-10-21 00:08:03 +07:00
curl -fsSL https://dot.net/v1/dotnet-install.sh -o dotnet-install.sh
2025-10-21 00:30:32 +07:00
bash dotnet-install.sh --channel 9.0 --install-dir "${WORKSPACE}/.dotnet"
2025-10-21 00:08:03 +07:00
2025-10-21 00:30:32 +07:00
export PATH="${WORKSPACE}/.dotnet:${PATH}"
dotnet --info
2025-10-20 12:01:53 +07:00
'''
}
}
2025-10-21 00:08:03 +07:00
stage('SCA (NuGet + OWASP)') {
2025-10-20 12:01:53 +07:00
steps {
sh '''
2025-10-20 12:32:04 +07:00
set -euo pipefail
2025-10-17 17:52:49 +07:00
echo "=== NuGet vulnerability audit ==="
2025-10-20 12:01:53 +07:00
dotnet restore
2025-10-17 17:19:55 +07:00
dotnet list package --vulnerable || true
2025-10-21 00:08:03 +07:00
echo "=== OWASP Dependency-Check ==="
2025-10-21 08:03:24 +07:00
rm -rf depcheck dependency-check
2025-10-17 17:19:55 +07:00
mkdir -p depcheck
2025-10-20 12:32:04 +07:00
API="https://api.github.com/repos/jeremylong/DependencyCheck/releases/latest"
2025-10-21 00:08:03 +07:00
echo "Resolving latest Dependency-Check..."
ASSET_URL=$(curl -fsSL "$API" | jq -r '.assets[]?.browser_download_url | select(test("release\\\\.zip$"))' | head -n1)
2025-10-20 12:32:04 +07:00
echo "Downloading: $ASSET_URL"
curl -fL --retry 3 --retry-all-errors -o depcheck.zip "$ASSET_URL"
2025-10-21 08:03:24 +07:00
unzip -oq depcheck.zip -d dependency-check
2025-10-20 12:32:04 +07:00
2025-10-21 00:08:03 +07:00
DC_BIN="dependency-check/dependency-check/bin/dependency-check.sh"
2025-10-20 12:32:04 +07:00
2025-10-21 00:08:03 +07:00
# อัปเดตฐานข้อมูลครั้งแรก (และทุกครั้งที่ cache ว่าง)
bash "$DC_BIN" --data "${DC_DATA}" --updateonly || true
2025-10-17 17:52:49 +07:00
2025-10-21 00:08:03 +07:00
# สแกนจริง (เร็ว เพราะใช้ cache)
bash "$DC_BIN" -f HTML -f XML \
--project "AS400API" \
--scan . \
--out depcheck \
--data "${DC_DATA}" \
--noupdate || true
2025-10-17 17:19:55 +07:00
'''
2025-10-17 17:52:49 +07:00
}
post {
always {
archiveArtifacts artifacts: 'depcheck/**', allowEmptyArchive: true
2025-10-21 00:08:03 +07:00
// ถ้ามี HTML Publisher plugin จะโชว์รายงานสวยขึ้น
2025-10-21 08:03:24 +07:00
script {
try {
publishHTML(target: [
reportName: 'OWASP Dependency-Check',
reportDir: 'depcheck',
reportFiles: 'dependency-check-report.html',
keepAll: true,
alwaysLinkToLastBuild: true,
allowMissing: true
])
} catch (Throwable e) {
echo "Skipping HTML report publish (plugin unavailable?): ${e.getClass().getSimpleName()}"
}
}
2025-10-17 17:52:49 +07:00
}
2025-10-17 17:19:55 +07:00
}
}
2025-10-21 00:08:03 +07:00
stage('SAST') {
2025-10-17 17:19:55 +07:00
steps {
2025-10-20 12:38:29 +07:00
sh '''
2025-10-21 00:08:03 +07:00
set -euo pipefail
if [ -n "${SONAR_HOST_URL:-}" ] && [ -n "${SONAR_TOKEN:-}" ]; then
echo "=== SAST with SonarQube ==="
# ถ้าใช้ sonarscanner for .NET (แนะนำ)
dotnet tool update --global dotnet-sonarscanner || dotnet tool install --global dotnet-sonarscanner
export PATH="$HOME/.dotnet/tools:${PATH}"
dotnet-sonarscanner begin \
/k:"AS400API" \
/d:sonar.host.url="${SONAR_HOST_URL}" \
/d:sonar.login="${SONAR_TOKEN}"
dotnet build -c Release
dotnet-sonarscanner end /d:sonar.login="${SONAR_TOKEN}"
else
echo "=== SAST with Roslyn analyzers (no Sonar) ==="
# เปิด .NET analyzers และ treat warnings เป็น error
dotnet build -c Release \
-p:EnableNETAnalyzers=true \
-p:TreatWarningsAsErrors=true \
-warnaserror
fi
'''
}
}
2025-10-17 17:19:55 +07:00
2025-10-21 00:08:03 +07:00
stage('Test + Coverage') {
steps {
sh '''
set -euo pipefail
# รันเทส + สร้างผลลัพธ์ JUnit XML + Coverage (Cobertura)
2025-10-21 08:16:01 +07:00
dotnet build -c Debug
2025-10-20 12:38:29 +07:00
dotnet test \
2025-10-21 00:08:03 +07:00
--logger "junit;LogFileName=test-results.xml" \
2025-10-21 08:09:21 +07:00
--results-directory "TestResults" \
2025-10-20 12:38:29 +07:00
/p:CollectCoverage=true \
/p:CoverletOutput=coverage/ \
/p:CoverletOutputFormat=cobertura
2025-10-21 00:08:03 +07:00
# รวม coverage ไว้ที่เดียวเพื่อ publish/เก็บ artifacts
2025-10-20 12:38:29 +07:00
mkdir -p coverage-report
2025-10-21 00:08:03 +07:00
COB=$(find . -type f -name "coverage.cobertura.xml" | head -n1 || true)
if [ -n "$COB" ]; then
cp "$COB" coverage-report/Cobertura.xml
2025-10-20 12:38:29 +07:00
fi
'''
2025-10-17 17:19:55 +07:00
}
post {
always {
2025-10-21 00:08:03 +07:00
// รายงานผลเทส (JUnit)
junit allowEmptyResults: false, testResults: '**/TestResults/**/*.xml'
// เก็บไฟล์ coverage
2025-10-17 17:19:55 +07:00
archiveArtifacts artifacts: 'coverage-report/**', allowEmptyArchive: true
2025-10-21 00:08:03 +07:00
// (ทางเลือก) ถ้าติดตั้ง Coverage plugin
// publishCoverage adapters: [coberturaAdapter('coverage-report/Cobertura.xml')], sourceFileResolver: sourceFiles('STORE_LAST_BUILD')
2025-10-17 17:19:55 +07:00
}
}
}
2025-10-21 00:08:03 +07:00
stage('Build') {
2025-10-17 17:19:55 +07:00
steps {
sh '''
2025-10-21 00:08:03 +07:00
set -euo pipefail
2025-10-17 17:19:55 +07:00
dotnet publish -c Release -o out
'''
}
2025-10-21 00:08:03 +07:00
post {
success {
archiveArtifacts artifacts: 'out/**', allowEmptyArchive: false
}
}
}
}
post {
always {
echo "Pipeline finished (status: ${currentBuild.currentResult})"
2025-10-17 17:19:55 +07:00
}
}
}