如何从Jenkins的单个构建中触发多个促销活动?

时间:2016-08-25 23:27:22

标签: jenkins jenkins-plugins continuous-deployment

我们正在使用"推广的构建插件"。我们的情况是我们有多个组件。每个组件都有一个构建。成功构建后,"部署到分段"触发促销并将组件部署到我们的登台服务器。每个组件构建还有一个部署到生产和#34;促销,(正如您所猜测的)将组件部署到我们的生产服务器。

目前,所有"部署到生产"促销是手动的。有人需要在经过某种验证后手动触发促销到生产,有时我们会忘记。有时我们的组件不同步。 99%的时间,我们只想确保生产与分期相同。

我的目标是创建一个单击解决方案,该解决方案贯穿我们所有的组件构建并触发"部署到生产"在每个组件的最新版本上进行促销(如果尚未提升)。实现这一目标的最佳方法是什么?

编辑:以下是我们用于查找未启动版本的所有作业的脚本。正如评论中所提到的,我们不想自动推广构建。这需要是一个手动过程。

import jenkins.model.Jenkins
import hudson.plugins.promoted_builds.*
import org.jvnet.hudson.plugins.groovypostbuild.*

def promotionName = "Released to PROD" 

def getLatestPromotion = { promotedProjectAction, promotionProcess ->
  if (!promotedProjectAction || !promotionProcess) {
    return
  }
  def allPromotions = promotedProjectAction.getPromotions(promotionProcess)
  if (allPromotions.size == 0) {
    println ''
    return 
  }
  def latestPromotion = allPromotions.get(allPromotions.size() - 1)
}

def jobs = Jenkins.instance.getAllItems()
def okJobs = []
def badJobs = []
def ignoredJobs = []
jobs.each { job ->
  def projectName = job.fullName
  // projects that end with '-build' are promotable
  if (!projectName.endsWith('-build')) {
    ignoredJobs += projectName
    return
  }

  def project = Jenkins.instance.getItemByFullName(projectName)
  def lastBuild = job.lastBuild

  def promotedBuildAction = lastBuild.getAction(PromotedBuildAction)
  if (!promotedBuildAction) {
    ignoredJobs += projectName
    return 
  }
  def promotedProjectAction = project.getAction(PromotedProjectAction)
  if (!promotedProjectAction) {
    ignoredJobs += projectName
    return 
  }
  def promotionProcess = promotedProjectAction.getProcess(promotionName)

  def latestPromotion = getLatestPromotion(promotedProjectAction, promotionProcess)
  if (!latestPromotion) {
    ignoredJobs += projectName
    return 
  }

  if (latestPromotion.rootBuild.number != lastBuild.number) {
    badJobs += "${projectName} last build #${lastBuild.number} last promotion #${latestPromotion.rootBuild.number}"
  } else {
    okJobs += projectName
  }
}

if (badJobs.size() > 0) {
  println 'WARNING---These jobs have on STAGING, but not on PROD'
  badJobs.forEach { msg -> println '  ' + msg }
  println ''
}

println 'These jobs are up to date on PROD'
okJobs.forEach { msg -> println '  ' + msg }
println ''


println 'These jobs are not promoted to PROD and are ignored in this build'
ignoredJobs.forEach { msg -> println '  ' + msg }
println ''

if (badJobs.size() > 0) {
  throw new Exception("Found ${badJobs.size()} unpushed jobs.")
}

结果将是已升级到暂存但尚未投入生成的构建列表。然后,我们手动点击"推广"如果必要且安全的话,可以使用每个按钮。

0 个答案:

没有答案