Skip to content

26、Jenkins流水线

1、Hello World

https://www.jenkins.io/zh/doc/book/pipeline/jenkinsfile/

agent指令是必需的,它指示 Jenkins 为流水线分配一个执行器和工作区。没有 agent 指令的话,声明式流水线不仅无效,它也不可能完成任何工作!默认情况下,agent 指令确保源代码仓库被检出并在后续阶段的步骤中可被使用。

一个合法的声明式流水线还需要 stages 指令和 steps 指令,因为它们指示 Jenkins 要执行什么,在哪个阶段执行。

2、新建流水线构建任务

pipeline-test

image-20250730142730411

image-20250730142826707

groovy
pipeline {
    agent any

    stages {
      stage('拉取代码') {
            steps {
                echo '拉取代码..'
            }
        }
      
        stage('Maven打包') {
            steps {
                echo 'Maven打包..'
            }
        }
        stage('制作镜像') {
            steps {
                echo '制作镜像..'
            }
        }
        stage('部署') {
            steps {
                echo '部署....'
            }
        }
    }
}

3、构建一次

image-20250730142925380

image-20250730143011765

4、拉取代码

1、生成脚本

image-20250730143058733

image-20250730143243312

image-20250730143555089

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 '部署....'
            }
        }
    }
}

image-20250730143748965

5、Maven构建

可以直接定义工具

image-20250730144135345

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 '部署....'
            }
        }
    }
}

image-20250730144544894

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 '部署....'
            }
        }
    }
}

image-20250730144808999

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"
            }
        }
    }
}

中间可以加上传镜像仓库

9、最终部署示例图

image-20250730145326141