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.
Log in to the Jenkins server -> Manage Jenkins -> Manage plugins
Now look for SSH in the available section.
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.
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.
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.
you don’t need to give any credentials details here if the repository is public.
Now come to the “build environment” where you have to select “send files or execute commands over ssh” and Select your ssh server.
I’ll run a few commands and execute them using Jenkins’s job. I will create a directory “abc” inside the /home/admin.
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.
[[email protected] abc]$ git init [[email protected] abc]$ git remote add origin https://github.com/Aryango/Technical.git
We can check the remote repository added in this directory using the below commands.
[[email protected] abc]$ git remote -v origin https://github.com/Aryango/Technical.git (fetch) origin https://github.com/Aryango/Technical.git (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.
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.
[[email protected] 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.
The output of the build:
Now check on the server in /home/admin/abc directory. you will see the file valid1 in the “failure” branch.
Note: if you get any error like ” error: cannot open .git/FETCH_HEAD: permission denied” issue when you run the job using 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.
To fix it, you have to make admin to the owner of the git project on the server using the below commands.
[[email protected] ~]# chown -R admin:admin /home/admin/abc