Create and push new git branch?

25 views
Skip to first unread message

jbr...@snapapp.com

unread,
Aug 7, 2018, 8:24:10 PM8/7/18
to Jenkins Users
Hi folks,

I've googled extensively, but I haven't seen any documentation or answers on how to create a new git branch in Pipeline. I must be missing something obvious, but the Git plugin page doesn't indicate anything other than checking out code with credentials. Has anyone here used Jenkins to create a branch and then push it to remote?

Thanks!
Jack

Mark Waite

unread,
Aug 7, 2018, 9:10:52 PM8/7/18
to jenkins...@googlegroups.com
The git plugin has no facility to support creating a branch or pushing a branch from a Pipeline step.

However, Pipeline can do it with an "sh" or "bat" step (depending on your platform).

If your repository URL is an ssh URL ( g...@example.com:dir/repo.git or ssh://g...@example.com/dir/repo.git ) then you would use place the sh or bat step inside an ssh-agent block.  The ssh-agent block starts ssh-agent and then command line git will use ssh-agent to authenticate.

If your repository is an http or https URL ( https://example.com/dir/repo.git ) then you would use place the sh or bat step inside a withCredentials block.  The withCredentials block extracts username and password from the Jenkins credentials and makes them available as a variable.  Place that variable inside the git URL in the sh or bat step as in "git push https://${USERNAME}:${PASSWORD}@example.com/dir/repo".  There are examples of withCredentials on the jenkins.io site to give good techniques.

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/22a42e16-f0df-4dad-9e1f-3996cae8d40a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jack Brooks

unread,
Aug 7, 2018, 9:12:34 PM8/7/18
to jenkins...@googlegroups.com
Aha! Thanks so much! I will have to get busy on this. I was looking at it totally wrong. 

You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/37zGosGowKc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAO49JtFn_4Zte%3DmQ_QLDB4JvGiVbt8GY3WbxaqB9hMTxqjM61g%40mail.gmail.com.

Baptiste Mathus

unread,
Aug 20, 2018, 9:01:57 AM8/20/18
to jenkins...@googlegroups.com
On a related note, when you want to automate something, you generally want to think on how you'd do it 'manually'. Here as Mark summarized perfectly, you just use 'sh' facility. Where plugins shine and where they can and should be used is when there's a interesting integration to avoid writing boilerplate in each of your pipeline (like checking out the right Sha for the thing that actually triggered the current build, etc.).

FWIW, it makes me think about the upcoming session of Michaël Pailloncy at Jenkins world next September in San Francisco:

Cheers

Reply all
Reply to author
Forward
0 new messages