Skip to content

Latest commit

 

History

History
106 lines (66 loc) · 6.84 KB

File metadata and controls

106 lines (66 loc) · 6.84 KB

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:

Create new item

Add a name to this pipeline, select Multibranch Pipeline, then click OK:

Multibranch Pipeline

Now it's time to set up the SSH credentials so Jenkins can pull the repository from GitHub.

Setup SSH Credentials

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.git

Since 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 yes

The 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 4096

When 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:

.ssh folder contents

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/_data

For 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:

step_3_deploy_key_0

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:

step_3_deploy_key_1

Multibranch Pipeline

Going back to the pipeline within Jenkins we see the following screen:

Pipeline Interface

Under the section Branch Sources, add the source Git:

Branch Source - Git

Next, get the SSH clone path on your GitHub repository:

GitHub SSH Path

Use this path for the Project Repository value. Next let's add a credential to this pipeline:

Create new item

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.

Create new item

Finally, make sure that the credential just created is selected:

Create new item

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:

Scan Pipeline

If it does not start, press Scan Multibranch Pipeline Now, on the left pane.

Jenkinsfile

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:

Go to Main Branch

Notice how you are able to see the results of each step:

Run Results

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

References

  1. Pipeline: Multibranch
  2. Pipeline-as-code with Multibranch Workflows in Jenkins