Advertisement
Guest User

Untitled

a guest
May 19th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. By Using SSH Username with private key
  2. 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.
  3.  
  4. a. Create directory for keeping SSH RSA Keys file and change to newly created directory.
  5.  
  6. mkdir -p ~/ssh-rsa-keys && cd ~/ssh-rsa-keys
  7. b. Create SSH RSA Keys in pem file format inside newly created directory.
  8.  
  9. ssh-keygen -t rsa -b 4096 -C "git ssh keys" -f gitcreds.pem
  10. Give passphrase when it is asked.
  11. 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.
  12. whereas other is private key file.
  13. ou have to further follow two mandatory steps:
  14. A. Set private key in Github or any other Git provider
  15. B. Set SSH private key in Jenkins
  16.  
  17. A. Set private key in Github or any other Git provider
  18. Basically we have to set the Public key in Git Server or Git provider (Github,Bitbucket,Gitlab etc.)
  19. Because Github is popular and mostlt use by many people, we are demonstrating how to set SSH RSA Public key for git access.
  20.  
  21. a. Login to Github account.
  22. b. Click on ‘Settings’ of your Github profile.
  23. 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.
  24. How to setup Jenkins Credentials for Git repo access
  25. December 16, 2018 by Sharad Chhetri Leave a Comment
  26.  
  27. 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.
  28.  
  29. 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).
  30.  
  31. 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.
  32.  
  33. 3 Generic Methods To Setup Jenkins Credential
  34. 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.
  35.  
  36. We have selected 3 Generic methods to configure Jenkins Credential for Git. These 3 methods are commonly used for accessing Git.
  37.  
  38. By using username and password
  39. By using SSH username and its keys
  40. By using Token
  41. Jenkins Credential Page
  42. 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”.
  43.  
  44. Go to ‘Jenkins Credential’ page, here is the navigation for adding credential –
  45. Home Page >> Credentials (Tab) >> Credential Store (Select) >> Add Credentials (Tab)
  46.  
  47. 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.
  48.  
  49. Method 1: By Using Username And Password
  50. After clicking on ‘Add Credential’ at right hand sidebar you will see the form to fill. It is pretty much self explanatory.
  51.  
  52. jenkins credentials
  53.  
  54. 1. Click on dropdown button labelled as ‘Kind’. Select the option ‘Username with password’.
  55. 2. Click on dropdown button labelled as ‘Scope’. Select either ‘Global’ or ‘System’.
  56. 3. In Username text box give git username.
  57. 4. In Password text box give git user’s password.
  58. 5. In ID text box write jenkins ID credential. You can write as per your wish.
  59. 6. In Description text box write description of this credential. For example – “My Github credentials”.
  60.  
  61. Method 2: By Using SSH Username with private key
  62. 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.
  63.  
  64. a. Create directory for keeping SSH RSA Keys file and change to newly created directory.
  65.  
  66. mkdir -p ~/ssh-rsa-keys && cd ~/ssh-rsa-keys
  67. b. Create SSH RSA Keys in pem file format inside newly created directory.
  68.  
  69. ssh-keygen -t rsa -b 4096 -C "git ssh keys" -f gitcreds.pem
  70. Give passphrase when it is asked.
  71. 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.
  72.  
  73. git ssh rsa key
  74.  
  75. In above screenshot, gitcreds.pem (private key) and gitcreds.pem.pub (public key) two files are created.
  76.  
  77. You have to further follow two mandatory steps:
  78. A. Set private key in Github or any other Git provider
  79. B. Set SSH private key in Jenkins
  80.  
  81. A. Set private key in Github or any other Git provider
  82. Basically we have to set the Public key in Git Server or Git provider (Github,Bitbucket,Gitlab etc.)
  83. Because Github is popular and mostlt use by many people, we are demonstrating how to set SSH RSA Public key for git access.
  84.  
  85. a. Login to Github account.
  86. b. Click on ‘Settings’ of your Github profile.
  87. github jenkins
  88. 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.
  89.  
  90. jenkins github
  91.  
  92. github jenkins
  93.  
  94. d. Give the ‘Title’ of your SSH key in Github.
  95. 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.
  96.  
  97. cat ~/ssh-rsa-keys/gitcreds.pem.pub
  98. e. The Public SSH key will be added in your Github account.
  99. B. Set SSH private key in Jenkins
  100. a. Click on ‘Add Credential’, at right hand sidebar you will get the form.
  101. b. Click on ‘Kind’ and select option ‘SSH Username with private key’
  102. c. Click on ‘Scope’ and either select ‘Global’ or ‘Standard’ as per your requirement.
  103. d. In ‘Username’, give authorised GIT username.
  104. e. In ‘Private Key’ text field, you have to paste the contents of SSH RSA private key which you have generated in previous section.
  105.  
  106. 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)
  107.  
  108. 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.
  109.  
  110. g. In ‘ID’ text field, write any ID name for this credential.
  111. h. In ‘Description’ box, give description.
  112. i. Click ‘OK’ button to finish.
  113.  
  114. 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