简体   繁体   English

詹金斯的日志更改

[英]Log changes in Jenkins

Continuing on my question here: Jenkins pass or fail build on external executable 在这里继续我的问题: Jenkins在外部可执行文件上通过还是失败

My build process now builds from source using MS Build, and executes a custom program as part of the build process. 现在,我的构建过程使用MS Build从源进行构建,并在构建过程中执行自定义程序。 Anything that I am writing to the console in my program is being logged in the console output. 我在程序中写入控制台的所有内容都记录在控制台输出中。

However, I would also like to log some entries in the "Changes" and/or the "Status" portions on the user interface (similar to what SVN does). 但是,我也想在用户界面的“更改”和/或“状态”部分中记录一些条目(类似于SVN的操作)。

How can this be done? 如何才能做到这一点?

Here's what I did to add my own log to the normal Jenkins 'Changes' link per build: I ended up having to write my svn ant routines (using svnant) to do checkouts and updates etc... 这是我为每个构建将自己的日志添加到普通的Jenkins'Changes'链接时所做的事情:我最终不得不编写我的svn ant例程(使用svnant)来进行签出和更新等操作。

My Trick To Make it Work: 我的秘诀:
1.) Under "Source Code Management" add "Subversion Modules". 1.)在“源代码管理”下添加“ Subversion模块”。 Do not fill out the section. 不要填写该部分。 I do not want it to check anything out. 我不希望它检查任何东西。 I have to do this to make Jenkins show the changelog.xml file I create, otherwise it can exist but Jenkins will not show it. 我必须执行此操作,以使Jenkins显示我创建的changelog.xml文件,否则它可以存在,但Jenkins将不显示它。 In the job config.xml file you will see SCM...null... without this. 在job config.xml文件中,您将看到SCM ... null ...没有此内容。 When you add the empty svn repo, it thinks you are using svn and then lets your changelog.xml show up. 当添加空的svn存储库时,它认为您正在使用svn,然后显示changelog.xml。 (who knows how long this will work?) (谁知道这可以工作多长时间?)

In your build you want to do these steps in this order: 在构建中,您要按以下顺序执行以下步骤:
2.) write a create-changelog target. 2.)编写一个create-changelog目标。 it should find the revision of code in your build folder. 它应该在您的构建文件夹中找到代码的修订版。 find the revision of code in svn. 在svn中找到代码的修订版。 do svn log asXml="true" or "--xml" from start-rev to stop-rev. svn从start-rev到stop-rev记录asXml =“ true”或“ --xml”。 concat multiple log files together if needed to create ${Jenkins_Home}/jobs/${MYJOB}/builds/${BUILD_NUMBER}/changelog.xml 如果需要创建$ {Jenkins_Home} / jobs / $ {MYJOB} / builds / $ {BUILD_NUMBER} /changelog.xml,可将多个日志文件连接在一起

3.) now you can delete $workspace area if you want to. 3.)现在,您可以删除$ workspace区域。

4.) svn checkout of update 4.)svn更新签出

5.) build 5.)建立

6.) after build, 'Changes' link will show your changelog.xml 6.)构建后,“更改”链接将显示您的changelog.xml

Problems I had: 我遇到的问题:
scm polling did not do tags. scm轮询没有做标签。
scm url was not flexible enough. scm url不够灵活。
groovy-postbuild-plugin messed up my jenkins ui so I could not use it at this time. groovy-postbuild-plugin弄乱了我的jenkins ui,所以我现在不能使用它。
sidebar-link-plugin does not seem to let you do it for each build. sidebar-link-plugin似乎不允许您为每个版本进行。 You might be able to do something with other plugins for build history, but I did not pursue it at this point. 您可能可以使用其他插件来执行构建历史记录,但是我现在还没有进行。

// This is a deliciously convoluted and fragile hack to force Jenkins to show the
// changes via a Groovy Postbuild script:

// fake a Subversion changelog.xml file
changes = new File(manager.build.getRootDir(), "../../workspace/changes.txt")
changelog = new File(manager.build.getRootDir(), "changelog.xml")
changelog.withWriter {
  out ->
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?><log><logentry revision=\""
  + manager.build.number + "\"><date>"
  + new java.util.Date() + "</date><paths>")
