- [Instructor] Now that we've got a good set up
for building our builder and building our product.
Let's go ahead and automate this process
and have Jenkins and GitHub do the work for us.
Let's start off by making ourselves a new GitHub project
to store our code.
I'll call it, example-webapp.
And back here in the terminal,
we're going to go ahead
and add that Git registry to our project.
So, I'm going to begin by running git init.
Just to get things started.
Okay, now we have a Git repo.
Now I'm going to run git add . to add all the files
to our repo.
Okay.
Then, git
commit.
And I'm going to type -m just so I don't have
to open an editor and close it again.
Getting started for the commit message.
Okay, we added all the files.
Now, let's attach this to our GitHub project.
Git remote add origin, that's the traditional name to use.
Git@
github.com:,
your GitHub username here,
mine is thearthur/
example-webapp.
All right.
Then, git
push
origin
master.
All right.
And now that I've pushed to that,
let's go ahead and get things set up on Jenkins.
One thing we need to do,
is we need to actually install Git onto our Jenkins server
that we've built previously in the course
so that it will be able to pull from GitHub.
Let's SSH on into our Jenkins server
just like we did before.
Okay, here I am in our Jenkins server and I'm going to run,
sudo yum install git.
Yes.
All right.
And now we're done.
I'm going to disconnect from the Git server.
And back here on my computer.
We need to make a key,
an SSH shared public key and private key
that can be used to allow our Jenkins server access
to our project on GitHub.
So, to make that key, from my computer here
I'm going to run
SSH-
keygen.
Now that's SSH-keygen, all one word.
There isn't a space here like that.
Space
- f.
Let's call this,
jenkins-key, so that's the file this key will be put in.
Then we can go ahead and hit enter
and Just hit enter all the way through
to accept the defaults.
Okay.
So now,
we have two files in our directory.
Jenkins-key, that's the private one
and jenkins-key.pub, that's a public one.
We're going to go ahead and give the public one to GitHub.
And the private one to Jenkins.
So, let's say,
cat
jenkins-key.pub,
just to get it on to the screen.
Then I'm going to grab the entire thing with my mouse.
Make sure you get both S's at the SSH
and all the way to the end of the comment part at the end.
Copy.
Now, I'm going to head on back to the browser.
And we need to make ourselves a project
to build our product.
I'm going to go ahead and click new item.
And let's call it, example-
webapp.
Then, scrolling down slightly,
I'm going to use the project type,
multibranch pipeline and click OK.
So, the first thing we need to do,
is tell this project how to get the source code.
So, here under branch sources,
I'm going to click add source and choose Git.
Then I'm going to enter the Git address for our project.
Git@github.com, and this part is the same for everybody.
Then your username, this part will be different for you.
/example-webapp, whatever name you chose.
And for credentials, Jenkins needs permission
to actually check things out.
Right now, we don't have any credentials on our system.
So, I'm going to click add,
and I'm going to choose the Jenkins folder.
This is not the name of the credential,
this is just where in Jenkins you want to put.
If you choose Jenkins, it can be used for multiple projects.
If you choose example-webapp,
it can be used only for this project.
So, I'm going to go ahead and choose Jenkins.
Then, under kind, right here,
I'm going to choose SSH username with private key.
Most of these fields can be blank except the username,
needs to be git and exactly git for everybody.
Now, for private key, enter directly, add.
And now, we need to hop back over to the terminal.
Right here, we've been looking at the public key,
that's not what we want for this.
For this one, we want to cat jenkins-key without the .pub
and we get a much larger thing
that starts with begin private key.
Just to let you know, this is the part that grants access.
So, I'm going to copy all of that,
including the lines at the top and bottom,
and paste the whole thing in here.
Okay.
Now, we're going to go ahead and click add.
Then, under credentials,
now we see the new credential we just added.
Okay.
Now, let's go ahead and do the GitHub side of that.
I'm going to head over here to the GitHub project.
And under settings,
I'll click on deploy keys.
These are SSH keys that are allowed
to check out the source code.
And we're going to add a deploy Key for Jenkins.
Let's call it, jenkins.
You can enter anything you want there.
Now, for this part right here,
we want not the private key but we want the public key,
jenkins-key.pub.
And this is where we're going to select the whole thing.
Copy
and paste in the much shorter public key here.
For this project, we don't need to allow write access
since Jenkins is just going to check out the source code
and then deploy it somewhere else.
If you're doing fancier things,
you might need to check this box here.
Let's go ahead, add key, okay, there's our key.
Now, to finish things off,
we'll switch back over to Jenkins,
check over what we've got so far.
That looks right.
Let's go ahead and click save.
Next, Jenkins is going to connect to GitHub
and check out our project, find all the branches
and start building them.
In our case, we only have one.
So, it succeeded, it found the branch.
Now, let's go check it out.
I'm going to go over here and click on the project.
Then, and I'm going to click on master,
the only branch we have.
And here we see that Jenkins checked up the code
and ran several stages of the deployment pipeline.
First it started, got the source code.
Then it built the builder from our previous video.
Then it ran some unit tests on it
because, hey, our project has unit tests,
why not use them?
And then, built a production image
and uploaded it to our private registry.