Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- By Using SSH Username with private key
- For this, first you have to generate your own SSH RSA keys. It is not mandatory to generate SSH RSA keys in Jenkins Server only (but you can). We will recommend you to generate the SSH RSA keys in your laptop or any secured system of your organisation.
- a. Create directory for keeping SSH RSA Keys file and change to newly created directory.
- mkdir -p ~/ssh-rsa-keys && cd ~/ssh-rsa-keys
- b. Create SSH RSA Keys in pem file format inside newly created directory.
- ssh-keygen -t rsa -b 4096 -C "git ssh keys" -f gitcreds.pem
- Give passphrase when it is asked.
- c. On completion of command, it will create two files – private key and public ky. The one which has .pub file extension refer to public key file whereas other is private key file.
- whereas other is private key file.
- ou have to further follow two mandatory steps:
- A. Set private key in Github or any other Git provider
- B. Set SSH private key in Jenkins
- A. Set private key in Github or any other Git provider
- Basically we have to set the Public key in Git Server or Git provider (Github,Bitbucket,Gitlab etc.)
- Because Github is popular and mostlt use by many people, we are demonstrating how to set SSH RSA Public key for git access.
- a. Login to Github account.
- b. Click on ‘Settings’ of your Github profile.
- c. Click on ‘SSH and GPG keys’ then click on ‘New SSH Key’. It will first ask you to give your Gtihub password and after authentication will show the ssh key setting page.
- How to setup Jenkins Credentials for Git repo access
- December 16, 2018 by Sharad Chhetri Leave a Comment
- It is crucial and mandatory step that you have to setup Git Credentials for accessing Git repo calling from Jenkins Jobs. The Jenkins Credentials can be called by its variable also. This article will help you to setup Jenkins Credentials for Git repo access. It is basic of Jenkins and must read article.
- In our previous post on Managing Jenkins Credential we have already written about types of “Credentials” and “Scope” (We strongly recommend you to read our previous post before you go for Git Jenkins Credential setup).
- To illustrate the Jenkins Credential for Git, we will do some work in our Gihub account too. We have decided to select Gthub due to its popularity. The method which we will tell you in this post is generic. You can apply this method with any Git service provider(Gitlab,Bitbucket,Github etc.) or your own self hosted Git.
- 3 Generic Methods To Setup Jenkins Credential
- You can access the Git repo in many ways (Note this point), it all depends upon what authentication method you have enabled in your Git server.
- We have selected 3 Generic methods to configure Jenkins Credential for Git. These 3 methods are commonly used for accessing Git.
- By using username and password
- By using SSH username and its keys
- By using Token
- Jenkins Credential Page
- Before we start to set Git Credential in Jenkins we would like to suggest to read the post on Managing Jenkins Credentials. It is mendatory to know about the terms which will be used while setting ‘Jenkins Credentials’ .In this post we have shown pictures depicting the steps to reach ‘Jenkins Credential’ page. In our previous post, we have also described about the types of “Scope”.
- Go to ‘Jenkins Credential’ page, here is the navigation for adding credential –
- Home Page >> Credentials (Tab) >> Credential Store (Select) >> Add Credentials (Tab)
- We have described about 3 methods to set git credentials in Jenkins. Select the method as per your requirement and type of credentials you have in your Git Server or Git provider like Github, Gitlab etc.
- Method 1: By Using Username And Password
- After clicking on ‘Add Credential’ at right hand sidebar you will see the form to fill. It is pretty much self explanatory.
- jenkins credentials
- 1. Click on dropdown button labelled as ‘Kind’. Select the option ‘Username with password’.
- 2. Click on dropdown button labelled as ‘Scope’. Select either ‘Global’ or ‘System’.
- 3. In Username text box give git username.
- 4. In Password text box give git user’s password.
- 5. In ID text box write jenkins ID credential. You can write as per your wish.
- 6. In Description text box write description of this credential. For example – “My Github credentials”.
- Method 2: By Using SSH Username with private key
- For this, first you have to generate your own SSH RSA keys. It is not mandatory to generate SSH RSA keys in Jenkins Server only (but you can). We will recommend you to generate the SSH RSA keys in your laptop or any secured system of your organisation.
- a. Create directory for keeping SSH RSA Keys file and change to newly created directory.
- mkdir -p ~/ssh-rsa-keys && cd ~/ssh-rsa-keys
- b. Create SSH RSA Keys in pem file format inside newly created directory.
- ssh-keygen -t rsa -b 4096 -C "git ssh keys" -f gitcreds.pem
- Give passphrase when it is asked.
- c. On completion of command, it will create two files – private key and public key. The one which has .pub file extension refer to public key file whereas other is private key file.
- git ssh rsa key
- In above screenshot, gitcreds.pem (private key) and gitcreds.pem.pub (public key) two files are created.
- You have to further follow two mandatory steps:
- A. Set private key in Github or any other Git provider
- B. Set SSH private key in Jenkins
- A. Set private key in Github or any other Git provider
- Basically we have to set the Public key in Git Server or Git provider (Github,Bitbucket,Gitlab etc.)
- Because Github is popular and mostlt use by many people, we are demonstrating how to set SSH RSA Public key for git access.
- a. Login to Github account.
- b. Click on ‘Settings’ of your Github profile.
- github jenkins
- c. Click on ‘SSH and GPG keys’ then click on ‘New SSH Key’. It will first ask you to give your Gtihub password and after authentication will show the ssh key setting page.
- jenkins github
- github jenkins
- d. Give the ‘Title’ of your SSH key in Github.
- Copy the content of Public Key which you have generated in previous steps and paste the content in ‘Key’ text field as shown in screenshot. In final step click to ‘Add SSH Key’ button.
- cat ~/ssh-rsa-keys/gitcreds.pem.pub
- e. The Public SSH key will be added in your Github account.
- B. Set SSH private key in Jenkins
- a. Click on ‘Add Credential’, at right hand sidebar you will get the form.
- b. Click on ‘Kind’ and select option ‘SSH Username with private key’
- c. Click on ‘Scope’ and either select ‘Global’ or ‘Standard’ as per your requirement.
- d. In ‘Username’, give authorised GIT username.
- e. In ‘Private Key’ text field, you have to paste the contents of SSH RSA private key which you have generated in previous section.
- Use cat command to get the contents of SSH RSA private key. Copy the content and paste in ‘Private Key’ text field (see in screenshot)
- cat ~/ssh-rsa-keys/gitcreds.pemf. In ‘Passphrase’ text field, write passphrase which you have given while generating the SSH RSA key. If not set, keep it blank.
- g. In ‘ID’ text field, write any ID name for this credential.
- h. In ‘Description’ box, give description.
- i. Click ‘OK’ button to finish.
- Now you can access git repo by using GIT Jenkins credential which has ssh keys information from Jobs/pipeline.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement