Jenkins 2 Pipleline的简单教程(二)

2018-07-13 阅读: Jenkins

Jenkins Pipeline支持两种语法,一种Declarative Pipeline(声明式),一种Scripted Pipeline(脚本式)。 声明式的Pipeline限制用户使用严格的预选定义的结构,是一种声明式的编程模型,对比脚本式的Pipeline学习起来更加简单;脚本式的Pipeline限制比较少,结构和语法的限制由Groovy本身决定,是一种命令式的编程模型。

关于Pipeline的语法在编写Pipeline的过程中,查看这里Pipeline Syntax就够了。

编写第一个Declarative Pipeline:

pipeline {
    agent { label 'jenkins-slave' }
    stages {
        stage('build') {
            steps {
                sh 'echo hello'
            }
        }
    }
}

运行多个Step的Pipeline:

pipeline {
    agent { label 'jenkins-slave' }
    stages {
        stage('build') {
            steps {
                sh 'echo step1'
                sh '''
                    echo step2-1
                    echo step2-2
                '''
            }
        }
    }
}

retry和timeout wrap step:

pipeline {
    agent { label 'jenkins-slave' }
    stages {
        stage('build') {
            steps {
                retry(3) {
                    sh 'echo deploy' 
                }
                timeout(time: 5, unit: 'SECONDS') {
                    sh 'echo healthcheck'
                    sh 'sleep 4'
                }
            }
        }
    }
}

post完成:

pipeline {
    agent { label 'jenkins-slave' }
    stages {
        stage('Test') {
            steps {
                sh 'echo "Fail!"; exit 1'
            }
        }
    }
    post {
        always {
            echo 'This will always run'
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            echo 'This will run only if failed'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
}

设置环境变量:

pipeline {
    agent { label 'jenkins-slave' }

    environment {
        DISABLE_AUTH = 'true'
        DB_ENGINE    = 'sqlite'
    }

    stages {
        stage('Build') {
            steps {
                sh 'printenv'
            }
        }
    }
}

将credentials设置为环境变量值:

pipeline {
    agent { label 'jenkins-slave' }

    environment {
        SONAR_LOGIN = credentials('sonarLogin')
    }

    stages {
        stage('Build') {
            steps {
                sh 'printenv'
            }
        }
    }
}

清理和通知:

pipeline {
    agent { label 'jenkins-slave' }
    stages {
        stage('build') {
            steps {
                timeout(time: 1, unit: 'SECONDS') {
                    sh 'sleep 2'
                }
            }
        }
    }
    post {
        failure {
            mail to: 'team@example.com',
             subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
             body: "Something is wrong with ${env.BUILD_URL}"
        }
    }
}

参考

标题:Jenkins 2 Pipleline的简单教程(二)
本文链接:https://blog.frognew.com/2018/07/jenkins-declarative-pipeline.html
转载请注明出处。

目录