// 根据选择分支进行构建
pipeline {
agent any
environment {
WS = "${WORKSPACE}"
tagStr = ""
}
//定义流水线的加工流程
stages {
//流水线的所有阶段
stage('Environmental inspection') {
steps {
println("${BRANCH_NAME}")
println("${WORKSPACE}")
sh 'printenv'
echo "Checking basic information"
sh 'java -version'
sh 'git --version'
sh 'docker version'
sh 'pwd && ls -alh'
}
}
stage('get code') {
steps {
script{
sh 'git config --global http.<https://github.com.proxy> socks5://127.0.0.1:1081'
sh 'git config -l'
if (DEPLOY_TYPE == "rollback") {
// 获取tag,切换tag分支
withCredentials([gitUsernamePassword(credentialsId: 'aiyongJenkins', gitToolName: 'Default')]) {
script {
sh 'git tag -l | xargs git tag -d'
// 拉取最新tag
sh "git fetch --tags"
// 切换tag分支
sh "git checkout ${TAG_NAME}"
}
}
} else {
echo "不是回滚,不需要获取tag"
checkout([$class : 'GitSCM', branches: [[name: "${BRANCH_NAME}"]],
doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CheckoutOption', timeout: 30], [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false, timeout: 30]], submoduleCfg: [],
userRemoteConfigs : [[credentialsId: 'aiyongJenkins', url: '<https://github.com/aiyongbao/itr-btit-fe-adminpc.git>']]])
}
}
}
}
stage('node compilation') {
agent {
docker {
image 'node:16.18.0'
}
}
steps {
//git下载来的代码目录下
sh 'yarn -version'
sh 'npm -version'
sh 'node --version'
sh 'pwd && ls -alh'
sh "echo 默认的工作目录:${WS}"
sh 'yarn config set sass_binary_site <http://cdn.npm.taobao.org/dist/node-sass> -g'
sh 'yarn config set registry <https://registry.npm.taobao.org> -g '
sh 'cd ${WS} && ' +
'yarn config list && ' +
'rm -rf yarn.lock && ' +
'yarn install && yarn postinstall && yarn run build'
}
}
stage('tar') {
steps {
sh 'pwd && cd dist && tar -cf ../dist.tar *'
sh 'pwd && ls -alh'
}
}
stage('deploy') {
steps {
echo "部署..."
sh 'pwd && ls -alh'
sshPublisher(publishers: [sshPublisherDesc(configName: '172.21.0.22', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''pwd
cd /home/aiyong/digital_admin/itr-btit-fe-adminpc/dist
find * | grep -v dist.tar | xargs rm
tar -xf dist.tar
mv dist.tar ..
''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/home/aiyong/digital_admin/itr-btit-fe-adminpc/dist', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'dist.tar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
}
}
stage('git build tag') {
steps {
// 判断
script {
if (env.DEPLOY_TYPE == 'rollback') {
echo "回滚,不需要打tag"
} else {
echo "打tag"
withCredentials([gitUsernamePassword(credentialsId: 'aiyongJenkins', gitToolName: 'Default')]) {
// 删除本地所有tag
sh 'git tag -l | xargs git tag -d'
// 拉取最新tag
sh 'git fetch --tags'
def GIT_AUTHOR_NAME = 'aiyongJenkins'
def GIT_AUTHOR_EMAIL = 'aiyongJenkins@mail.com'
// 设置git邮箱
sh "git config --global user.email ${GIT_AUTHOR_NAME}"
// 设置git用户名
sh "git config --global user.name ${GIT_AUTHOR_EMAIL}"
try {
def recentlyTag = ""
// 获取远程所有tag,按照时间排序
def tagSortListByCreateTime = sh(script: 'git tag -l --sort=-creatordate', returnStdout: true).trim().tokenize('\\n')
// 循环tags,获取以v前缀开头的tag
for (int i = 0; i < tagSortListByCreateTime.size(); i++) {
echo "tag: ${tagSortListByCreateTime[i]}"
if (tagSortListByCreateTime[i].startsWith('digital.v')) {
recentlyTag = tagSortListByCreateTime[i].replace('digital.v', '')
break
}
}
echo "recentlyTag: ${recentlyTag}"
// 如果tagStr为空,说明没有以v开头的tag,那么就从1开始
if (recentlyTag == "") {
recentlyTag = "1.0.0";
}
// 获取最新tag
def recentlyTagArr = recentlyTag.split("\\\\.")
// 获取最新tag的最后一位
def recentlyTagLast = recentlyTagArr[recentlyTagArr.size() - 1]
// 最新tag的最后一位+1
def recentlyTagLastAdd = recentlyTagLast.toInteger() + 1
// 替换最新tag的最后一位
recentlyTagArr[recentlyTagArr.size() - 1] = recentlyTagLastAdd
// 拼接tag
tagStr = "digital.v" + recentlyTagArr.join(".")
}catch (Exception e){
echo "获取最新tag失败,使用默认tag${e.getMessage()}"
// 如果tags为空,初始化版本
tagStr = "digital.v1.0.0";
}
println "tagStr: ${tagStr}"
}
}
}
}
}
}
// post必走
post {
always {
script {
echo "构建结果:always"
// 如果是tag拉出来的分支,回倒是scm获取不到分支和tag列表
if (DEPLOY_TYPE == 'rollback') {
// 切换主分支
checkout([$class : 'GitSCM', branches: [[name: "main"]],
doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CheckoutOption', timeout: 30], [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false, timeout: 30]], submoduleCfg: [],
userRemoteConfigs : [[credentialsId: 'aiyongJenkins', url: '<https://github.com/aiyongbao/itr-btit-services-group.git>']]])
}
}
}
success {
script {
echo "构建结果:success"
if(DEPLOY_TYPE == 'deploy'){
withCredentials([gitUsernamePassword(credentialsId: 'aiyongJenkins', gitToolName: 'Default')]) {
def currentDate = new Date().format("yyyyMMddHHmmss");
// 打tag
sh "git tag -a ${tagStr} -m 'tag ${tagStr}-${currentDate}'"
// 推送tag
sh "git push origin ${tagStr}"
}
}
}
}
failure {
script {
echo "构建结果:failure"
if(DEPLOY_TYPE == "deploy"){
echo "部署失败"
}
}
}
}
}