How to Add Git Credentials in Jenkins

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how to add git credentials in jenkins [Music] if you are working with git repositories that require authentication that means you're going to have to set up credentials within jenkins but which type of credentials do you need to set up that's what we're going to talk about in this video here's today's starting point i have a jenkins lts controller it's version 2.303.3 and attached to this controller i have a linux based agent and for the example today i am using a private repository on github you may or may not be using github but the process that we're following today is very similar on both bitbucket and gitlab so first off let's go ahead and create a job pointed at our private repository and we go to the private repository and first off i'm going to select https and we're going to use the https version in the first part of the video and then we're going to be doing ssh in the second part so copy https let's go back over to our controller click on new item and we'll call this https pipeline click ok let's change this from pipeline script to pipeline script from scm scm to git and now let's paste in our repository url and tab and when we tab we get an error message saying support for password authentication was removed this is a github specific item but bottom line is this very last line authentication failed for https github and the rest of my url and by the way the url for that repository is down in the description of this video so what do we need to do well we know we need to create a credential in order to authenticate with the github repository and again if you're using gitlab or if you're using bitbucket or some other type of git repository the process is going to be similar but obviously the steps are going to be very specific to each of those providers so in the case of github what we're going to do for https is we need to set up a personal access token and the reason why we know that is support for password authentication was removed so that's all gone and they even tell me here please use a personal access token instead so how do we do that well let's go back over to github we'll go up to my id and go to settings in the case of a personal access token we're going to click on developer settings and then personal access tokens and what we're going to do is we're going to create a new token i'm going to give the note or the name in my case is going to be jenkins example private pat it doesn't really matter what it is now i'm also going to go ahead and set an expiration date for 30 days you can choose to have no expiration or a custom but they give also give you 7 30 60 and 90. so if you do set an expiration and you need to refresh that token you'll just need to remember to come back and do that at the time before it expires now what are the different scopes that we need to select for this well in order to access a private repository we need to select the repo scope which gives you the full control of private repositories now this could be up to you maybe you go ahead and decide i don't want all of these but in my case i'm just going to select the full repo i'm also going to go ahead and select two other items first one is user email and the second one is going to be i'm trying to remember what it is yes it is read org i'm just going to pick read org there is a knowledge base article on the cloudbee support site that has this information i'll be sure to include that in the readme in the repository so we have a read org user email and then the top level repo let's go ahead and click on generate token and i'm going to copy my token so copy that there and i am going to paste it off on the side because i'm going to need this again in a few moments okay so i have that set up let's go back over to our job and just right here we're going to add a credential jenkins and for this we are going to be using the username and password type credential so in my case my username is my personal email address i'm going to treat username as secret as well i'm going to copy my token and for the id i'm going to call it jenkins example github pat and the same for the description so what i have here is my username for whatever my platform is in this case it's my email address and for specifically github in order to use the password it's actually a personal access token but it goes into the password field let's go ahead and click on add and let's select this credential that we just created jenkins example github p-a-t and watch what happens it re-authenticated and all of those messages went away so now we know that we're authenticated with our repository let's go ahead and finish this up we have a jenkins file that's in that repository that's just a hello world so let's go ahead and click on build now just to prove out that we're actually connecting to that repository so we take a look at the run we can see here that it's cloning the repository so that tells me that my authentication is good and we also see echo hello world so everything worked as expected for https so for the first job we did https in the second job let's use ssh so we'll go back over to our repository click on code click on ssh and we're going to copy this url let's go back over to our controller new item we'll call this one ssh pipeline click ok same thing here pipeline script from sem change it to git and paste in the ssh repository url and here we can see that we're also getting a permission denied for public key we're unable to read from the remote repository perfect that's exactly what we want so what we need to do now is set up an ssh key pair to be able to authenticate from our jenkins controller back into github so let's go back over to github go back to my profile we're going to go to settings but instead of going to developer settings on the left nav we're going to go to ssh and gpg keys and what we're going to do is we're going to create a new ssh key but how do we do that well fortunately right down here they give us the link to how to generate an ssh key and if we take a look at this generating a new ssh key and adding it to ssh agent we don't really care about doing it to the agent at this point we just want to generate the key and what we have here is it gives us a copy and paste for ssh key gen t and we're going to use this specific algorithm and we're going to give it the email address so in my case what we're going to do i'm going to open up a shell and i'm on mac os so what i'm going to run here is ssh keygen dash t ed25519 and then dash c with my email address and it says enter file in which to save the key i'm just going to let it go to the default there i'm going to add no passphrase and leave the passphrase out you could add a passphrase if you want and we're all good so now what do we need to do well let's go back over to github and we're going to add a new ssh key in this case my title is going to be jenkins example get ssh and let's get that off the front and then my key is going to be my public key well if we go back over to the shell here is my public key so let's just cap that out there's that so let's go ahead and copy it and let's paste that in to here so we're adding our ssh key and now we can see that our jenkins example get ssh was added on this date never used read write all of that's good so now that we have created our ssh key over on github at least the public side now what we need to do over on the jenkins side is to create a new credential and specifically the type of credential that we need to create is ssh username with private key so in my case here my id is going to be just what i named it over on the other side that way i can keep track of okay which ones map from jenkins over in my case to github my username is going to be again the id that i put inside of my credential i'm going to treat it like a secret as well and then for my private key i'm going to enter it directly i'm going to click on add now let me go and grab my private key which now looks like this so we'll go ahead and copy that we'll head back over here and paste that in and if i had added in a passphrase i would go ahead and type in this passphrase but since i didn't add a passphrase nothing to add there let's click on add and then let's change our credential to git ssh and again much like what we saw with the https version the authentication errors went away as we selected that credential because what happened under the hood is it was reauthenticating and testing that credential to make sure i'm able to actually authenticate against my git repository we'll do the same thing again main jenkins file we'll click on save and click on build now and once the job completes what we can see here is that we are cloning the repository based on ssh does all the steps as we expect and then we see hello world which credential type should you pick when integrating with git the answer is it depends for the examples that we had in this video either will work fine however if instead of using a pipeline job you're using multi-branch or using an organization type job then the only real option will be to use user name and password type credentials why is that if you think about it for a moment what's happening is there are plugins that are integrating with your scm providers and most of the time those plugins are doing more than just doing a git clone or a get fetch they're actually making api calls to those providers and since api calls will not support ssh private key type credentials and really only username password or token based then that's why you have to select a username password type credential in jenkins if you have any questions or comments you can reach out to us on twitter at cloudbees if this video was helpful to you give us a thumbs up and if you haven't subscribed to cloudbeast tv yet why not take a moment click on that subscribe button and then ring that bell and you'll be notified anytime there's new content available on cloudbees tv thanks for watching and we will see you in the next video you
Info
Channel: CloudBeesTV
Views: 480
Rating: undefined out of 5
Keywords: darin pope, jenkins, jenkins tutorial, git credentials jenkins pipeline, jenkins git credentials not working, jenkins create credentials, jenkins credentials ssh username with private key, add ssh credentials to jenkins, jenkins add credentials, jenkins credentials, how to create new credentials in jenkins, how to create credentials in jenkins, how to add credentials in jenkins for git, how to add github credentials to jenkins, how to add git credentials in jenkins
Id: HSA_mZoADSw
Channel Id: undefined
Length: 12min 13sec (733 seconds)
Published: Tue Nov 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.