5.Jenkins部署多个服务


目录
  • 目的
  • 插件安装
  • 插件的使用
    • 配置
    • 效果
  • PipeLine脚本
  • 其他
    • Docker多台部署
    • pipeline与node循环语法差异

目的

通过Jenkins拉取项目,完成编译打包后,动态部署多个服务

插件安装

通过Extended Choice Parameter这个插件可实现Build参数可传多个

插件的使用

配置

image-20220420234650034

效果

image-20220420234748136

PipeLine脚本

//接收上面脚本定义的DeployServices,并且按,切分
def deployServices = "${DeployServices}".split(',')
pipeline {
    agent any
    stages {
        stage('拉取代码' ) {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master2']], extensions: [], userRemoteConfigs: [[credentialsId: '538da12e-f04b-41f2-adc3-7e1728a5bd1e', url: 'https://gitee.com/RollBack2010/jekins-study.git']]])
            }
        }
        stage('编译代码 ') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('发布运行 ') {
			steps {
			//注意此处,如果是pipeline,必须加script{},否则for语法报错
				script {
				//循环最上面切分好的数组
                    for(int i=0;i

其他

Docker多台部署

原理一样,只不过循环时把替换成了发送docker拉取镜像运行的命令

pipeline与node循环语法差异

pipeline {
    agent any
    stages {
        stage('pipeline循环' ) {
            steps {
				script {
					for(int i=0;i<5;i++) {
						echo "a${i}"
					}
				}
            }
        }
    }
}


node {
    stage('node循环') {
        for(int i=0;i<5;i++) {
			echo "a${i}"
		}
    }
}