jenkins agent
參考位址https://www.jenkins.io/doc/book/pipeline/syntax/
該agent部分指定整個Pipeline或特定階段将在Jenkins環境中執行的位置,具體取決于該agent 部分的放置位置。該部分必須在pipeline塊内的頂層定義 ,但階段級使用是可選的。
簡單來說,agent部分主要作用就是告訴Jenkins,選擇那台節點機器去執行Pipeline代碼。這個指令是必須要有的,也就在你頂層pipeline {…}的下一層,必須要有一個agent{…},agent這個指令對應的多個可選參數,本篇文章會一一介紹。這裡注意一點,在具體某一個stage {…}裡面也可以使用agent指令。這種用法不多,一般我們在頂層使用agent,這樣,接下來的全部stage都在一個agent機器下執行代碼。
#參數1:any 作用:在任何可用的代理上執行Pipeline或stage
pipeline {
agent any
parameters {gitParameter branchFilter: 'origin/(.*)', defaultValue: 'dev', name: 'BRANCH', type: 'PT_BRANCH'}
stages{
stage("git clone"){
steps {
script {
sh """
"""
}
}
}
stage("git clone"){
steps {
}
}
stage("git clone"){
steps {
}
}
}
}
參數2:none 當在pipeline塊的頂層應用時,将不會為整個Pipeline運作配置設定全局代理,并且每個stage部分将需要包含其自己的agent部分。
pipeline {
agent any
parameters {gitParameter branchFilter: 'origin/(.*)', defaultValue: 'dev', name: 'BRANCH', type: 'PT_BRANCH'}
stages{
stage("git clone"){
agent {
label 'jenkins-git'
}
steps {
script {
sh """
"""
}
}
}
stage("git build"){
agent {
label 'jenkins-jdk'
}
steps {
}
}
stage("deploy"){
agent {
label 'jenkins-sync'
}
steps {
}
}
}
}
//參數3:label 作用:使用提供的标簽在Jenkins環境中可用的代理機器上執行Pipeline或stage内執行。
pipeline {
agent {
label 'jenkins-dev'
}
}
//參數4:node 作用:和上面label功能類似,但是node運作其他選項,例如customWorkspace
pipeline {
agent {
node {
label 'jenkins-agent-node1'
customWorkspace "${env.JOB_NAME}/${env.BUILD_NUMBER}"
}
}
}
//其實agent相關的還有兩個可選參數,分别是docker和dockerfile
//第一種全部在一個節點和 容器内運作 在節點jenkins-dev 上運作 golang:1.17-alpine容器挂載目錄
pipeline {
agent {
docker {
label 'jenkins-dev'
image 'golang:1.17-alpine'
args "-v ${SSH_KEY_PATH}:${SSH_CREDENTIAL_PATH}"
}
}
parameters {gitParameter branchFilter: 'origin/(.*)', defaultValue: 'dev', name: 'BRANCH', type: 'PT_BRANCH'}
stages{
stage("git clone"){
steps {
script {
sh """
"""
}
}
}
stage("git build"){
agent {
label 'jenkins-jdk'
}
steps {
}
}
stage("deploy"){
agent {
label 'jenkins-sync'
}
steps {
}
}
}
}
//使用多個容器
pipeline {
agent none
stages {
stage('git pull') {
agent {
docker{ image 'git:latest' }
}
steps {
sh 'git version'
}
}
stage('build'){
agent {
docker{ image 'node:7-alpine' }
}
steps {
sh 'node --version'
}
}
}
}