UPES-CSDV3001

CI/CD with Jenkins

You can choose to install Jenkins either using AWS, on an EC2 instance (recommended), or on your local machine.

Method 1: Install Jenkins Server on EC2

Jenkins is typically run as a standalone application in its own process with the built-in Java servlet container/application.

  1. Create a *.medium, Ubuntu EC2 instance with 30GB disk.
  2. Connect to your instance, install java by
sudo apt update
sudo apt install openjdk-11-jre
  1. Download and install Jenkins as described here.
  2. On Jenkins machine, install Docker engine. You may want to add jenkins linux user the docker group, so Jenkins could run docker commands:
    sudo usermod -a -G docker jenkins
    sudo usermod -a -G docker $USER
    
  3. Install kubectl.
  4. Install Git if needed.
  5. You’ll need your Jenkins server to have static public ip address. From the EC2 navigation pane, create an Elastic IP and associate it to your Jenkins instance.
  6. Open port 8080 in your instance’s security group, and visit your Jenkins server via http://<static-ip>:8080 and complete the setup steps.

Method 2: Install Jenkins locally

Follow the official installation guide and choose the relevant OS installation.

Configure a GitHub webhook

Note: Throughout this module you will build, test and deploy an app called Roberta (can be found in our shared repo under roberta). This is a simple flask webserver that serves a sentiment analysis AI model based on roberta-base. The model was trained on a data from Reddit to analyze the sentiment of an English sentence.

A GitHub webhook is a mechanism that allows GitHub to notify a Jenkins server when changes occur in the repo. When a webhook is configured, GitHub will send a HTTP POST request to a specified URL whenever a specified event, such as a push to the repository, occurs.

  1. If you don’t have it yet, create a new GitHub repository and copy the files under the roberta directory to the root directory of your repo, push it.
  2. To set up a webhook from GitHub to the Jenkins server, on your GitHub repository page, go to Settings. From there, click Webhooks, then Add webhook.
  3. In the Payload URL field, type http://<jenkins-ip>:8080/github-webhook/ (if you run the Jenkins server locally, please read the note below). In the Content type select: application/json and leave the Secret field empty.
  4. Choose the following events to be sent in the webhook:
    1. Pushes
    2. Pull requests

Note for students running their Jenkins server locally

As your Jenkins server is being running locally, it is not accessible over the internet. This creates a communication challenge between GitHub and your Jenkins server, since GitHub has to notify Jenkins when events occur in the repo.

Ngrok can solve this problem by creating a secure tunnel between the local machine (where the Jenkins is running) and a public URL provided by Ngrok. It exposes the local server to the internet, allowing GitHub servers to reach the webhook URL and send updates to Jenkins.

Sign-up for the Ngrok service (or any another tunneling service to your choice), then install the ngrok agent as described here.

Authenticate your ngrok agent. You only have to do this once:

ngrok config add-authtoken <your-authtoken>

Since the Jenkins service is listening on port 8080, start ngrok by running the following command:

ngrok http 8080

Your Jenkins public URL is the URL specified in the Forwarding line (e.g. https://16ae-2a06-c701-4501-3a00-ecce-30e9-3e61-3069.ngrok-free.app).

Managing Jenkins

Most standard administrative tasks can be performed from the screens in the Manage Jenkins section of the dashboard.

Let’s take a look on some of them:

More information can be found here.

Update plugins

Jenkins plugin updates are essential for maintaining compatibility, security, and functionality, as they ensure that the latest features and bug fixes are incorporated into the Jenkins ecosystem.

As your Jenkins server will be used not only during this module, but actually up to the end of course, you are expected to maintenance your server (as if it is functioning for a real project). Update your plugins when needed.

The below screenshot (taken from the top right bar in the main Jenkins dashboard) warns the user regarding available plugin update.

You can also see and perform updates in Manage Jenkins > Manage Plugins.

The jenkins user and its home directory

When installed Jenkins, a jenkins linux user was created during the installation process and is used to execute Jenkins processes and manage its files. Jenkins stores its global configuration in files on the jenkins home directory. On Ubuntu by default, this is set to /var/lib/jenkins.

Jenkins uses this directory to store configurations, to perform builds and keep archives.

Self-check questions

Enter the interactive self-check page

Exercises

:pencil2: Install Jenkins plugins

The Jenkins community plugins are extensions that enhance the functionality of the Jenkins automation server.

In the Dashboard page, choose Manage Jenkins, then Manage Plugins. In the Available tab, search and install:

Then restart jenkins by http://<ip>:8080/safeRestart

:pencil2: Custom log recorder

Create log recorder that track only INFO logs related to GitHub webhook.

:pencil2: Run Jenkins using Docker

Launch the Jenkins server in a docker container.

You may utilize the Jenkins docker official docs

:pencil2: Configure a GitHub webhook with token

  1. In Jenkins, create a text credentials.
  2. In Manage Jenkins, Configure System, configure the GitHub server to use the created credentials.
  3. Configure the token in GitHub webhook page.