0

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. I've written a jenkinsfile for the same.

Now the issue is, I need to commit and push the mstest results file to the same branch on git. I've tried to do the same using 'bat' in jenkinsfile, but it gives me Detached head state. Below is the jenkinsfile configuration:-

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'
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Shir
  • 13
  • 6

1 Answers1

0

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.

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:

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.

fishi0x01
  • 3,579
  • 21
  • 25