" /> " />

目 录CONTENT

文章目录

Jenkinsfile

Administrator
2024-02-28 / 0 评论 / 0 点赞 / 8 阅读 / 16033 字

地铁运行线路图:Jenkinsfile

https://www.jenkins.io/doc/book/pipeline/jenkinsfile/

pipline: 流水线

stage: 阶段

关于Jenkins流水线的运行我们可以抽象一下,例如:可以把流水线(pipeline)想象成13号线地铁,把流水线的阶段(stage)想象成地铁的每一个站点,把流水线脚本(jenkinsfile)想象成地铁线路图。这就是流水线的多样性,每条线路都有不同的站点

Jenkinsfile:对pipline的描述,抽象理解地铁的运行线路图

Pipeline

  • Jenkins的Pipeline通过Jenkinsfile进行描述(类似于Dockerfile)

  • 通过Pipeline 的DSL语法可以完成从简单到复杂的交付流水线实现。

Jenkinsfile

  • Jenkinsfile使用两种语法进行编写,分别是声明式和脚本式。

WebHook触发流程

在代码托管服务中配置webhook,在Url中填入:https://your_IP/generic-webhook-trigger/invoke?token=mytoken,并指定webhook触发条件,调用Jenkins

JenkinsFile示例

pipeline {  
    agent {  
        label 'shimingzhi12'  # 指定执行机器
    }  
    triggers {

        GenericTrigger (

            genericVariables: [

                [key: 'ref', value: '$.ref'],

                [key: 'repository_name', value: '$.repository.name'],

                [key: 'repository_git_url', value: '$.repository.git_http_url'],

                [key: 'committer_name', value: '$.head_commit.committer.user_name'],

                [key: 'before', value: '$.before'],

                [key: 'after', value: '$.after']

            ],

            causeString: 'Triggered by $ref',

            printContributedVariables: true,

            printPostContent: true,

            silentResponse: false,

            // regexpFilterExpression: 'refs/heads/' + BRANCH_NAME,

            regexpFilterExpression: '^refs/heads/(master|develop|dev_shimingzhi2|dev_shimingzhi3|dev_shimingzhi4)$',

            regexpFilterText: '$ref',

            token: 'VXnNT5X/GH8Rs'

        )

    } 
    stages {  
        // 多分支Pipline  
        stage('branch:master') {  
            when {  
                branch 'master'  
            }  
            steps {  
                echo '主干分支'  
                // input message: 'Finished using the web site? (Click "Proceed" to continue)'  
            }  
        }  
        stage('branch:develop') {  
            when {  
                branch 'develop'  
            }  
            steps {  
                echo '集成分支'  
                // input message: 'Finished using the web site? (Click "Proceed" to continue)'  
            }  
        }  
        stage('branch:***的开发分支') {  
            when {  
                branch 'dev_shimingzhi4'  
            }  
            steps {  
                echo '个人开发分支'  
                // input message: 'Finished using the web site? (Click "Proceed" to continue)'  
            }  
        }  
        stage('branch:***的开发分支') {  
            when {  
                branch 'dev_shimingzhi3'  
            }  
            steps {  
                echo '个人开发分支'  
                // input message: 'Finished using the web site? (Click "Proceed" to continue)'  
            }  
        }  
        stage('branch:***的开发分支') {  
            when {  
                branch 'dev_shimingzhi2'  
            }  
            steps {  
                echo '个人开发分支'  
                // input message: 'Finished using the web site? (Click "Proceed" to continue)'  
            }  
        }  
        stage('代码扫描') {  
            steps {  
                script {  
                    scannerHome = tool 'sonarqube-scanner'  
                }  
                // Get some code from a GitHub repository  
                withSonarQubeEnv('sonarqube-scanner') {  
                    sh "${scannerHome}/bin/sonar-scanner"  
                }  
                script {  
                    def maxWaitTime = 2 // 最大等待时间,单位:分钟  
                    timeout(activity: true, time: maxWaitTime) {  
                        waitUntil {  
                            sleep(15)  
                            // 等待sonarqube结果回调过来  
                            def qg = waitForQualityGate()  
                            echo "${qg}"  
                            echo "结果状态: ${qg.status}"  
  
                            if (qg.status == 'OK') {  
                                return true  
                            } else if (qg.status == 'IN_PROGRESS') {  
                                sleep(60) // 休眠1分钟后再次检查  
                                // return false  
                            } else {  
                                // 未通过SonarQube的代码质量阈检查,提示用户手动确认  
                                input message: '代码扫描未通过,确认是否跳过代码扫描? (点击“Proceed”继续执行)'  
                                return true  
                            }  
                        }  
                    }  
                }  
                echo '静态代码扫描结束'  
            }  
        }  
        stage('部署服务参数') {
                }
           }
    post {  
        always {  
            echo '强制清理环境'  
            deleteDir() /* clean up our workspace */  
        }  
        success {  
            echo 'I succeeeded!'  
        }  
        unstable {  
            echo 'I am unstable :/'  
        }  
        failure {  
            echo 'I failed :('  
        }  
        changed {  
            echo 'Things were different before...'  
        }  
    }  
}

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区