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

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM