小哥之哥 小哥之哥
首页
    • Prometheus
    • Kubertenes
    • Docker
    • MySQL
  • Go
  • Python
  • Vue
  • Jenkins
  • ELK
  • LDAP
  • 随笔
  • 最佳实践
  • 博客搭建
  • 问题杂谈
关于
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

小哥之哥

运维扫地僧
首页
    • Prometheus
    • Kubertenes
    • Docker
    • MySQL
  • Go
  • Python
  • Vue
  • Jenkins
  • ELK
  • LDAP
  • 随笔
  • 最佳实践
  • 博客搭建
  • 问题杂谈
关于
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • LDAP

  • Jenkins

    • Jenkins之Docker部署
    • Jenkins之自动化项目发布
    • Jenkins之使用ansible发布项目
    • Jenkins之pipline初识
    • Jenkins之pipline语法
    • Jenkins之钉钉通知
    • Jenkins最佳实践之为job添加构建预览信息
      • Jenkins最佳实践之webhook
    • 技术专题
    • Jenkins
    tchua
    2023-06-13
    目录

    Jenkins最佳实践之为job添加构建预览信息

    # 一、前言


    前面我们介绍过,关于Jenkins使用流水线的配置相关操作,Jenkins作为一个功能比较全面的CI/CD工具,里面还有很多可以探索的功能,今天我们探索下,为job新增构建描述信息,指的是当我们构建成功之后,可以在构建信息处看到我们配置的信息,这样可以更加直观的看到本次构建人或者构建分支,而不用再去点击console去观察构建信息,这样做,其实也只是为了方便核实,有时候研发侧发布上线后,遇到问题可能会怀疑发布代码有问题,这样我们通过展示发布分支和描述信息,可以快速排出所发布的代码没有问题。

    图片

    # 二、配置


    # 2.1 pipline配置

    完整pipline脚本

    pipeline {
        agent any
        environment {
            git_url = "https://gitee.com/shanghai_moji/supplier.git"
            branchName = "master"
        }
        options {
            buildDiscarder(logRotator(numToKeepStr: '10'))
            disableConcurrentBuilds()
            timeout(time: 10, unit: 'MINUTES')
            timestamps()
        }
        parameters {
            string(name: 'env', defaultValue: 'dev', description: '输入构建的环境')
            choice(name: 'mode', choices: ['hash', 'history'], description: '选择前端构建模式')
        }
        stages {
            stage('清理工作目录') {
                steps {
                    cleanWs()
                }
            }
            stage('pull代码') {
                steps {
                    checkout([$class: 'GitSCM', branches: [[name: "master"]], extensions: [],userRemoteConfigs: [[credentialsId: 'bea96a6a-73a7-473d-b73b-f7bb4f769b2a', url: "$git_url"]]])
                }
            }
            stage('build(构建/CI)') {
                steps {
    				echo '我是构建步骤'
                }
            }
            stage('deploy(发布/CD)') {
                steps {
    				echo '我是发布步骤'
    				
                }
            }
        }
     post {
            success {
                script {
                        def commitAuthor = sh(returnStdout: true, script: 'git log -1 --pretty=format:"%an"').trim()
                        def commitMsg = sh(returnStdout: true, script: 'git log -1 --pretty=format:"%s"').trim()
                        //Jenkins页面显示构建信息
                        wrap([$class: 'BuildUser']) {
                            currentBuild.description = "构建人:${BUILD_USER} --> ✅\n"
                            currentBuild.description += "构建分支: ${env.branchName}\n"
                            currentBuild.description += "提交人: ${commitAuthor}\n"
                            currentBuild.description += "提交信息: ${commitMsg}"
                        }
                    }
            }
            failure {
                script {
                 wrap([$class: 'BuildUser']) {
                            currentBuild.description = "构建人:${BUILD_USER} --> ❌"
                             currentBuild.description += "构建分支: ${env.branchName}"
                        }
                }
            }
            aborted {
                echo "当前构建aborted结果结果: ${currentBuild.currentResult}"
            }
            always {
                script {
                    catchError {
                        echo "当前always.catchError构建查看结果: ${currentBuild.currentResult}"
                        wrap([$class: 'BuildUser']) {
                            buildTime = currentBuild.durationString.split("and")[0]
                            if(currentBuild.currentResult == "SUCCESS"){
                                statusColor = "#4CC417"
                                status = "成功"
                            }else if (currentBuild.currentResult == "FAILURE"){
                                statusColor = "#FF0000"
                                status = "失败"
                            }else if(currentBuild.currentResult == "ABORTED"){
                                statusColor = "#529578"
                                status = "取消"
                            }
                            dingtalk (
                                robot: '120039c5-1111-4d7d-9461-f7499be9cb75',
                                type: 'MARKDOWN',
                                title: "${JOB_BASE_NAME}构建通知",
                                text: [
                                    "### [$JOB_BASE_NAME]($JOB_URL)通知",
                                    "",
                                    "---",
                                    "",
                                    "> * 任务:[#$BUILD_ID]($BUILD_URL)",
                                    "> * 状态:<font color=$statusColor>$status</font>",
                                    "> * 持续时间:$buildTime",
                                    "> * 执行人:${BUILD_USER}",
                                    "> * 构建分支: ${branchName}"
                                ]
                            )
                        }
                    }
                }
            }
        }
    
    }
    
    
    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
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    # 2.2 配置解读

    stages 步骤

    stages步骤中主要定义了相关的打包构建及后续发布操作

    post

    post中我们定义了几种状态,在sucess中使用wrap([$class: 'BuildUser']) {}来处理构建信息,这样可以把

    信息展示在构建视图页面,failure这里考虑的是既然失败了,就没必要去显示详细信息,如果每个状态都显示,可以直接复制到对应的状态即可。

    # 2.3 效果展示

    image-20230613102921090

    注意

    • 在使用wrap([$class: 'BuildUser']) {}中,包装器BuildUser需要安装Build User Vars插件才可以使用
    • currentBuild.description如果想要展示多条,则需要使用+=符号拼接,否则会覆盖
    编辑 (opens new window)
    上次更新: 2023/06/29, 18:27:26
    Jenkins之钉钉通知
    Jenkins最佳实践之webhook

    ← Jenkins之钉钉通知 Jenkins最佳实践之webhook→

    最近更新
    01
    cert-manager自动签发Lets Encrypt
    09-05
    02
    Docker构建多架构镜像
    08-02
    03
    Prometheus数据迁移至VMstorage
    08-01
    更多文章>
    Theme by Vdoing | Copyright © 2023-2024 |豫ICP备2021026650号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式