Skip to main content

Development

Using GIT deploy key in Jenkins – Written By Tom Tang

This post is an introduction for who want to use Jenkins manage multiple project, using GIT as version control.

Of course! We can use SSH-KEY mapping GIT server and CI server together. But that means we must use same SSH-KEY in different projects and some guys can run command “commit” and “push “commit code to GIT repository through CI server.   To avoid this, GIT give out a solution for this scenario: Deploy Key. We can create deploy key for each project and add those keys to GIT server. Those deploy keys only can run command “clone” and “pull”.  That’s enough for CI environment such as “Jenkins “.

Example: How to use GIT deploy key to fetch code from GIT repository.

Step1: Create a freely Jenkins Job

Step2: Select GIT and input repository URL

You may get an error here, but don’t worry we will resolve it later, click save.

Step3: Go to Jenkins Home

03

Step4: Generate SSH Key:

sudo -u jenkins ssh-keygen -t rsa -f { jenkins_HOME }/.ssh/id_rsa.{projectName} -C “{Comments}”.

PS: The bold part means it runs follow command as user “Jenkins”. Otherwise we may get some permission error.

04

Step5: Add key to GIT Project as deploy key

05

Open public key “vi id_rsa.projectA.pub”.

052

Copy and add it to GIT project as deploy key:

Here, you should be a master of this project.

053

Step6: Map deploy key and local private key

Go to {jenkins_HOME}, create config file and add following code.

             Host {git_home}-{project_name}

             Hostname {git_home}

             User git

             IdentityFile { jenkins_home}/.ssh/id_rsa. {project_name}

Example:

            Host gdcgit-Enable2

            Hostname gdcgit

            User git

            IdentityFile /var/lib/jenkins/.ssh/id_rsa. Enable2

Step7: Update repository URL

06

Update repository URL from git@gdcgit:philip.wang/enable2.git to git@gdcgit-Enable2:philip.wang/enable2.git.

The repository error would be resolved.

Build job, we can use deploy key get code from GIT repository now.

07

 

Tags

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us