Integrating Git with Jenkins

1 comments

Problem

I want to check-out my automation code from Git to build in Jenkins CI environment.

Pre-Conditions

  • Jenkins is installed and running.
  • Git repository is available.
  • Git user is available.

Solution

Install Git client on CI server
Windows: Download Git installer and run through Installation Wizard

  1. Install "Git plugin" in Jenkins
  • Click "Manage Jenkins"
  • Click "Manage Plugins"
  • Click "Available" tab
  • Enter "Git plugin" in Search box
  • Select "Git plugin"
  • Click "Install without restart"
     3. Add Credentials for Git.
         There are 2 ways to authenticate to Git: via HTTPS 
         To Add HTTPS Credentials
    • In Jenkins GUI: Click "Credentials"
    • Click "Global Credentials"
    • Click "Add Credentials"
    • Select "Username with password"
    • Enter username in Firstname_Lastname@gmail.com format
    • Enter domain password
    • Click OK


    4. Create Jenkins Job.
    • In Jenkins GUI: Click "New Item"
    • Enter Job name
    • Select "Freestyle project"
    • Click OK
    • Select Git under "Source Code Management" section
    • Enter Git repository URL (can be copied from Gitlab UI)
    • Enter branches to be fetched (*/master is a good default)
    •  Add build triggers as necessary. Expand for details...
    • Click Save (we don't add build step in this recipe)

    End State

    • Git plugin is installed in Jenkins
    • Git Credentials are added
    • Jenkins job fetching the code from Git is created and the code is fetched to CI server.

    Limitations / Known issues

    • Windows: Fetching may hang depending on which version of git client (git bash vs git cmd) is in your %PATH%.
      If encountered freezing of fetching, update %PATH% environment variable to another version (I.g. update entry from %GIT_HOME%\cmd to %GIT_HOME%\bin).
    • Jenkins Git plugin is suitable for any kind of git repositories but it contains only basic functionality. If one is using Gitlab or GitHub, specific Gitlab andGitHub Jenkins plugins might be the preferable solution.
      For more documentation please see corresponding plugin pages.