ocicreate-ci-pipeline-jenkins-oracle

Bitnami How-To Guides for Oracle Cloud Infrastructure

Create a Continuous Integration Pipeline with Jenkins and GitHub on Oracle Jump Start

Introduction

If your job involves developing or deploying software, you’ve probably already heard of Jenkins, a Continuous Integration (CI) server designed specifically for automating software builds and deployments. As a leading open source project, Jenkins is extremely versatile and comes with 1000+ plugins that allow easy integration with many different platforms, source code management systems and build systems.

Oracle Cloud Jump Start makes it easy to get started with pre-configured solutions running on Oracle Cloud infrastructure, for free. The Jenkins Demo Lab in Oracle Cloud Jump Start is a one-click solution that lets you deploy a Jenkins instance on Oracle Cloud infrastructure, then use it to automate builds and deployments for your software projects.

With this guide, you will learn how to create a Continuous Integration (CI) pipeline for a GitHub project using the Jenkins Demo Lab on Oracle Cloud Jump Start. Once you’ve completed the steps in this guide, any changes made to your GitHub project code will be automatically and instantly built and tested by Jenkins, with appropriate success/failure indications as needed. By following the steps in this guide, you will gain a hands-on understanding of how Jenkins works and how you can use it to automate your software testing and delivery process.

Assumptions and Pre-requisites

This guide makes the following assumptions:

Step 1: Log in to Jenkins

To begin, navigate to the Jenkins application at the IP address in your Demo Lab confirmation email. You should be prompted to log in, as shown below:

Jenkins login

Log in with the access credentials specified in your Demo Lab confirmation email or on the Demo Lab launch page. Since this is the first time you’re logging in, you will be prompted to install a recommended set of additional plugins and restart Jenkins:

Plugin installation

These plugins are not required to complete the steps in this guide, but may be useful for other projects, so you should go ahead and install them. If you decide to install all suggested plugins, you will be prompted to confirm the Jenkins URL. Then, click “Save and Finish”. On the resulting screen, click “Restart” to finish the Jenkins setup. Refresh the page to avoid unexpected issues during the loading process.

Once the installation process is complete and Jenkins has restarted (or if you have chosen to skip the installation), log in again if needed and you will arrive at the Jenkins dashboard, which looks like this:

Jenkins dashboard

The Jenkins dashboard is your primary control point for interacting with Jenkins. It includes controls for creating and running builds, inspecting build output, working with credentials and configuring Jenkins. You’ll notice that at the moment, there are no projects or queued builds. You’ll change that in the next step.

Step 2: Create and build a simple project

Jenkins makes it easy to automatically build, test and deploy software. It does this by allowing you to define “projects” and then specifying “steps” to build, test and deploy them. It also lets you automatically trigger builds on specific events (such as a repository change) or on a fixed schedule, and perform predefined actions (such as sending an email notification) on build success or failure.

To see how this works in practice, begin by clicking the “New Item” link in the Jenkins dashboard. You should be presented with a form to define a new project, as shown below. Enter a name for the new project (in this example, “myapp”) and select “Freestyle project” as the project type. Click “OK” to proceed.

Project definition

You should now see the project configuration page, which allows you to define various aspects of the build. For the moment, leave everything at default values and head for the “Build” section. Click the “Add build step” button and add a step to “Execute shell”.

Shell execution build step

In the resulting “Command” box, enter a shell command that displays output, like the one below, and click “Save” to save the changes:

echo "Hello from Jenkins!"

Shell execution command

Head back to the project page and click the “Build now” link.

Project summary page

Jenkins should begin building the project. The “Build History” panel in the bottom left corner of the page will show you the status of the build (blue indicates success, red indicates failure).

project build status

Once the build is complete, select the “Console Output” menu item to access a detailed build log. The build log should display the output of the shell command you specified earlier.

project build log

Congratulations! You just completed your first successful build with Jenkins.

Step 3: Build a GitHub project

Now that you know the basics of using Jenkins, let’s look at a more “real world” example: building a project stored in an external SCM, such as GitHub. You’ll use the simple Java + Maven application included with the Jenkins user documentation for this.

  • Begin by logging into GitHub with your account and browsing to the repository for the example Java + Maven application. Click the “Fork” button to create your own personal fork of the repository.

    Fork creation

  • Note the GitHub clone URL for your fork of the repository. Make sure that you are using the “Clone with HTTPS” option.

    GitHub clone URL for fork

