jenkins pipeline使用方式

pipeline 使用

使用groovy的一种DSL语言,流程控制 pipeline脚本同其他脚本语言一样,从上到下顺序执行,它的流程控制取决于Groovy表达式,为jenkins用户提供了更巨大的灵活性和可扩展性,本章内容围绕 devops [持续交付实践] pipeline

 

1.使用声明式写法

agent : 该agent部分指定整个Pipeline或特定阶段将在jenkins环境中执行的位置,具体取决于该agent部分的放置位置。该部分必须在pipeline块内的顶层定义。但stage级使用是可选的

  1):any、none、指定slave

    any 在任何可用的agent 上执行Pipeline或stage

              none 当在pipeline块的顶层使用none时,将不会为整个Pipeline运行分配全局agent ,每个stage部分将需要包含其自己的agent

              label 使用提供的label标签,在Jenkins环境中可用的代理上执行Pipeline或stage。例如:agent { label 'my-defined-label' } 

parameters:配置部署之前参数。

  1):string、password、choice

def name="test1"
def test2(name1){
  println name1
if(name1 == "test3"){
    println "the is a test name1 is ${name}"
}else{
    println "the not is name1 the is ${name}"
    }

}
pipeline { agent any
  

    parameters {
       string(name: 'username', defaultValue: 'guozh10', description: 'username ')
        password(name: 'password', defaultValue: 'password', description: 'password')

         choice(name: 'ENV_TYPE', choices: editionName, description: 'test means test env,….')
    }

    stages {
        stage('Non-parallel Stage') {
            steps {
                echo 'This stage will be executed first.'
            }
        }
        stage('parakked Stage') {
            when {
                branch 'master'
            }
            parallel {
                stage('Branch A') {
                    agent {
                        label "for-branch-a"
                    }
                    steps {
                        echo "On Branch A"
                    }
                
                }
                stage('Branch B') {
                    agent {
                        label "for-branch-b"
                    }
                    steps {
                        echo "On Branch B"
                    }
                }
            }
        }
    }
}

 

脚本式

node ("master"){
	stage('Example') {
		try {
			sh 'exit 1'
		}
		catch (exc) {
			echo 'Something failed, I should sound the klaxons!'
			throw
		}
	}
}
node("slave01") {
	stage('Example') {
		try {
			sh 'exit 1'
		}
		catch (exc) {
			echo 'Something failed, I should sound the klaxons!'
			throw
		}
	}
}

  

 

posted @ 2019-07-15 15:29  扛把子修BUG  阅读(2847)  评论(0编辑  收藏  举报