web前端开发

三、Jenkinsfile语法

2021-07-04  本文已影响0人  InsaneLoafer

一、Jenkinsfile语法简介

二、Jenkinsfile语法-Declarative pipeline

Declarative 语句树

image.png
pipeline{
    agent{
        node{
            label "myslave"
            customWorkspace "myWorkspace"
        }
    }
}

必要参数

stages{
    stage('git pull source code'){
        steps{
            echo 'sync update code'
            git 'https://github.com/priceqjzh/iPipeline.git'
        }
    }
}

非必要参数post


post{
    success{
        echo 'goodbye pipeline success'
        sleep 2
    }
    always{
        echo 'always say goodbye'
    }
}

非必要参数environment

environment{
    hlw='hello world'
}
stages{
    stage('Print environment_1'){
        steps{
            echo hlw
        }
    }
}

非必要参数options

options{
    timeout(time:30, unit:'SECONDS')
    buildDiscarder(logRotator(numToKeepStr:'2'))
    retry(5)
}

非必要参数

parameters{
    string(name:'PERSON', defaultValue:'Jenkins', description:'输入的文本参数')
    }
    stages{
        stage('Test Parameters'){
            steps{
                echo "Hello ${params.PERSON}"
            }
        }
    }

非必要参数triggers

triggers{
    pollSCM('H */4 * * 1-5')
}

三、Jenkinsfile语法Scripts pipeline

Script 语句树

image.png

流程控制之 - if/else

node{
    stage('Example'){
        if (env.BRANCH_NAME == 'master'){
            echo 'I only execute on the master brach'
        }else{
            echo 'I execute elsewhere'
        }
    }
}

流程控制之 - try/catch

node{
    echo 'This is test stage which run on the slave agent.'
    try{
        echo 'This is in the try block.'
    }catch(exc){
        echo "Something failed, I'm in the catch block"
    }finally{
        echo "Finally, I'm in the finally block."
    }
}

groovy 语句设置

image.png
image.png
image.png
上一篇下一篇

猜你喜欢

热点阅读