Go to Previous Step or Go to Next Step
Now that we're done with the initial setup, let's create a multibranch pipeline. Click on New Item:
Add a name to this pipeline, select Multibranch Pipeline, then click OK:
Now it's time to set up the SSH credentials so Jenkins can pull the repository from GitHub.
Jenkins must have credentials before it's able to pull data from private GitHub repositories. For public repositories, all you need is the HTTPS clone path. For example:
https://github.com/adamiao/jenkins-docker.gitSince you may be using private repositories, we'll set up SSH credentials in this tutorial. Once the pair of SSH keys is shared by the container and the repository, Jenkins will have permission to pull the project and run the build/test/deploy processes.
The first thing we need to do is create a .ssh folder anywhere on the host machine. Next we'll add a file within this folder, called config (no extension), which has the information below:
Host github.com
Hostname github.com
User git
IdentityFile /var/jenkins_home/.ssh/id_rsa
IdentitiesOnly yesThe config file contains configuration parameters for the SSH connection such as the location of the SSH keys. This file can have multiple blocks in them, one for each Host. But here we'll only be using one. Notice that the IdentityFile points to a location that doesn't exist in the host machine. However, this will exist in the container once we move .ssh there.
Next we create the SSH keys by running the following command:
ssh-keygen -t rsa -b 4096When setting up the key, make sure you save it within the .ssh folder just created. This is very important! After all is said and done, the .ssh folder will contain the following files:
Next we must copy this folder and all of its contents to the container. We do that by copying it to the volume location in the host machine. Remember that you can run docker volume inspect <volume_name> to find the location of the volume within the host machine. The copy command will look something like this:
cp -r .ssh /var/lib/docker/volumes/jenkins_home/_dataFor the last step, we have to copy the public key (id_rsa.pub) and place it in GitHub. This will be the key that will check the credentials used by Jenkins. To do that we'll go to the Settings tab of the repository, then on the left pane go to Deploy keys, and finally click on Add deploy key:
You may or may not want to give write access to the key. This is up to you and your use case. For this tutorial the key will not have write access. Once this process is done you will see the following:
Going back to the pipeline within Jenkins we see the following screen:
Under the section Branch Sources, add the source Git:
Next, get the SSH clone path on your GitHub repository:
Use this path for the Project Repository value. Next let's add a credential to this pipeline:
Make sure that under Kind you have SSH Username with private key selected. Other than that, you only really need to add a Username to this credential since the SSH keys are in a default location within the container for GitHub to find.
Finally, make sure that the credential just created is selected:
Once this last step is done, click the Save button. Once saved, the pipeline should start a first automatic run. The Scan Multibranch Pipeline Log page looks like below:
If it does not start, press Scan Multibranch Pipeline Now, on the left pane.
The pipeline that just ran was orchestrated by Jenkinsfile. This is a text file that defines the CI/CD pipeline logic for a project with steps to build/test/deploy etc. [2]
Because the Jenkinsfile can group commands together in stages, you are able to see the logs for each one of them. To see them, first go to the pipeline of interest and then click on the branch you're testing on. Here we are testing the main branch:
Notice how you are able to see the results of each step:
Next we'll set up a way for Jenkins to trigger the pipeline once changes are made in the GitHub repository.
Go to Previous Step or Go to Next Step