一尘不染

Jenkins的日志更改

jenkins

现在,我的构建过程使用MS Build从源进行构建,并在构建过程中执行自定义程序。我在程序中写入控制台的所有内容都记录在控制台输出中。

但是,我也想在用户界面的“更改”和/或“状态”部分中记录一些条目(类似于SVN所做的事情)。

如何才能做到这一点?


阅读 365

收藏
2020-07-25

共1个答案

一尘不染

// 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)
2020-07-25