jenkins pipeline filenode
currentBuild.description="${BRANCH}_${ENV}" pipeline{ agent { node { label 'master' customWorkspace "workspace/${JOB_NAME}_${ENV}" } } post { success { sh ''' curl -H "Content-Type: application/json" -X POST -d '{"branch":"'${BRANCH}'","project_name":"'${PROJECT_NAME}'","test_env":"'${ENV}'","artifact":"'`${ID}`'"}' http://hbtc.houbank.net:8000/api/release/artifactInfo/ ''' } } options { //保持構建的最大個數 buildDiscarder(logRotator(numToKeepStr: '10')) } parameters { choice(name: 'ENV', choices: 'mit\nsit\nuat\nprod', description: 'env') string(name: 'STATUS', defaultValue: '改造後', description: '環境變量遷入配置中心') string(name: 'BRANCH', defaultValue: '${gitlabSourceBranch}', description: '選擇發佈的分支,默認爲git觸發分支') string(name: 'GIT', defaultValue: 'https://code.houbank.net/project/kuaiyipai_server.git', description: 'git地址') } environment { LOB = "telesale" //業務線 postfix="jar" TARGET_FILE="kuaiyipai-app-api/target/kuaiyipai-app-api.${postfix}" PACKAGE_PATH_old="/var/jenkins_home/package/$LOB/${PROJECT_NAME}/${ENV}" //項目改造前包目錄 PACKAGE_PATH_new="/var/jenkins_home/package/$LOB/${PROJECT_NAME}/prod" //項目改造後包目錄 ID="git rev-parse ${params.BRANCH}" PROJECT_NAME = "${JOB_BASE_NAME}" // 工程名字 GIT_ADDRESS="${params.GIT}" //GIT地址 SAVE_FILE_NUM=3 //保留歷史質品數量,實際保留2天 } stages { stage('檢出代碼'){ steps { git( url: "${GIT_ADDRESS}", credentialsId: '23477e7c-edd5-48a1-a0e4-acfb57c55687', branch: "${params.BRANCH}" ) } } stage('帶參編譯打包'){ when { not { environment name: 'STATUS',value: '改造後' } } agent { docker { image 'maven:3.5.3' args '-v /root/.m2:/root/.m2' } } steps { sh ''' cd ${WORKSPACE} if [ ! -d ${PACKAGE_PATH_old} ];then mkdir -p ${PACKAGE_PATH_old} fi if [ ! -f ${PACKAGE_PATH_old}/${PROJECT_NAME}_`${ID}`.${postfix} ];then mvn clean package -U -Dmaven.test.skip=true -Dmaven.compile.fork=true -P${ENV} else echo "file exist" fi ''' } } stage('不帶參編譯打包') { when { environment name: 'STATUS',value: '改造後' } agent { docker { image 'maven:3.5.3' args '-v /root/.m2:/root/.m2' } } steps { sh ''' cd ${WORKSPACE} if [ ! -d ${PACKAGE_PATH_new} ];then mkdir -p ${PACKAGE_PATH_new} fi if [ ! -f ${PACKAGE_PATH_new}/${PROJECT_NAME}_`${ID}`.${postfix} ];then mvn clean package -U -Dmaven.test.skip=true -Dmaven.compile.fork=true else echo "file exist" fi ''' } } stage ('改造前 分發倉庫'){ when { not { environment name: 'STATUS',value: '改造後' } } steps { sh ''' if [ ! -f ${PACKAGE_PATH_old}/${PROJECT_NAME}_`${ID}`.${postfix} ];then ls -t ${PACKAGE_PATH_old} | tail -n +${SAVE_FILE_NUM} | xargs rm -f cp -rf ${WORKSPACE}/${TARGET_FILE} ${PACKAGE_PATH_old}/${PROJECT_NAME}_`${ID}`.${postfix} else echo "file exits" fi ''' } } stage ('改造後 分發倉庫'){ when { environment name: 'STATUS',value: '改造後' } steps { sh ''' if [ ! -f ${PACKAGE_PATH_new}/${PROJECT_NAME}_`${ID}`.${postfix} ];then ls -t ${PACKAGE_PATH_new} | tail -n +${SAVE_FILE_NUM} | xargs rm -f cp -rf ${WORKSPACE}/${TARGET_FILE} ${PACKAGE_PATH_new}/${PROJECT_NAME}_`${ID}`.${postfix} else echo "file exits" fi ''' } } stage('改造前環境發佈'){ when { allOf { not {environment name: 'ENV',value: 'prod'} not {environment name: 'STATUS',value: '改造後'} } } steps { sh ''' /usr/bin/ansible-playbook -i /app/ansible/hosts /app/ansible/deploy.yml -e "env=${ENV} lob=${LOB} groupname=${PROJECT_NAME} id=`${ID}`" -e"@/app/ansible/group_vars/${PROJECT_NAME}" ''' } } stage('改造後環境發佈'){ when { allOf { environment name: 'STATUS',value: '改造後' not {environment name: 'ENV',value: 'prod'} } } steps { sh ''' ansible-playbook -i /app/ansible/hosts /app/ansible/deploy.yml -e "env=${ENV} lob=${LOB} groupname=${PROJECT_NAME} id=`${ID}` remote_path=http://192.168.13.9:81/jenkins/${LOB}/${PROJECT_NAME}/prod" -e"@/app/ansible/group_vars/${PROJECT_NAME}" ''' } } } }