How to git push from a Jenkinsfile?

22,362 views
Skip to first unread message

Idan Adar

unread,
Mar 24, 2017, 7:37:57 AM3/24/17
to Jenkins Users
How do you get "git push" to work in a Jenkinsfile? I am not sure what am I missing.

The Jenkinsfile checkouts a repo using an access token (this is configured in the Jenkins UI for the job).
The Jenkins file also does a merge of a PR in one of its stages: 

withCredentials([usernamePassword(credentialsId: '****', usernameVariable: 'ACCESS_TOKEN_USERNAME', passwordVariable: 'ACCESS_TOKEN_PASSWORD',)]) {
    sh
"curl -X PUT -d '{\"commit_title\": \"Merge pull request\"}'  https://***/pulls/$CHANGE_ID/merge?access_token=$ACCESS_TOKEN_PASSWORD"
}

But I can't get "git push" to work in another stage:

git config --global user.name '****'
git config
--global user.email ****
git commit
-am 'Bumped version number'
git remote
set-url origin git@****.git
git push origin master

I tried wrapping the above in withCredential and also . with agent, but nothing works...  

Tips?

Idan Adar

unread,
Mar 24, 2017, 7:44:58 AM3/24/17
to Jenkins Users, gl...@cloudbees.com
Note: this is done in a declarative pipeline.

jglick, thoughts?

Idan Adar

unread,
Mar 24, 2017, 7:50:45 AM3/24/17
to Jenkins Users, gl...@cloudbees.com
Examples: 

git push '****:****@https://github.****.git/'

But it says: fatal: I don't handle protocol '****:****@https'

Then also tried:
git remote set-url origin https://****:****@github.****.git
git push origin master

But it fails with:
fatal: unable to access 'https://****:****@github.****.com:****.git/': Could not resolve host: ****:****@github.****.com; Name or service not known

With sshagent:
git remote set-url origin git@github.****.com:****.git
+ git push origin master
Host key verification failed.
fatal: Could not read from remote repository.

Mark Waite

unread,
Mar 24, 2017, 8:05:35 AM3/24/17
to Jenkins Users, gl...@cloudbees.com
PR452 is a pending pull request for the git plugin which adds GitPublisher support to pipeline.

If you're willing to download and test https://ci.jenkins.io/job/Plugins/job/git-plugin/view/change-requests/job/PR-452/lastSuccessfulBuild/artifact/target/git.hpi , you could provide feedback on that pull request to tell if it works for you or not.

Mark Waite

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/16980860-a0fc-42d0-b091-eb5f607001fd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Idan Adar

unread,
Mar 24, 2017, 8:11:33 AM3/24/17
to Jenkins Users, gl...@cloudbees.com
I can try. 
What are the instructions after updating the plugin?

Mark Waite

unread,
Mar 24, 2017, 8:16:36 AM3/24/17
to Jenkins Users, gl...@cloudbees.com
I suspect that the "Pipeline Syntax" link will display a new step "Git Publisher" which can then be used to generate the Jenkinsfile syntax for the Git Publisher.

If you're using declarative, then I suspect that it's user interface will also show a Git Publisher step.

Mark Waite

Idan Adar

unread,
Mar 24, 2017, 8:29:56 AM3/24/17
to Jenkins Users, gl...@cloudbees.com
So, a scenario: assuming I have checked out the master branch, ran some unit tests and if all is okay I published to npm... 
I now up the version number in package.json and I want this change pushed back to master... using this plugin and the following generated snippet:

gitPublisher branchesToPush: [[branchName: 'master']], credentialsId: 'caf2691d-42c7-4d10-acf0-cf5fcc2575a4', url: '****'

The master branch should be updated the changed file?

Idan Adar

unread,
Mar 24, 2017, 8:31:56 AM3/24/17
to Jenkins Users, gl...@cloudbees.com
More context:

sh "npm --no-git-tag-version version minor"
                 
// commit the updated version number back to GHE
sh
"git commit -am 'Bumped version number [ci skip]'"
gitPublisher branchesToPush
: [[branchName: 'master']], credentialsId: 'caf2691d-42c7-4d10-acf0-cf5fcc2575a4', url: 'https://***'

Mark Waite

