Jenkins file Declarative Pipeline 常用写法注释

Jenkinsfile (Declarative Pipeline)

pipeline {
    // 执行环境, any任何环境一般是本机, 可以是远程节点服务器, 可以是docker服务
    agent any
    agent {
        docker { image 'node:7-alpine' }
    }

    // 全局环境变量
    environment {
        DISABLE_AUTH = 'true'
        DB_ENGINE    = 'sqlite'
    }

    // 构建步骤: Build, Test, Deploy
    stages {
        stage('Build') {
            steps {
                sh 'echo "Hello World"'
                sh '''
                    echo "Multiline shell steps works too"
                    ls -lah
                '''
            }
        }

        stage('Test') {
            steps {
                retry(3) {
                    sh './flakey-test.sh'
                }

                timeout(time: 3, unit: 'MINUTES') {
                    sh './health-check.sh'
                }

                sh 'echo "Fail!"; exit 1'
            }
        }

        stage('Deploy - Staging(适用于部署)') {
            steps {
                sh './deploy staging'
                sh './run-smoke-tests'
            }
        }
        stage('Sanity check 神志正常检查, 点击继续开始执行下面内容, 否则卡死此步骤') {
            steps {
                input "Does the staging environment look ok?"
            }
        }
        stage('Deploy - Production(产品化,正式部署)') {
            steps {
                sh './deploy production'
            }
        }
    }

    // 构建结束调用
    post {
        always {
             echo 'This will always run'
            echo 'One way or another, I have finished'
            /* do sth cleanup */
        }
        success {
            echo 'This will run only if successful'
            echo 'I succeeeded!'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
            echo 'I am unstable :/'
        }
        failure {
            echo 'This will run only if failed'
            echo 'I failed :('

            mail to: 'team@example.com',
                 subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
                 body: "Something is wrong with ${env.BUILD_URL}"

            hipchatSend message: "Attention @here ${env.JOB_NAME} #${env.BUILD_NUMBER} has failed.",
                        color: 'RED'

            slackSend channel: '#ops-room',
                      color: 'good',
                      message: "The pipeline ${currentBuild.fullDisplayName} completed successfully."
        }
        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'
            echo 'Things were different before...'
        }
    }
}
Donate - Support to make this site better.
捐助 - 支持我让我做得更好.