Create Jenkins Job and Clone Repository From GitHub

Naren Chejara
4 min readDec 25, 2019

--

A Youtube video is also available if you like watching instead of reading

Jenkins's job creation is an effortless process, In this article, we will learn how to create a job in Jenkins and configure project cloning from Git.

Let’s get start

I presume that Jenkins is installed and running on your machine if not then refer to Jenkin Installation steps.

Create a Job

  1. Navigate to Jenkin URL in the browser, In my case, the URL is “http://localhost:8080”.

2. Click on the link highlighted in the image below

3. Enter a Job Name, select “Freestyle project” and hit the “OK” button

You will be redirected to the job configuration page where you can see the following settings

  • General Settings: The section contains the general setting of the job like discarding old builds, support parameters, Disabling the project, etc.
  • Source Code Management: The section contains source code options such as GIT, SVN, etc.
  • Build Triggers: The section contents trigger settings that trigger the build based on the specific condition match
  • Build: The section contains the build steps that can be performed by adding a Batch or shell command
  • Post-build Actions: The section contains the build steps that can be performed after the build action.

Let’s add a build step that prints the date by adding the “Executing shell” step.

  • Click on the “Add build step” drop-down and select “Execute shell”
  • Type the “date” command and save the configuration
  • Build a job by clicking on the “Build Now” link and validating the current date in the build console logs

Clone Project From Git

There are two ways to clone the project(repository) from Github.

  1. Clone with HTTPS
  2. Clone with SSH

Clone with HTTPS

Create a new Jenkins job called ‘Clone-with-https’, move to the “Source Control Management” setting, and choose “Git” options if you cannot see the Git options that mean the ‘GitHub’ plugin wasn’t installed in the Jenkins machine.

Copy and paste your git repository HTTPS URL in the “Repository URL” and specify your branch name in the ‘branches to build’ section, in my case I will keep the default i.e. ‘*/master’.

Save and click on the ‘Build Now’ button to build the job, The Jenkins will create a folder in the workspace and clone the repository in it

Console Output

Clone with SSH

Cloning the repository with an SSH URL requires configuring a public/private key, therefore, Let’s generate the SSH key prior to cloning the repository.

Generating a new SSH key

  1. Open CMD or Git Bash
  2. Execute the below command in the terminal
$ ssh-keygen -t rsa -b 4096 -C "Your email address"

The above command asks the location to store the key and passphrase for the security, I press enter to keep the default location and no passphrase

3. Copy & paste the public key(from id_rsa.pub file) in Github

Add ssh key in Github

Now, Let’s Create a new Jenkins job called ‘Clone-with-ssh’, move to the “Source Control Management” setting, and choose “Git” options.

Copy and paste your git repository SSH URL into the “Repository URL”

Git SSH URL requires credentials, therefore, let’s create & configure credentials.

  • Click on ‘Jenkins’ from the ‘Add’ drop-down button
  • Select ‘SSH Username with private key’ from the ‘Kind’ drop-down and fill username and private key (copy from .ssh/id_rsa)
Tips: You can also choose different credential types from the following available types 
- Username with password
- SSH Username with private key
- Secret File
- Secret text
- Certificates

Save and click on the ‘Build Now’ button to build the job, The Jenkins will create a folder in the workspace and clone the repository in it

--

--