2

I'm trying to commit some files in separate commits in a Jenkins Pipeline using the following code:

            sh '''
                git add $SOURCE_ENV/metadetail/current/*
                git commit -m "Updating Snapshot Metadata"
                git add $SOURCE_ENV/release/*
                git commit -m "Package for Release Branch"
                git push -u origin HEAD:$BRANCH_NAME
                git checkout master
                git checkout -b $BRANCH_NAMEfb
                git cherry-pick $BRANCH_NAME
                git push -u origin HEAD:$BRANCH_NAMEfb
            '''

The aim here is to do 2 separate commits on the current branch, create a new feature branch from master, then cherry-pick the 2nd commit from original branch onto the new branch. This is failing the jenkins build following the first commit, with the following message:

+ git commit -m Updating Snapshot Metadata
HEAD detached from 4b8ea1e
Untracked files:
    dPATCH/release/

nothing added to commit but untracked files present
script returned exit code 1

I'm not certain what is going on - my understanding is that only files in 'staging' are important when committing so not sure why the other files are being picked up when I'm only initially running git add on a subset of the files. Any ideas?

nom
  • 95
  • 2
  • 9

1 Answers1

2

I'm not certain what is going on

As I mentioned in "Adding git branch as a properties using Gradle on Jenkins", Jenkins would, by default, checkout the HEAD, not the branch, setting its workspace in a detached HEAD mode (hence your first commit output)

You need to make sure to checkout the branch, before starting making commits.

See Jenkins issue 6856: Git builds with detached head no matter what:

The "pipeline syntax" link on the left of most pipeline job pages will open a page where you can prototype the code you need to perform a checkout to a specific branch.
Use the "checkout" selection from that list (not the "git" selection), then select "Git" and add the "Additional Behaviours" for "Check out to specific local branch".
You can then set a specific branch name, or you can use "**" to indicate that it should use the branch name from the repository where the clone was performed.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250