How To Pull Files From GitHub Project Using Jenkins

How To Pull Files From GitHub Project Using Jenkins

We can configure ssh and add a git repository URL in Jenkins. you can also run the git command using publish over ssh plugin in Jenkins. We will configure the GitHub URL repository in Jenkins. Let’s see, how to pull files from Github.

If you want to pull files from the Github repository on your project directory, So we can create a job for this to complete the task.

Note: Before starting, you should have network connectivity between both the servers. we will have to configure ssh and then we can run git command in Jenkins.

SSH Configuration:

We have to make a connection from Jenkins server to the remote server using publish over ssh plugins. Let’s install the required plugins for SSH on Jenkins.

Log in to the Jenkins server -> Manage Jenkins -> Manage plugins

Now look for SSH in the available section.

jenkins deploy to remote server

I’ve already installed them. you can check in the installed section by searching “ssh“.

Once all the plugins installed, we need to configure the SSH. Go back to Manage Jenkins then select configure systems.

Check Out: Boot Ubuntu Machine In Rescue and Emergency Mode

Now give a password of the ssh user in “Passphrase

Click on the Add server and put the server information as shown in the below figure. Replace the Settings according to your details. you can also check the connectivity when you click on “test configuration“.

you should receive the “success” message. We’ve successfully made SSH Connection. 

Configure the Git On the Project:

Let’s create a job on the Jenkins server. Click on “new item” and give it a name “test” and click on “Ok“. 

If you want to define a Description of the project, So you can do here. 

how to connect remote server using jenkins

Select ‘git‘ and give a path of the repository project from Github and also define the branch name as shown in the below figure. you can give the credentials if you have for this repository only when you have the private repository.

Check Out: How To Install Window 10 In Vmware Workstation Pro

you don’t need to give any credentials details here if the repository is public. 

how to configure git repository in jenkins

Now come to the “build environment” where you have to select “send files or execute commands over ssh” and Select your ssh server. 

how to install git plugin in jenkins manually

I’ll run a few commands and execute them using Jenkins’s job.  I will create a directory “abc” inside the /home/admin.

run git command over ssh using jenkins

Once the directory is created inside the location you have to initialize the git repository using “git init” and add the remote repository using the below commands.

[admin@redhat abc]$ git init
[admin@redhat abc]$ git remote add origin

We can check the remote repository added in this directory using the below commands.

[admin@redhat abc]$ git remote -v
origin (fetch)
origin (push)

You will have to run the “git pull” command at least once inside the folder and it will ask your GitHub credentials to verify the account.

Check Out: How To Solve Jenkins HTTP Error 404 On Redhat / CentOS

After this, it will pull the changes to the current directory and next time we will do this using the Jenkins. we won’t need to give the credentials again.

[admin@redhat abc]$ git pull

Git configuration is finished.

It’s time to play with Jenkins. So I will run the few commands as shown in the below figure where I will pull the changes using the Jenkins and checkout the branch as well.  

how to pull files

The output of the build:

git command with jenkins

Now check on the server in /home/admin/abc directory. you will see the file valid1 in the “failure” branch.

git jenkins command

Note: if you get any error like ” error: cannot open .git/FETCH_HEAD: permission denied” issue when you run the job using Jenkins.

configure ssh in jenkins

This is because you have made SSH connection with the admin user and the owner of the git project will be someone else on the server when you created it.

Check Out: Create Gitlab Admin User After Authentication With AD

To fix it, you have to make admin to the owner of the git project on the server using the below commands. 

[root@redhat ~]# chown -R admin:admin /home/admin/abc 

You’re done

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment