diff --git a/fe/fe-portal/ci-portal-frontend.Jenkinsfile b/fe/fe-portal/ci-portal-frontend.Jenkinsfile deleted file mode 100644 index e69de29..0000000 diff --git a/fe/fe-portal/ci-uat-portal-frontend.Jenkinsfile b/fe/fe-portal/ci-uat-portal-frontend.Jenkinsfile new file mode 100644 index 0000000..2250d37 --- /dev/null +++ b/fe/fe-portal/ci-uat-portal-frontend.Jenkinsfile @@ -0,0 +1,279 @@ +pipeline { + agent any + environment { + GIT_CREDENTIALSID = 'b03f36c4-bba3-4764-94ca-b620651b2a68' + GIT_URL = 'http://work.gct.com.vn/anhln/PORTAL_2025.git' + GIT_BRANCH = 'development' + + ENV = 'uat' + PROJECT_NAME = 'portal' + TRIGGER_JOB_NAME = 'CD-FE-PORTAL' + METADATA_FILENAME = "${env.ENV}_${env.PROJECT_NAME}_metadata.json" + + NUXT_BUILD_FOLDER_PATH = "${env.WORKSPACE}" + OUTPUT_FOLDER_PATH = "${env.ENV}-${env.PROJECT_NAME}-output" //thư mục .output (sau khi build xong) + COMMAND_NUXT_INSTALL = 'npm install' //command install dependencies + COMMAND_NUXT_BUILD = "NITRO_OUTPUT=${env.OUTPUT_FOLDER_PATH} yarn linux-builder" // command build MT uat + + COMPRESSED_FILE_NAME = "${env.ENV}_${env.PROJECT_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}-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('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} \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: '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} \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 + } + }