304 lines
11 KiB
Plaintext
304 lines
11 KiB
Plaintext
pipeline {
|
|
agent any
|
|
options { disableConcurrentBuilds(abortPrevious: true) }
|
|
parameters {
|
|
choice(name: 'PORTAL_NAME', choices: ['ktdt', 'hnt'], description: 'Choose PORTAL NAME')
|
|
}
|
|
environment {
|
|
GIT_CREDENTIALSID = 'b03f36c4-bba3-4764-94ca-b620651b2a68'
|
|
GIT_URL = 'http://work.gct.com.vn/anhln/PORTAL_2025.git'
|
|
|
|
GIT_BRANCH = ''
|
|
|
|
KTDT_BRANCH = 'uat_kinhtedothi'
|
|
HNT_BRANCH = 'uat_hanoitimes'
|
|
|
|
ENV = 'uat'
|
|
PROJECT_NAME = 'portal'
|
|
|
|
TRIGGER_JOB_NAME = 'CD-FE-PORTAL'
|
|
METADATA_FILENAME = "${env.ENV}_${env.PROJECT_NAME}_${params.PORTAL_NAME}_metadata.json"
|
|
|
|
NUXT_BUILD_FOLDER_PATH = "${env.WORKSPACE}"
|
|
OUTPUT_FOLDER_PATH = "${env.ENV}-${env.PROJECT_NAME}-${params.PORTAL_NAME}-output" //thư mục .output
|
|
|
|
COMMAND_NUXT_INSTALL = 'npm install' //command install dependencies
|
|
COMMAND_NUXT_BUILD = "NITRO_OUTPUT=${env.OUTPUT_FOLDER_PATH} yarn build" // command build MT uat
|
|
|
|
COMPRESSED_FILE_NAME = "${env.ENV}_${env.PROJECT_NAME}_${params.PORTAL_NAME}_output.zip" // tên file nén
|
|
COMPRESSED_FILE_PATH = "${env.WORKSPACE }/${env.COMPRESSED_FILE_NAME}"
|
|
|
|
NEXUS_CREDENTIALS = credentials('Nexus_credential')
|
|
NEXUS_REPO_URL = "https://nexus.gct.com.vn/repository/${env.ENV}-${env.PROJECT_NAME}-frontend"
|
|
GROUP_ID = 'vn.kinhtedothi'
|
|
ARTIFACT_ID = "${env.ENV}-${env.PROJECT_NAME}-${params.PORTAL_NAME}-frontend"
|
|
PACKAGING = 'zip'
|
|
VERSION = '1.0.0' // Phiên bản cơ bản
|
|
|
|
TIMESTAMP = new Date().format('yyyyMMdd.HH', TimeZone.getTimeZone('UTC'))
|
|
FULL_VERSION = "${env.VERSION}-${env.TIMESTAMP}-${env.BUILD_NUMBER}" // Tạo phiên bản hoàn chỉnh
|
|
|
|
TELEGRAM_CHAT_ID = -4678013464
|
|
TELEGRAM_BOT_TOKEN = credentials('TELEGRAM_BOT_TOKEN')
|
|
}
|
|
stages {
|
|
stage('Configure GIT_BRANCH') {
|
|
steps {
|
|
script {
|
|
if (params.PORTAL_NAME == 'ktdt') {
|
|
env.GIT_BRANCH = env.KTDT_BRANCH
|
|
} else if (params.PORTAL_NAME == 'hnt') {
|
|
env.GIT_BRANCH = env.HNT_BRANCH
|
|
}
|
|
}
|
|
echo "Branch được thiết lập là: ${env.GIT_BRANCH}"
|
|
}
|
|
}
|
|
|
|
stage('Checkout') {
|
|
steps {
|
|
// Checkout mã nguồn từ Gitea
|
|
checkoutFromGit(env.GIT_URL as String, env.GIT_CREDENTIALSID as String, env.GIT_BRANCH as String)
|
|
}
|
|
}
|
|
|
|
stage('Install Dependencies') {
|
|
steps {
|
|
script {
|
|
installDependencies(env.NUXT_BUILD_FOLDER_PATH as String, env.COMMAND_NUXT_INSTALL as String)
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Build') {
|
|
steps {
|
|
script {
|
|
buildProject(env.NUXT_BUILD_FOLDER_PATH as String, env.COMMAND_NUXT_BUILD as String)
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Package (Compress Output Files)') {
|
|
steps {
|
|
compressItems(env.COMPRESSED_FILE_PATH, env.OUTPUT_FOLDER_PATH)
|
|
}
|
|
}
|
|
|
|
stage('Upload to Nexus') {
|
|
steps {
|
|
script {
|
|
String groupPath = env.GROUP_ID.replace('.', '/')
|
|
env.NEXUS_URL = "${env.NEXUS_REPO_URL}/${groupPath}/${env.ARTIFACT_ID}/${env.VERSION}"
|
|
env.NEXUS_ARTIFACT_NAME = "${env.ARTIFACT_ID}-${env.FULL_VERSION}.${env.PACKAGING}"
|
|
|
|
echo "NEXUS_URL : ${env.NEXUS_URL}"
|
|
echo "NEXUS_ARTIFACT_NAME : ${env.NEXUS_ARTIFACT_NAME}"
|
|
|
|
String nexusUploadUrl = "${env.NEXUS_URL}/${env.NEXUS_ARTIFACT_NAME}"
|
|
|
|
uploadToNexus(
|
|
env.NEXUS_CREDENTIALS_USR,
|
|
env.NEXUS_CREDENTIALS_PSW,
|
|
env.COMPRESSED_FILE_PATH,
|
|
nexusUploadUrl)
|
|
}
|
|
}
|
|
}
|
|
|
|
stage('Create Metadata File') {
|
|
steps {
|
|
createMetadataFile(
|
|
env.METADATA_FILENAME,
|
|
env.GROUP_ID,
|
|
env.ARTIFACT_ID,
|
|
env.VERSION,
|
|
env.NEXUS_URL,
|
|
env.NEXUS_ARTIFACT_NAME)
|
|
echo "metadataFileName: ${env.METADATA_FILENAME}"
|
|
archiveArtifacts artifacts: "${env.METADATA_FILENAME}", allowEmptyArchive: false
|
|
}
|
|
}
|
|
}
|
|
|
|
post {
|
|
always {
|
|
echo 'Pipeline execution finished.'
|
|
}
|
|
success {
|
|
echo "Job '${env.JOB_NAME}' completed successfully. Attempting to trigger Job '${TRIGGER_JOB_NAME}'..."
|
|
script {
|
|
def message = "✅Build thành công : FRONTEND - Môi trường ${env.ENV} - Dự án ${env.PROJECT_NAME} - Portal_Name ${params.PORTAL_NAME} \n ${currentBuild.fullDisplayName}\n${env.BUILD_URL} \n Đang tiến hành Deploy...!"
|
|
sh "curl -s -X POST https://api.telegram.org/bot${env.TELEGRAM_BOT_TOKEN}/sendMessage -d chat_id=${env.TELEGRAM_CHAT_ID} -d text=\"${message}\""
|
|
}
|
|
script {
|
|
try {
|
|
def buildResult = build job: "${TRIGGER_JOB_NAME}", parameters:[
|
|
string(name: 'ENV', value: env.ENV),
|
|
string(name: 'PORTAL_NAME', value: params.PORTAL_NAME),
|
|
string(name: 'CI_JOB_BUILD_NUMBER', value: env.BUILD_NUMBER),
|
|
|
|
],
|
|
propagate: false
|
|
if (buildResult.result != 'SUCCESS') {
|
|
echo "[WARNING] Job 2 failed with result: ${buildResult.result}"
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
echo "[ERROR] Failed to trigger job: ${TRIGGER_JOB_NAME}. Error: ${e.message}"
|
|
throw e
|
|
}
|
|
}
|
|
}
|
|
failure {
|
|
script {
|
|
def message = "❌Build thất bại: FRONTEND - Môi trường ${env.ENV} - Dự án ${env.PROJECT_NAME} - Site ${params.PORTAL_NAME} \n ${currentBuild.fullDisplayName}\n Kiểm tra tại đây ${env.BUILD_URL}."
|
|
sh "curl -s -X POST https://api.telegram.org/bot${env.TELEGRAM_BOT_TOKEN}/sendMessage -d chat_id=${env.TELEGRAM_CHAT_ID} -d text=\"${message}\""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//Thienvv- hàm checkout git
|
|
void checkoutFromGit(String gitUrl, String credentialsId, String branch) {
|
|
echo 'Start checkoutFromGit'
|
|
try {
|
|
checkout([$class : 'GitSCM',
|
|
userRemoteConfigs: [[url: gitUrl, credentialsId: credentialsId]],
|
|
branches : [[name: "*/${branch}"]]])
|
|
} catch (Exception e) {
|
|
echo "[ERROR] Unexpected error: ${e.message}"
|
|
throw e
|
|
}
|
|
}
|
|
|
|
// Thienvv - hàm installDependencies
|
|
void installDependencies(String buildFolderPath, String command) {
|
|
echo 'Start installDependencies'
|
|
try {
|
|
if (!fileExists(buildFolderPath)) {
|
|
error "buildFolderPath is not exist : ${buildFolderPath}"
|
|
}
|
|
|
|
dir("${buildFolderPath}") {
|
|
Integer result = sh(script: "${command}", returnStatus: true)
|
|
|
|
echo "result:${result}"
|
|
|
|
if (result == 0) {
|
|
echo 'install Dependencies successfully.'
|
|
} else {
|
|
error "install Dependencies failed with status: ${result}"
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
error "Restore failed: ${e.message}"
|
|
}
|
|
}
|
|
|
|
//Thienvv - build project
|
|
void buildProject(String buildFolderPath, String command) {
|
|
echo 'Start buildProject'
|
|
try {
|
|
if (!fileExists(buildFolderPath)) {
|
|
error "buildFolderPath is not exist : ${buildFolderPath}"
|
|
}
|
|
// Build dự án Nuxt.js
|
|
dir("${buildFolderPath}") {
|
|
Integer result = sh(script: "${command}", returnStatus: true)
|
|
|
|
echo "result:${result}"
|
|
|
|
if (result == 0) {
|
|
echo 'Build Project successfully'
|
|
} else {
|
|
error "Build Project failed, status: ${result}"
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
error "Build Project failed: ${e.message}"
|
|
}
|
|
}
|
|
|
|
//Thienvv - nén file
|
|
void compressItems(String compressedFilePath, String parentFolderPath) {
|
|
echo "Starting compression of the entire folder into a zip file : ${compressedFilePath}"
|
|
try {
|
|
if (fileExists(compressedFilePath)) {
|
|
echo "Old ZIP file exists. Deleting: ${compressedFilePath}"
|
|
sh "rm -f ${compressedFilePath}"
|
|
} else {
|
|
echo 'No old ZIP file found.'
|
|
}
|
|
|
|
if (!fileExists(parentFolderPath)) {
|
|
error "parentFolderPath is not exist : ${parentFolderPath}"
|
|
}
|
|
|
|
dir(parentFolderPath) {
|
|
// Thực hiện lệnh zip để nén tất cả các file trong thư mục
|
|
Integer result = sh(script: "zip -r ${compressedFilePath} * ", returnStatus: true)
|
|
|
|
// Kiểm tra kết quả
|
|
if (result == 0) {
|
|
echo "compressItems completed successfully: ${compressedFilePath}"
|
|
} else {
|
|
error "compressItems failed with exit code: ${result}."
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
echo "[ERROR] Unexpected error: ${e.message}"
|
|
throw e
|
|
}
|
|
}
|
|
|
|
void uploadToNexus(String nexusUsername, String nexusPassword, String compressedFilePath, String uploadUrl) {
|
|
echo "Starting upload ${compressedFilePath} To Nexus"
|
|
try {
|
|
// Truyền các biến môi trường vào lệnh sh mà không lộ ra trong log
|
|
String result = sh(script: """
|
|
curl -u ${nexusUsername}:${nexusPassword} --upload-file ${compressedFilePath} ${uploadUrl} -w '%{http_code}'
|
|
""", returnStdout: true).trim()
|
|
|
|
if (result == '200' || result == '201') {
|
|
echo 'uploadToNexus successfully'
|
|
} else {
|
|
error "uploadToNexus failed, HTTP status: ${result}"
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
echo "[ERROR] Unexpected error: ${e.message}"
|
|
throw e
|
|
}
|
|
}
|
|
|
|
void createMetadataFile(
|
|
String metadataFileName,
|
|
String groupId,
|
|
String artifactId,
|
|
String version,
|
|
String nexusUrl,
|
|
String nexusArtifactName) {
|
|
echo 'Starting create metadata file'
|
|
try {
|
|
// Tạo metadata
|
|
metadata = [
|
|
groupId: groupId,
|
|
artifactId: artifactId,
|
|
version: version,
|
|
nexusUrl: nexusUrl,
|
|
nexusArtifactName: nexusArtifactName
|
|
]
|
|
|
|
// Ghi metadata vào file JSON
|
|
writeJSON file: "${metadataFileName}", json: metadata
|
|
|
|
if (!fileExists(metadataFileName)) {
|
|
error "metadataFileName is not exist : ${metadataFileName}"
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
echo "[ERROR] Unexpected error: ${e.message}"
|
|
throw e
|
|
}
|
|
}
|