Jenkins:改变不同工作的构建历史

时间:2015-07-17 14:01:03

标签: jenkins

我有两个jenkins工作:

  • 构建项目
  • 部署它

两者都运行良好,我可以从项目构建作业中触发部署作业。

步骤:

  1. 使用应用程序作业中的参数进行构建>>检查dev>上的部署>构建
  2. 在应用程序作业的构建历史记录中添加黄色星形徽章 - 使用groovy post-build操作(下面的代码)
  3. 以构建后操作的方式触发部署作业
  4. 问题

    1. 部署作业完成后失败,从部署作业更改应用程序作业(黄色星形>>例如红色星标)的构建历史记录。我该怎么办?
    2.     if ("true".equals(manager.build.buildVariables.get('DEPLOY_ON_DEV'))) {
            manager.addBadge("star-gold.gif", "SNAPSHOT deployed on DEV")
            }
      

1 个答案:

答案 0 :(得分:0)

这花了我一段时间来开发,但现在它就像后构建操作→添加后构建操作→Groovy Postbuild→Groovy脚本中的魅力:

import hudson.model.Build
import hudson.model.Cause
import hudson.model.Project
import jenkins.model.Jenkins
import org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildAction

def log = manager.listener.logger
log.println('  ----------------')
log.println('  Groovy Postbuild')

// decorate this build
manager.addShortText('SNAPSHOT deployed on DEV', 'black', 'gold', '1px', 'black')
manager.addInfoBadge('SNAPSHOT deployed on DEV')
manager.addBadge('star-gold.png', 'SNAPSHOT deployed on DEV')

// decorate upstream builds
Jenkins jenkins = Jenkins.getInstance()
List<Project> projects = jenkins.getAllItems(Project.class)

log.println("    This build: '${manager.build}' --> " + manager.build.getResult())
log.println('    Decorating the following upstream builds:')
//log.println(manager.build.getUpstreamBuilds())  // prints "[:]", so using this to get the upstream Builds doesn't work

for (Cause cause : manager.build.getCauses()) {
  for (Project project : projects) {
    if (cause.toString().contains(project.getName())) {
      int no = cause.getUpstreamBuild() 
      Build usb = project.getBuildByNumber(no)
      log.println("      ${usb}")

      usb.getActions().add(GroovyPostbuildAction.createShortText(
          'SNAPSHOT deployed on DEV', 'black', 'gold', '1px', 'black'));
      usb.getActions().add(GroovyPostbuildAction.createInfoBadge(
          'SNAPSHOT deployed on DEV'))
      usb.getActions().add(GroovyPostbuildAction.createBadge(
          'star-gold.png', 'SNAPSHOT deployed on DEV'))
      }
    } // for (projects)
  } // for (causes)

log.println('  ----------------')

注意:

无论构建结果如何,都会添加徽章,但我确信您可以轻松添加相应的if。要删除徽章,请参阅Groovy Postbuild Plugin's page

参考文献: