Jenkins打包使用pipeline方式
使用pipeline方式可以自定义git参数(可以用depth参数解决由于.git文件太大导致clone失败的问题:git clone --depth)。
-
新建任务
流水线 -
Pipeline script脚本配置:
其中credentialsId是证书ID,可以在“系统管理-Manage Credentials”菜单查看配置的证书ID;extensions里面可以填入git参数,比如depth。
脚本配置
# 前端脚本示例
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: 'feature/flow']],
extensions: [[$class: 'CloneOption', depth: 1, noTags: false, reference: '', shallow: true]],
userRemoteConfigs: [[credentialsId:'526634df-a2c3-4bc2-bbe0-611a01fcd148',url:'https://gitee.com/****.git',branch:"develop"]]])
}
}
stage('编译构建') {
steps {
sh 'npm install;npm run build;'
}
}
stage('项目部署') {
steps {
sh 'rm -rf /var/we-safer/package/portal/dist;'
sh 'cp -r /var/lib/jenkins/workspace/portal-cooperate-pipeline/dist /var/package/portal/dist'
}
}
}
}
# 后端脚本示例
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
checkout([$class: 'GitSCM',
branches: [[name: 'feature']],
extensions: [[$class: 'CloneOption', depth: 1, noTags: false, reference: '', shallow: true]],
userRemoteConfigs: [[credentialsId:'526634df-a2c3-4bc2-bbe0-611a01fcd148',url:'https://gitee.com/****.git',branch:"develop"]]])
}
}
stage('编译构建') {
steps {
sh '/usr/local/src/apache-maven-3.8.5/bin/mvn clean package'
}
}
stage('项目部署') {
steps {
sh 'echo \'containerId is ........................\' $(docker ps | grep uaa-web$ | awk \'{print $1}\')'
sh 'if [ $(docker ps| grep uaa-web$ | awk \'{print $1}\') ];then docker stop `docker ps| grep uaa-web$ | awk \'{print $1}\' `; echo \'stop uaa-web\'; fi'
sh 'if [ $(docker ps -a | grep uaa-web$ | awk \'{print $1}\') ];then docker rm uaa-web;echo \'rm uaa-web\'; fi'
sh 'cd /var/lib/jenkins/workspace/wwyt-service-cooperate-pipeline/uaa-web; docker build -t uaa-web -f Dockerfile .;'
sh 'docker run -d -p 8902:8902 --name=\'uaa-web\' uaa-web --spring.profiles.active=prod-;'
}
}
}
}
网友评论