unread,
Mar 24, 2017, 8:34:34 AM3/24/17
to Jenkins Users, gl...@cloudbees.com
That seems reasonable.  I haven't evaluated that pull request, so I don't know the steps for sure, that's just my assumption.

Mark Waite

Idan Adar

unread,
Mar 24, 2017, 8:41:10 AM3/24/17
to Jenkins Users, gl...@cloudbees.com
Unfortunately it fails with ERROR: No branch repo to push to defined

Joshua Noble

unread,
Mar 24, 2017, 9:18:28 AM3/24/17
to Jenkins Users
Generally when you do a basic checkout, the Jenkins workspace is in a detached HEAD state. You want to check out to a local branch likely using "localBranch". I have to ask though - if Jenkins does a merge of the PR - why would you need to push?

Idan Adar

unread,
Mar 24, 2017, 9:31:59 AM3/24/17
to Jenkins Users
Thanks for the question Joshua.
Here's the full Jenkinsfile: http://pastebin.com/nG4iTZhQ

What happens is this:
1. Developers make a pull request to the master branch from their feature branch.
2. The pull_request webhook tells Jenkins to start working based on the stages in the Jenkinsfile. This includes: checkout, unit test, sonar analysis and merge to master.
3. The effect of the merge is basically a push webhook, which means that Jenkins is now going to run the same Jenkinsfile, but on the master branch instead of the Pull Request. So, this time: checkout, unit tests and publish to npm.

In the case where it is running from the master branch, I also bump the version number in package.json and want to push this change back to the master branch.
Developers will then update their feature branch with the updated package.json number...

The part that is not working is the git push.
You have mentioned "localbranch". I haven't heard of this or know how to do this. Can you elaborate?

Idan Adar

unread,
Mar 24, 2017, 10:37:58 AM3/24/17
to Jenkins Users
Joshua, perhaps you mean something like this?
The git commands likely miss something...

stage ("Publish to npm") {
   steps
{
      script
{
         STAGE_NAME
= "Publish to npm"
     
}
     
     
// Prepare the workspace
      deleteDir
()
      sh
'''
         git checkout -b master
         npm --no-git-tag-version version minor
         git commit -am '
Bumped version number [ci skip]'
         git push origin master
      '''

     
}
   
}
}

Kevin Burnett

unread,
Mar 25, 2017, 12:08:55 AM3/25/17
to Jenkins Users
you may want to omit the -b in "git checkout -b master" if the master branch already exists (seems likely).

we use this to make maven release work on any branch (maven release has a similar issue where it cannot work with detached head):

sh 'git checkout ' + env.BRANCH_NAME

Idan Adar

unread,
Mar 25, 2017, 2:26:20 AM3/25/17
to Jenkins Users
Okay, solved at last.

In addition to the below stage, for your reference, I had to also add the id_rsa and id_rsa.pub files to ~/.ssh. Then I had to do a git push manually from the command line which then instructed me to trust the ssh key. Following this, the script from the stage then worked.

The problem I am facing now is that it seems the "ci skip" plug-in is not working. Despite the presence of "[ci skip]" in the commit message, the job is being triggered in a loop (because the job makes a push to the master branch, meaning a push webhook again triggers it...

stage ("Publish to npm") {
         steps
{
            script
{
               STAGE_NAME
= "Publish to npm"

         
               
if (BRANCH_NAME == "master") {
                 
// Publish to npm and bump the version number for the next release
                  sh
"npm publish"
                  deleteDir
()
                  sshagent
(credentials: ['e276113e-0ec9-4eaa-88f9-a7db5c9635b6']) {
                     sh
'''
                        git clone git@****.git
                        cd ****
                        npm --no-git-tag-version version minor

                        git config --global user.name "****"
                        git config --global user.email ****
                        git commit -am 'Bumped version number [ci skip]'
                        git push origin master
                     '''

                 
}
                 
                  slackSend
(
                     color
: '#199515',
                     message
: "$JOB_NAME: Build #$BUILD_NUMBER passed successfully."
                 
)
               
}
               
else {
                  echo
"Not in 'master' branch. Don't attempt publishing."
               
}
           
}
         
}
     
}
   
}

Reply all
Reply to author
Forward
0 new messages