NOTE: This guide uses a simple Java project hosted on GitHub as an example. However, you can follow the steps described here for any public GitHub project, remembering to ensure that all the necessary build tools are correctly configured in Jenkins.

  • Click the Jenkins logo to access the dashboard. Select the project you created in Step 2 and click the “Configure” button for the project.

  • On the project configuration page, in the “Source Code Management” section, select “Git” as the option and enter the clone URL for your GitHub repository.

SCM configuration

The example project requires Maven and so, before you can build it, you must install Maven in Jenkins.

  • Click the Jenkins logo to return to the Jenkins dashboard, then click the “Manage Jenkins -> Global Tool Configuration” option.

    Jenkins tool configuration

  • On the “Global Tool Configuration” page, find the “Maven” section and click the “Add Maven” button. Enter a name for the new Maven installation (here, simply “Maven”) and ensure that the “Install automatically” button is ticked. Click “Save” to save the changes and install Maven.

    Maven installation

Once Maven is installed, you can proceed to configure Jenkins to download and build the example application.

  • Return to the project configuration page, and in the “Build” section, remove the “Execute shell” build step created in Step 2, by clicking the red “X” in the top right corner of the section.

Previous build step removal

  • Add a new build step to “Invoke top-level Maven targets”.

    • In the “Maven Version” field, select the name of the Maven installation you just created.
    • In the “Goals” field, specify the Maven goals as “clean package”.

    Maven build goals

  • Click “Save” to save the changes.

Head back to the project page and click the “Build now” link. Jenkins should clone your GitHub repository and build it with Maven. Once the build is complete, select the “Console Output” menu item (as done previously in Step 2) to access detailed build information.

Project build log

Step 4: Create a Continuous Integration pipeline for a GitHub project

The next step is to create a Continuous Integration (CI) pipeline that connects your GitHub repository with Jenkins. Once this is in place, any changes to your GitHub repository will trigger a new build in Jenkins. Any errors resulting from the changes will result in a build failure, giving you an immediate heads-up on potential issues or bugs.

  • Navigate to your GitHub repository and click on the “Settings” tab.
  • From the left side repository, click “Webhooks”. On the resulting screen, click the “Add webhook” button.
  • On the Webhook page, enter the following information:

    • Payload URL: enter the URL to your Jenkins installation with the additional /github-webhook/ suffix. For example, if your Jenkins installation is available at http://SERVER-IP/jenkins, enter the Jenkins hook URL as http://SERVER-IP/jenkins/github-webhook/.
    • Content type: select the “application/json” option.
    • Which events would you like to trigger this webhook?: select the “Just the push event”.

    Add GitHub webhook

  • Click the “Add webhook” button to create the new webhook.

    NOTE: It may take a few minutes for the integration to begin working.

To set up this pipeline, you need to enable the GitHub hook trigger in Jenkins for your project. Then, you will configure GitHub to automatically notify Jenkins on any change to the repository.

  • From the Jenkins dashboard, select the project you created in Step 3. It redirects you to the project configuration page.
  • From the left side menu, click the “Configure” option. On the next screen, scroll down until the “Build Triggers” section. Enable the “Github hook trigger for GITScm polling” option and save the changes.

    GitHub hook activation in Jenkins

Your pipeline is now configured, and all that remains is to test it. To do this, make a change to the repository - for example, by adding a line to the README file and committing the change.

Minor GitHub commit to trigger pipeline

Your change should trigger a new build in Jenkins, as shown below:

Triggered build in Jenkins

Clicking the build number will show you the details of the commit that triggered the build:

Triggered build summary

Congratulations! Your CI pipeline is now complete, and any subsequent changes to the repository will be automatically built by Jenkins.

After completing the previous steps, you should now have a good understanding of how Jenkins works, how to configure a project in Jenkins, and how to integrate Jenkins with GitHub. You should also have a working CI pipeline between your GitHub code repository and your Jenkins instance running on Oracle Jump Start.

What you’ve seen so far is just the tip of the iceberg. Jenkins is an extremely versatile application and you can adapt it to almost any use case or build/test requirement. Learn more about Jenkins and the other topics discussed in this guide by visiting the following links:

Last modification September 26, 2018