<转>Jenkins流水线获取提交日志

写在前

之前使用Jenkins pipeline的时候发现拿不到日志,使用multiple scms插件对应是日志变量获取日志的方式失效了,

但是查看流水线Pipeline Syntax发现checkout竟然有包含提交日志的选项,这里一定有办法获取到日志,苦于之前时间紧任务重,就先当它不能获取日志😄

最近在搞点东西,顺便想到了点关键词终于google到了,没看到其它博客里有写,就记录一下

实现原理

在pipeline块外部声名一个使用@NonCPS修饰的方法,从构建时变量currentBuild.changeSets中获取日志对象,遍历对象得到更新日志

Groovy代码

在pipeline的stages/stage/script块中调用这个方法就能得到日志了,写个最简单的demo示意

#!groovy
// Declarative //
pipeline {
	agent any
    stages {
        stage('拉代码') {
            steps {
                //这里就不写了,用pipeline syntax生成一份checkout命令
            }
        }
        stage('输出日志') {
            steps {
                script{
                    //调用方法得到日志 并 输出
                    def changeString = getChangeString()
                    echo "$changeString"
                }
            }
        }
    }
}
 
@NonCPS
def getChangeString() {
    MAX_MSG_LEN = 100
    def changeString = ""
 
    echo "Gathering SCM changes"
    def changeLogSets = currentBuild.changeSets
    for (int i = 0; i < changeLogSets.size(); i++) {
        def entries = changeLogSets[i].items
        for (int j = 0; j < entries.length; j++) {
            def entry = entries[j]
            truncated_msg = entry.msg.take(MAX_MSG_LEN)
            changeString += " - ${truncated_msg} [${entry.author}]\n"
        }
    }
    if (!changeString) {
        changeString = " - No new changes"
    }
    return changeString
}

currentBuild.changeSets数据结构伪代码

另外这里输出的部分不是changeSets的全部,下列伪代码参考了一些,又查API文档写了些,差不多够用了,不够的请参考api猜结构 :happy:

currentBuild.changeSets{
	items[{
    	msg //提交注释
    	commitId //提交hash值
    	author{ //提交用户相关信息
          	id
        	fullName
      	}
    	timestamp
    	affectedFiles[{ //受影响的文件列表
            editType{
                name
            } 
            path: "path"
    	}]
        affectedPaths[// 受影响的目录,是个Collection<String>
			"path-a","path-b"
        ]
  	}]
}

原文链接

Jenkins流水线获取提交日志

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<properties> <hudson.security.AuthorizationMatrixProperty> <inheritanceStrategy class="org.jenkinsci.plugins.matrixauth.inheritance.InheritParentStrategy"/> <permission>USER:hudson.model.Item.Read:anonymous</permission> </hudson.security.AuthorizationMatrixProperty> <jenkins.model.BuildDiscarderProperty> <strategy class="hudson.tasks.LogRotator"> <daysToKeep>90</daysToKeep> <numToKeep>-1</numToKeep> <artifactDaysToKeep>30</artifactDaysToKeep> <artifactNumToKeep>-1</artifactNumToKeep> </strategy> </jenkins.model.BuildDiscarderProperty> <com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="gitlab-plugin@1.7.7"> <gitLabConnection>src.ift.run</gitLabConnection> <jobCredentialId/> <useAlternativeCredential>false</useAlternativeCredential> </com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty> <com.sonyericsson.rebuild.RebuildSettings plugin="rebuild@1.34"> <autoRebuild>false</autoRebuild> <rebuildDisabled>false</rebuildDisabled> </com.sonyericsson.rebuild.RebuildSettings> <hudson.plugins.throttleconcurrents.ThrottleJobProperty plugin="throttle-concurrents@2.11"> <maxConcurrentPerNode>3</maxConcurrentPerNode> <maxConcurrentTotal>0</maxConcurrentTotal> <categories class="java.util.concurrent.CopyOnWriteArrayList"/> <throttleEnabled>true</throttleEnabled> <throttleOption>project</throttleOption> <limitOneJobWithMatchingParams>false</limitOneJobWithMatchingParams> <paramsToUseForLimit/> <configVersion>1</configVersion> </hudson.plugins.throttleconcurrents.ThrottleJobProperty> <jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl plugin="branch-api@2.1071.v1a_188a_562481"> <durationName>hour</durationName> <count>40</count> <userBoost>true</userBoost> </jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl> </properties> 这是jenkin中 job定义中的一段,请解释这一段的含义
07-14
这段代码是 Jenkins Job 配置文件中的一部分,定义了一些与该 Job 相关的属性和插件配置。 让我们逐个解释每个属性的含义: 1. `<hudson.security.AuthorizationMatrixProperty>`:定义了 Jenkins 的权限矩阵属性,指定了该 Job 的权限设置。在这里,`<permission>` 元素指定了匿名用户对该 Job 的读取权限。 2. `<jenkins.model.BuildDiscarderProperty>`:定义了构建丢弃策略属性,该属性决定了构建历史的保留和丢弃规则。在这里,使用了 `<strategy>` 元素指定了构建日志保留的天数和数量限制。 3. `<com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>`:定义了 GitLab 插件的连接属性,用于与 GitLab 服务器进行集成。在这里,`<gitLabConnection>` 元素指定了 GitLab 服务器的地址。 4. `<com.sonyericsson.rebuild.RebuildSettings>`:定义了重新构建插件的设置属性,用于允许或禁止重新构建 Job。在这里,`<autoRebuild>` 元素指定了是否自动重新构建。 5. `<hudson.plugins.throttleconcurrents.ThrottleJobProperty>`:定义了并发限制属性,用于限制并发执行 Job 的数量。在这里,`<maxConcurrentPerNode>` 元素指定了每个节点上允许的最大并发数量。 6. `<jenkins.branch.RateLimitBranchProperty_-JobPropertyImpl>`:定义了分支限制属性,用于限制 Job 分支的创建频率。在这里,`<durationName>` 元素指定了限制时间间隔的单位,`<count>` 元素指定了该时间间隔内允许的分支创建数量。 这些属性和插件配置可以根据你的需求进行修改和调整,以满足特定的工作流程和业务要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值