Jenkins - 在PR版本中提交更新

时间:2017-12-14 23:37:40

标签: git jenkins

我有一个jenkins工作,我的木偶的东西,所有这些工作确实是验证和部署到木偶。我的问题是,作为该构建的一部分,我想通过bundle强制更新,这将更新puppetfile,然后需要将其作为该PR的一部分进行检查。

我担心的一个问题是PR的提交最终将触发另一个构建,这将触发另一个更新......依此类推。

我唯一能找到的就是你可以在某种程度上控制作业的运行方式(重启/停止和新提交的新功能)但这些似乎是一揽子设置而不是特定于工作实例,我无法通过管道找到控制它的东西。

我必须想象这是人们做的事情,并且有一种方法,但我的谷歌foo失败或我正在搜索错误的条款。

此外,如果重要的是我们在现场使用github企业。

1 个答案:

答案 0 :(得分:1)

免责声明:可能有一种比我要描述的更好的方式(看起来很重)但我们也发现内置的Jenkins排除设置不足。主要是因为它仅适用于未安排的多分支管道扫描的触发构建。此外,这仅适用于 Pipeline Groovy DSL作业;不要自由式工作或声明性管道工作。

<强>摘要

最佳解释为伪代码

if git changed files since last run == 0 then
  no need to change puppetfile
  end build
endif

if 1 file changed and it is puppetfile then
  if the contents of the change is just the auto-changed puppetfile stuff
    no need to change puppetfile again
    end build
  endif
endif

if we get here then more stuff changed so do the auto-change puppetfile stuff

<强>详情

对于具体实现,我将使用Javascript项目package.json作为示例,但它与puppetfile的情况完全相同。只需将package.jsonpuppetfileversion:替换为您自动生成的不应触发其他构建的内容更改。

首先我们需要一些实用方法:

/**
 * Utility method to run git and handle splitting its output
 */
def git( command ) {
  def output = []
  outputRaw = sh(
    script: "git $command",
    returnStdout: true
  )
  if ( !outputRaw.empty ) {
    output = outputRaw.split('\n')
  }
  return output
}

/**
 * The names of files changed since the last successful build
 */
def getFilesChangedSinceLastSuccessfulBuild() {
  def filesChanged = []
  if (lastSuccessfulCommit) {
    filesChanged = git "diff --name-only $currentCommit '^$lastSuccessfulCommit'"
  }
  echo "DEBUG: Files changed $filesChanged (size ${filesChanged.size()})"
  return filesChanged
}

/**
 * Get changes as a list without context
 */
def getContextFreeChanges() {
  def contentsChanged = []
  if (lastSuccessfulCommit) {
    contentsChanged = git "diff --unified=0 $lastSuccessfulCommit $currentCommit | egrep '^\\+ ' || exit 0"
  }
  echo "DEBUG: Contents changed: $contentsChanged (size ${contentsChanged.size()})"
  return contentsChanged

}

然后我们接受这些并创建needToBumpVersion()(根据需要重命名)将所有这些联系在一起以找出需要发生的任何自动生成更改:

/**
 * Do we need to actually increment version in package.json
 */
def needToBumpVersion() {
  def filesChanged = getFilesChangedSinceLastSuccessfulBuild()
  if (filesChanged.size() == 0) {
    echo "INFO: No files changed, no need to bump version"
    return false
  }
  if (filesChanged.size() == 1 && filesChanged[0] == 'package.json') {
    def contentsChanged = getContextFreeChanges()
    if (contentsChanged.size() == 1 && contentsChanged[0] =~ /"version":/) {
      echo "INFO: Don't need to bump version, it has already been done"
      return false
    }
  }
  echo "INFO: We do need to bump the version, more than just package.json version string changed"
  return true
}

最后然后您可以使用此方法确定是否进行自动生成的更改并提交它们:

if (needToBumpVersion()) {
  // Do bump here
}
相关问题