有没有办法在 Jenkins 2 作业中应用插件后自动提交、推送?

Is there a way to automate the commit, push after applying a plugin in Jenkins 2 job?

我一直在尝试在 jenkins 作业中自动执行 code style formatting,因此在将来添加新代码时,Jenkins 会负责代码格式化(如果未应用)——这也有助于遗留代码存储库。

所以思路是运行mvn install build之前的下一个阶段,

stage('Apply Google code style ') {
      steps {
        script {
          sh "mvn com.coveo:fmt-maven-plugin:format"
        }
      }
    }

如果构建顺利,(自动)通过识别 if 任何 *.java 文件已更改来提交这些更改。

我一直在尝试使用 changesetchangeLogSet 来识别应用插件后的变化,结果发现它们只包含上次提交的变化。 有什么办法可以在结帐后更改 Jenkins 中的文件吗?

Jenkins groovy 函数是这里的答案,检查 git status --porcelain

@NonCPS
Boolean getGitStatus() {
  def changedFiles = sh(returnStdout: true, script: "git status --porcelain")
  if(changedFiles != null) {
    return true
  } else {
    return false
  }
}

正在阶段检查此函数,如果 return 为真则提交。

stage('Check for new changes and commit') {
  steps {
    script {
      if (env.BRANCH_NAME != 'hotfix/*') {
        def changedFiles = getGitStatus()
        if(changedFiles) {
          echo "Changes identified by Jenkins for Auto commit"
          sh "git add -u"
          sh "git commit -m 'Jenkins Auto Commit : Google code styler applied'"
          sh "git push origin $env.BRANCH_NAME"
        } else  {
          echo "No new changes identified"
        }
      }
    }
  }
}