message  = ""
changes.eachLine {
   line ->
     if (line.startsWith("./")) line = line.substring(2)
     if (!".checksums".equals(line)) {
       out.println("<path action=\"M\">" + line + "</path>")
       message += line + "\n"
    }
   }
  out.println("</paths><msg>" + message + "</msg></logentry></log>")
}

// get an instance of the SubversionChangeLogParser
import java.net.URL;
import java.net.URLClassLoader;
baseDir = new File(jenkins.model.Jenkins.getInstance().getRootDir(),
  "plugins/subversion/WEB-INF/")
urls = new URL[2];
urls[0] = new File(baseDir, "classes/").toURI().toURL() 
urls[1] = new File(baseDir, "lib/svnkit-1.3.4-hudson-2.jar").toURI().toURL() 
loader = new URLClassLoader(urls,  manager.getClass().getClassLoader())
svn = loader.loadClass("hudson.scm.SubversionChangeLogParser").newInstance()

// force the current build to take that parser, parse the changelog.xml,
// and force it down AbstractBuild's throat, too
scmField = manager.build.getClass().getSuperclass().getSuperclass().getDeclaredField("scm")
scmField.setAccessible(true)
scmField.set(manager.build, svn)

changeSet = svn.parse(manager.build, changelog)
changeSetField = manager.build.getClass().getSuperclass().getSuperclass().getDeclaredField("changeSet");
changeSetField.setAccessible(true)
import java.lang.ref.WeakReference;
if (changeSetField.getDeclaringClass().isAssignableFrom(WeakReference.class))
  changeSet = new WeakReference(changeSet)
changeSetField.set(manager.build, changeSet)

Me again :) 又是我 :)

On the simplest end of the spectrum, to simply add another link on the sidebar, along with "Changes" and "Status" links, there is a sidebar-link plugin 在最简单的范围内,只需在边栏上添加另一个链接以及“更改”和“状态”链接,便有一个边栏链接插件
https://wiki.jenkins-ci.org/display/JENKINS/Sidebar-Link+Plugin https://wiki.jenkins-ci.org/display/JENKINS/Sidebar-Link+Plugin

It allows you to create a link on the job page (not individual job runs however), which can link to a absolute or a relative (to job's configuration) location. 它允许您在作业页面上创建一个链接(但是不能运行单个作业),该链接可以链接到绝对或相对(相对于作业的配置)位置。 You can reference a file within the workspace by putting the following into the link URL: 您可以通过将以下内容放入链接URL来引用工作空间中的文件:

ws/my_output.txt

This however implies that the file is always present in workspace, and at best it would show the result of the last job execution. 但是,这意味着该文件始终存在于工作空间中,并且充其量只能显示上一次作业执行的结果。 Also, clicking the link would take you to the file in workspace, it won't show the rest of the Jenkins UI anymore. 另外,单击链接会将您带到工作区中的文件,它将不再显示Jenkins UI的其余部分。

A more advanced approach would be to use a Groovy Script 更高级的方法是使用Groovy脚本
https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin

Look at examples #1 and #6 看例子#1和#6
在此处输入图片说明在此处输入图片说明

The stuff next to yellow exclamation icon is what had been added to the job's status page by this plugin, and this is fully customizable and allows HTML code. 黄色感叹号图标旁边的内容是此插件添加到作业状态页面的内容,并且可以完全自定义并允许HTML代码。 This however requires knowing Groovy script, but the examples provide enough to do something quick 但是,这需要了解Groovy脚本,但是示例提供了足够的功能来快速完成操作

i have created a simple jenkins plugin to do that ... 我已经创建了一个简单的詹金斯插件来做到这一点...

https://github.com/rmalchow/changelog-appender https://github.com/rmalchow/changelog-appender

this takes creates a list of changesets from all builds since the last successful one, and writes those into a file, with a headline containing the version or build number and the date. 此操作将创建自上次成功构建以来所有构建的变更集列表,并将这些变更集写入文件,并在标题中包含版本或内部版本号和日期。

UPDATE: 更新:

this was for an old version of jenkins, and it hadn't been updated in a long time (we switched to gitlab-ci), so i took down that repo. 这是用于詹金斯的旧版本,并且很长一段时间都没有更新(我们切换到gitlab-ci),所以我删除了该回购协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM