• 01 | jenkins的pipeline 脚本


    1 jenkins官网

    1.1 參考地址

    https://www.jenkins.io/zh/doc/pipeline/tour/hello-world/

    1.2 什么是jenkins pipeline

    Jenkins Pipeline 提供了一套可扩展的工具,用于将“简单到复杂”的交付流程实现为“持续交付即代码”。Jenkins Pipeline 的定义通常被写入到一个文本文件(称为 Jenkinsfile )中,该文件可以被放入项目的源代码控制库中。

    2 执行多个步骤

    2.1 Linux、BSD 和 Mac OS

    Jenkinsfile (Declarative Pipeline)
    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    sh 'echo "Hello World"'
                    sh '''
                        echo "Multiline shell steps works too"
                        ls -lah
                    '''
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.2 Windows

    Jenkinsfile (Declarative Pipeline)
    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    bat 'set'
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.3 超时、重试和更多

    Jenkinsfile (Declarative Pipeline)
    pipeline {
        agent any
        stages {
            stage('Deploy') {
                steps {
                    retry(3) {
                        sh './flakey-deploy.sh'
                    }
    
                    timeout(time: 3, unit: 'MINUTES') {
                        sh './health-check.sh'
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    或者

    Jenkinsfile (Declarative Pipeline)
    pipeline {
        agent any
        stages {
            stage('Deploy') {
                steps {
                    timeout(time: 3, unit: 'MINUTES') {
                        retry(5) {
                            sh './flakey-deploy.sh'
                        }
                    }
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.4 完成时动作

    Jenkinsfile (Declarative Pipeline)
    pipeline {
        agent any
        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'
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    3 定义执行环境

    agent 指令告诉Jenkins在哪里以及如何执行Pipeline或者Pipeline子集。 正如您所预料的,所有的Pipeline都需要 agent 指令。

    Jenkinsfile (Declarative Pipeline)
    pipeline {
        agent {
            docker { image 'node:7-alpine' }
        }
        stages {
            stage('Test') {
                steps {
                    sh 'node --version'
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4 使用环境变量

    Jenkinsfile (Declarative Pipeline)
    pipeline {
        agent any
    
        environment {
            DISABLE_AUTH = 'true'
            DB_ENGINE    = 'sqlite'
        }
    
        stages {
            stage('Build') {
                steps {
                    sh 'printenv'
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    5 记录测试和构建结果

    Jenkinsfile (Declarative Pipeline)
    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    sh './gradlew build'
                }
            }
            stage('Test') {
                steps {
                    sh './gradlew check'
                }
            }
        }
    
        post {
            always {
                archiveArtifacts artifacts: 'build/libs/**/*.jar', fingerprint: true
                junit 'build/reports/**/*.xml'
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    6 清理和通知

    Jenkinsfile (Declarative Pipeline)
    pipeline {
        agent any
        stages {
            stage('No-op') {
                steps {
                    sh 'ls'
                }
            }
        }
        post {
            always {
                echo 'One way or another, I have finished'
                deleteDir() /* clean up our workspace */
            }
            success {
                echo 'I succeeeded!'
            }
            unstable {
                echo 'I am unstable :/'
            }
            failure {
                echo 'I failed :('
            }
            changed {
                echo 'Things were different before...'
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    6.1 电子邮件

    post {
        failure {
            mail to: 'team@example.com',
                 subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
                 body: "Something is wrong with ${env.BUILD_URL}"
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    6.2 Hipchat

    post {
        failure {
            hipchatSend message: "Attention @here ${env.JOB_NAME} #${env.BUILD_NUMBER} has failed.",
                        color: 'RED'
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    6.3 Slack

    post {
        success {
            slackSend channel: '#ops-room',
                      color: 'good',
                      message: "The pipeline ${currentBuild.fullDisplayName} completed successfully."
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    7 部署

    Jenkinsfile (Declarative Pipeline)
    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    echo 'Building'
                }
            }
            stage('Test') {
                steps {
                    echo 'Testing'
                }
            }
            stage('Deploy') {
                steps {
                    echo 'Deploying'
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    7.1 阶段即为部署环境

    stage('Deploy - Staging') {
        steps {
            sh './deploy staging'
            sh './run-smoke-tests'
        }
    }
    stage('Deploy - Production') {
        steps {
            sh './deploy production'
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    7.2 人工确认

    enkinsfile (Declarative Pipeline)
    pipeline {
        agent any
        stages {
            /* "Build" and "Test" stages omitted */
    
            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'
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    8 常见问题

    问题1:No such property: HOME for class: groovy.lang.Binding

    groovy.lang.MissingPropertyException: No such property: HOME for class: groovy.lang.Binding
    
    • 1

    解决方法: 初步判断 是插件的版本问题(待验证)

    问题2:Ubuntu怎么修改下载源

    参考地址:
    https://mirrors.cloud.tencent.com/

    解决方法:

    wget -O /etc/apt/sources.list http://mirrors.cloud.tencent.com/repo/ubuntu18_sources.list
    
    • 1
    deb http://mirrors.cloud.tencent.com/ubuntu/ bionic main restricted universe multiverse
    deb http://mirrors.cloud.tencent.com/ubuntu/ bionic-security main restricted universe multiverse
    deb http://mirrors.cloud.tencent.com/ubuntu/ bionic-updates main restricted universe multiverse
    #deb http://mirrors.cloud.tencent.com/ubuntu/ bionic-proposed main restricted universe multiverse
    #deb http://mirrors.cloud.tencent.com/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src http://mirrors.cloud.tencent.com/ubuntu/ bionic main restricted universe multiverse
    deb-src http://mirrors.cloud.tencent.com/ubuntu/ bionic-security main restricted universe multiverse
    deb-src http://mirrors.cloud.tencent.com/ubuntu/ bionic-updates main restricted universe multiverse
    #deb-src http://mirrors.cloud.tencent.com/ubuntu/ bionic-proposed main restricted universe multiverse
    #deb-src http://mirrors.cloud.tencent.com/ubuntu/ bionic-backports main restricted universe multiverse
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    问题3:NO_PUBKEY 3B4FE6ACC0B21F32

    W: GPG error: https://mirrors.tencent.com/ubuntu bionic InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 3B4FE6ACC0B21F32
    E: The repository 'https://mirrors.tencent.com/ubuntu bionic InRelease' is not signed.
    W: GPG error: https://mirrors.tencent.com/ubuntu bionic-updates InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 3B4FE6ACC0B21F32
    E: The repository 'https://mirrors.tencent.com/ubuntu bionic-updates InRelease' is not signed.
    W: GPG error: https://mirrors.tencent.com/ubuntu bionic-security InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 3B4FE6ACC0B21F32
    E: The repository 'https://mirrors.tencent.com/ubuntu bionic-security InRelease' is not signed.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    解决方法:

    sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 3B4FE6ACC0B21F32
    
    • 1
  • 相关阅读:
    回溯算法 | N皇后 | leecode刷题笔记
    驱动LSM6DS3TR-C实现高效运动检测与数据采集(5)----姿态解算
    Node.js最新版黑马配套笔记
    计算机网络-DNS以及FastGitHub
    【04】基础知识:React组件实例三大核心属性 - state
    Apache Paimon 文件管理
    gitBash中如何使用Linux中的tree命令
    黑马头条-day10
    C++11--lambda表达式--包装器--bind--1119
    金融行业蓄电池监控,究竟有多厉害!
  • 原文地址:https://blog.csdn.net/u013916029/article/details/127527427