26、Jenkins流水线
1、Hello World
https://www.jenkins.io/zh/doc/book/pipeline/jenkinsfile/
agent
指令是必需的,它指示 Jenkins 为流水线分配一个执行器和工作区。没有 agent
指令的话,声明式流水线不仅无效,它也不可能完成任何工作!默认情况下,agent
指令确保源代码仓库被检出并在后续阶段的步骤中可被使用。
一个合法的声明式流水线还需要 stages 指令和 steps 指令,因为它们指示 Jenkins 要执行什么,在哪个阶段执行。
2、新建流水线构建任务
pipeline-test
groovy
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
echo '拉取代码..'
}
}
stage('Maven打包') {
steps {
echo 'Maven打包..'
}
}
stage('制作镜像') {
steps {
echo '制作镜像..'
}
}
stage('部署') {
steps {
echo '部署....'
}
}
}
}
3、构建一次
4、拉取代码
1、生成脚本
2、流水线
groovy
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
echo '拉取代码..'
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitee', url: 'https://gitee.com/xiaoxueblog/xx-springboot.git']])
sh 'ls'
}
}
stage('Maven打包') {
steps {
echo 'Maven打包..'
}
}
stage('制作镜像') {
steps {
echo '制作镜像..'
}
}
stage('部署') {
steps {
echo '部署....'
}
}
}
}
5、Maven构建
可以直接定义工具
groovy
pipeline {
agent any
tools {
maven "maven3.9.11"
}
stages {
stage('拉取代码') {
steps {
echo '拉取代码..'
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitee', url: 'https://gitee.com/xiaoxueblog/xx-springboot.git']])
sh 'ls'
}
}
stage('Maven打包') {
steps {
echo 'Maven打包..'
sh 'mvn clean package'
}
}
stage('制作镜像') {
steps {
echo '制作镜像..'
}
}
stage('部署') {
steps {
echo '部署....'
}
}
}
}
6、制作镜像
groovy
pipeline {
agent any
tools {
maven "maven3.9.11"
}
stages {
stage('拉取代码') {
steps {
echo '拉取代码..'
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitee', url: 'https://gitee.com/xiaoxueblog/xx-springboot.git']])
sh 'ls'
}
}
stage('Maven打包') {
steps {
echo 'Maven打包..'
sh 'mvn clean package'
}
}
stage('制作镜像') {
steps {
echo '制作镜像..'
sh "docker build -t xx-springboot:v$BUILD_NUMBER ."
}
}
stage('部署') {
steps {
echo '部署....'
}
}
}
}
7、部署
sh
pipeline {
agent any
tools {
maven "maven3.9.11"
}
stages {
stage('拉取代码') {
steps {
echo '拉取代码..'
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitee', url: 'https://gitee.com/xiaoxueblog/xx-springboot.git']])
sh 'ls'
}
}
stage('Maven打包') {
steps {
echo 'Maven打包..'
sh 'mvn clean package'
}
}
stage('制作镜像') {
steps {
echo '制作镜像..'
sh "docker build -t xx-springboot:v$BUILD_NUMBER ."
}
}
stage('部署') {
steps {
echo '部署....'
sh "docker run -d -p 8001:8001 --name=xx-springboot xx-springboot:v$BUILD_NUMBER"
}
}
}
}
8、部署优化 - 停止服务
groovy
pipeline {
agent any
tools {
maven "maven3.9.11"
}
stages {
stage('拉取代码') {
steps {
echo '拉取代码..'
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitee', url: 'https://gitee.com/xiaoxueblog/xx-springboot.git']])
sh 'ls'
}
}
stage('Maven打包') {
steps {
echo 'Maven打包..'
sh 'mvn clean package'
}
}
stage('制作镜像') {
steps {
echo '制作镜像..'
sh "docker build -t xx-springboot:v$BUILD_NUMBER ."
}
}
stage('停止服务') {
steps {
sh "chmod 777 stop_container.sh && bash stop_container.sh xx-springboot $BUILD_NUMBER"
}
}
stage('部署') {
steps {
echo '部署....'
sh "docker run -d -p 8001:8001 --name=xx-springboot xx-springboot:v$BUILD_NUMBER"
}
}
}
}
中间可以加上传镜像仓库