简体   繁体   English

Jenkins多分支管道中的Git commit和push问题

[英]Git commit and push issue in Jenkins multibranch pipeline

I'm working on a multibranch pipeline in Jenkins for building a .Net application and creating ms test results using msbuild on a windows server. 我正在詹金斯(Jenkins)中的多分支管道上工作,以构建.Net应用程序并在Windows Server上使用msbuild创建ms测试结果。 I've written a jenkinsfile for the same. 我已经为它写了一个jenkinsfile。

Now the issue is, I need to commit and push the mstest results file to the same branch on git. 现在的问题是,我需要提交并将mstest结果文件推送到git的同一分支。 I've tried to do the same using 'bat' in jenkinsfile, but it gives me Detached head state . 我试图在jenkinsfile中使用'bat'来做同样的事情,但是它给了我Detached head state Below is the jenkinsfile configuration:- 下面是jenkinsfile配置:-

node ('windows') {


stage 'Checkout'

        checkout scm

    stage 'Build'

         bat '"Path to MSBuild.exe" ProjectFile.proj'

         bat '"Path to git.exe" add mstest/output.trx'
         bat '"Path to git.exe" commit -am "adding test results"'
         bat '"Path to git.exe" push origin Develop'
}

The call checkout scm always checks out a specific commit (latest commit on the branch from the time the Jenkinsfile was loaded) and results in a detached HEAD state. 调用checkout scm总是checkout scm特定的提交(从Jenkinsfile加载时起分支上的最新提交),并导致HEAD状态分离。

If you want to checkout a specific branch and later commit changes to it, you could use git directly, which in your case may look like that: 如果您要签出特定分支并稍后提交更改,则可以直接使用git,在您的情况下,它可能看起来像这样:

bat "<path-to-git.exe> checkout ${env.BRANCH_NAME}"

Obviously, in case someone else pushes to that branch after the checkout, but before jenkins pushes the test results, this approach might lead to unexpected behavior. 显然,如果有人在结帐后但在詹金斯推送测试结果之前将其推送到该分支,则此方法可能会导致意外行为。 Note, that env.BRANCH_NAME is a build variable from Jenkins multibranch projects which gives you the name of the branch for which your pipeline is running. 请注意, env.BRANCH_NAME是Jenkins多分支项目的一个构建变量,它为您提供管道正在运行的分支的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM