≡ Menu

How to Setup Git Repository and Credentials for Jenkins Jobs

Jenkins is an open source automation server, which will help you to automate the application deployment in your infrastructure.

From Jenkins, for deployment, you can connect to any kind of source code control system, and pull the source, build it, and deploy it automatically to one or more servers.

In this tutorial, we’ll explain how you can create a new Jenkins Job, and connect it to a Git repository to pull the source code for building.

Install Jenkins Git Plugin

For this, you should have “Git Plugin” installed in your Jenkins.

Login to Jenkins UI using your admin account, and go to “Manage Jenkins” -> Manage Plugins -> Click on “Installed” Tab -> From here, search for “git plugin” in the filter.

If the “Git plugin” is already installed, it will display it here as shown below.

Jenkins Git Plugins List

During Jenkins installation, if you’ve chosen to install the “recommended plugin”, you’ll already have “git plugin” as shown above.

If you don’t see it, then install it. Go to “Available” tab, search for “git plugin”, and click on “Install without restart”.

From your plugins list, if you see that “Git Plugin” is an older version, then upgrade it to the latest version as explained here: How to Upgrade Jenkins to New Version and Update Plugins

Create New Jenkins Job

Click on “New” to create a new jenkins job. Select “Free Style Project” as type as shown below. Name: Dev App Build. Type: Free Style Project

Jenkins Git Create New Job

By default this will go into editing the job after creating it. In the edit mode, you’ll see multiple tabs on the top, and “General” tab will be selected.

Select “Git” for Source Code Management

For this particular job, click on “Source Code Management” tab. This will have the following options:

  • None (This option will be selected by default).
  • CVS
  • CVS Projectset
  • Git
  • Subversion

Click on “Git” to select it. This will expand the section, and show all git related parameters as shown below.

Jenkins Job Git Source Code Management Tab

Under the Git section, in the Repositories section, you’ll see the following:

Repository URL: This will be the URL of your remote repository. The syntax is same as what you’ll specify in the git command line. For example: username@git-server-ip:/git/repository/full/path

The following are some possible syntax for the repository URL:

  • ssh://git@github.com/github/git.git
  • git@github.com:github/git.git
  • ssh://user@other.host.com/~/repos/R.git
  • https://github.com/github/git.git
  • git://github.com/github/git.git

Credentials: By default, this will be “None”. There will an “Add” button next to it. From here, you can specify the credentials that are required for the Git URL specified above.

Advanced button: When you click on the “Advanced” button, you’ll see two more text boxes. For most cases, you don’t need to enter anything here.

  • Name – You can leave this empty, and Jenkins will automatically generate unique name. This “name” is exactly the same as the one used in the git remote command.
  • Refspec – The default refspec is “+refs/heads/*:refs/remotes/REPOSITORYNAME/*”. This is Ok for most of you, and you can leave this empty. By default will be retrieve all the branch heads as remotes/repositoryname/branchname. Instead, if you always want to retrive just one branch, you may want to modify this.

Add repository button: Click on “Add repository” to create this as the default git repository to be used by this particular Jenkins Job.

Branches to build section: In this section under “Branch Specifier (blank for ‘any’):”, the default value is */master. For most cases, you can leave this to default. This will track only the master branch. But, if you leave this blank, all branches will be examined for changes and built. The syntax for this field is: refs/heads/

The following are various possible examples for the “Branch Specifier” field:

  • refs/heads//origin/master
  • remotes/origin/master
  • refs/remotes/origin/master
  • tag-name
  • refs/tags/tag-name
  • commit-id
  • ${ENV_VARIABLE}
  • ${TREEISH}
  • refs/tags/${TAGNAME},…
  • Wildcards: e,g: *, **
  • :regular-expression

Add Branch button: Click on this button to add the branch specified in the above “Branch Specifier” field to be used.

Repository browser: By default, this drop down list will be set to “Auto”. In most cases, you can just leave it to default. This will create the links for the “changes” view inside the Jenkins to an external system for browsing. The “Auto” option will automatically choose the best possible repository browser for the selected SCM.

The following are possible values for this Repository browser:

  • Auto
  • AssemblaWeb
  • FishEye
  • Kiln
  • Microsoft Team Foundation Server/Visual Studio Team Services
  • bitbucketweb
  • cgit
  • gitblit
  • githubweb
  • gitiles
  • gitlab
  • gitlist
  • gitoriousweb
  • gitweb
  • phabricator
  • redmineweb
  • rhodecode
  • stash
  • viewgit

When you create a new Jenkins job, make sure the security is enabled on that particular job as explained here: How to Setup User Security on Jenkins with Project Matrix Authorization

Setup Jenkins Credentials for Git

When you create the repository, you might get the following error:

Repository URL error 1: ramesh@192.168.101.2:/home/git/devapp
Failed to connect to repository : Command "git ls-remote -h ramesh@192.168.101.2:/home/git/devapp HEAD" returned status code 128:
stdout:
stderr: Host key verification failed.
fatal: The remote end hung up unexpectedly

Typically, you’ll get the above error message, when your “Credentials” under “Git” is set to “None”.

To add a credential, click on “Add” next to “Credentials” -> Select “Jenkins Credential Provider”, this will display the following add credentials screen.

Jenkins Git Create Credentials
  • Domain: By default “Global credentials (unrestricted)” is selected. Other option is: “Username and password”. Use default.
  • Kind: The following are the possible options:
    • Username and password (This is shown in the above screenshot).
    • SSH Username with private key
    • Secret file
    • Secret text
    • Certificate
  • Username – Specify the username that will be used to connect to the remote git repository
  • Password – Specify the password for the above username.
  • ID – Leave this empty. This is an internal unique ID that are used to identify this credentials in the job. When you leave this empty, Jenkins will auto generate an id.
  • Description – Descriptive notes about this credentials

If you choose, the SSH username with private key, you’ll see the following screen:

Jenkins Git Create Credentials SSH
  • Kind: SSH Username with Private Key
  • Username – Specify the username that will be used to connect to the remote git repository
  • Private Key: The following are three options for this:
    • Enter directly
    • From a file on Jenkins master
    • From the jenkins master ~/.ssh
  • Passphrase – The passphrase for your key
  • ID – The unique ID (same as what is explained above)
  • Description – Notes

Once the credentials are created, complete the remaining sections in the “Git” section under the “Source Code Management” tab for your particular job.

After this, if you want to view all the credentials that you’ve created so far in Jenkins, go to “Jenkins” -> Click on “Credentials” on the menu, which will list all the credentials that you’ve created inside Jenkins so far as shown below.

Jenkins Git Credentials List

So far we’ve created a new Jenkins job and associated it to a specific git repository, and specified which branch from the repository to be used when this particular job is executed.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Malhar October 10, 2016, 2:40 pm

    Very god summary of using git plugin.
    I am having trouble enabling credentials. I am getting following error:
    Failed to connect to repository : Error performing command: /usr/local/bin/git ls-remote -h ssh://ambus-git.xxxx.com:29418/xxx/packages/dag.git HEAD

    The Jenkins user (jruser) does not have account ambus-git. I (mpalkar) have access. Should I create credential as mpalkar? Using “private key”? Just copy my (mpalkar) ~/.ssh/id_rsa? along with —- lines?

    BTW, username/password did not work.
    Thanks.

    /MALHAR

  • fais October 16, 2016, 7:57 pm

    Got this error when fill in Repository URL

    Failed to connect to repository : Could not init C:\Windows\TEMP\hudson1187545182870999980tmp

    Anyone can help me? Thanks in advance

  • Manassés Souza March 2, 2017, 8:32 am

    I think it’s worthy to add how to config the jenkins SSH:
    1) generate a new SSH key at jenkins machine
    2) Add the pub key to specific account (github, bitbucket, etc)
    3) Choose the following option to kind “SSH Username with private key” (when adding a new Credential)
    * From the Jenkins master ~/.ssh

  • harvey lin March 22, 2017, 11:48 am

    Hi @Manassés Souza, can you be more specific about how to do this? I am stuck at this point as well, can not get Jenkins to work with SSH. If you can point me to a guide online that will be great, thanks!

  • vikram April 11, 2017, 6:35 am

    I am getting error while connecting to the git server from jenkins
    ——————output————

    FailedConsole Output

    Started by user king
    Building in workspace /var/lib/jenkins/workspace/gitdemo
    Cloning the remote Git repository
    Cloning repository 10.6.108.79:/home/git/Fickle.git
    > git clone ravi.babu@10.6.108.79:/home/git/Fickle.git init /var/lib/jenkins/workspace/gitdemo # timeout=10
    ERROR: Error cloning remote repo ‘origin’
    hudson.plugins.git.GitException: Could not init /var/lib/jenkins/workspace/gitdemo
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$5.execute(CliGitAPIImpl.java:664)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$2.execute(CliGitAPIImpl.java:465)
    at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1061)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1101)
    at hudson.scm.SCM.checkout(SCM.java:495)
    at hudson.model.AbstractProject.checkout(AbstractProject.java:1280)
    at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:604)
    at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:86)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:529)
    at hudson.model.Run.execute(Run.java:1730)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:405)
    Caused by: hudson.plugins.git.GitException: Error performing command: git clone ravi.babu@10.6.108.79:/home/git/Fickle.git init /var/lib/jenkins/workspace/gitdemo
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1806)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1772)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1768)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommand(CliGitAPIImpl.java:1415)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$5.execute(CliGitAPIImpl.java:662)
    … 12 more
    Caused by: java.io.IOException: Cannot run program “git clone ravi.babu@10.6.108.79:/home/git/Fickle.git” (in directory “/var/lib/jenkins/workspace/gitdemo”): error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at hudson.Proc$LocalProc.(Proc.java:245)
    at hudson.Proc$LocalProc.(Proc.java:214)
    at hudson.Launcher$LocalLauncher.launch(Launcher.java:850)
    at hudson.Launcher$ProcStarter.start(Launcher.java:384)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1795)
    … 16 more
    Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.(UNIXProcess.java:247)
    at java.lang.ProcessImpl.start(ProcessImpl.java:134)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    … 21 more
    ERROR: null
    Finished: FAILURE

  • Manassés Souza May 10, 2017, 4:43 pm

    Hey @harvey lin
    I wonder why I received the notification about your comment only today! it’s been a while since I done that configuration but I had not followed some guide, but tried some configurations and the mentioned sequence worked. Are you still stuck on this?

  • harvey lin May 10, 2017, 5:31 pm

    Hi Manasses, yes I am still stuck, can’t get SSH to work, so I am using the cleartext version of the repository setting now. Thanks for replying though, I wonder if you can update the page since some things has changed for Jenkins? Thanks a ton!

  • Manassés Souza May 28, 2017, 7:23 am

    I’m afraid I cannot update the page @harvey lin, as I’m not the one who published the article. Have you followed exactly the sequence I posted? If you prefer, send me your contacts so we can get in touch later.

  • Sonali April 7, 2020, 12:52 am

    Thank you for sharing this detailed information on the setting of credentials for jobs. It’s beautifully explained.
    My question “Is it possible to achieve the same using Jenkinsfile?”
    I mean can we create the crrdentials using jenkinsfile directly instead of creating in the GUI?

    Thanks in advance.