ECHOKE
Guides

Basic DevOps CI/CD Pipeline #2

Engin Can Höke
#devops#ci/cd#jenkins#docker#github

Before reading this blog, check out the first part of the series.

In this part, we’ll integrate a private GitHub repository with Jenkins by adding credentials and automate the process of building and pushing Docker images.

Adding GitHub Credentials to Jenkins

  1. Access Jenkins GUI:

    • Navigate to your Jenkins instance in a web browser.
  2. Add Credentials:

    • Go to Manage Jenkins > Credentials > (Global) > Add Credentials.
    • Select “Username with password” as the kind.
    • Fill in your GitHub username and password.
    • Assign an ID to these credentials, e.g., github-creds.
    • Click “OK” to save.

    You should now see the credentials listed.

SS-1

Creating the Jenkins Pipeline

We’ll create a Jenkins pipeline to check out the source code from GitHub.

  1. Create a New Pipeline:

    • In Jenkins, create a new pipeline job.
  2. Define the Pipeline Script:

    • Use the following script to check out the code:

      pipeline {
          agent any
          stages {
              stage('Checkout') {
                  steps {
                      git(branch: 'master', credentialsId: 'github-creds', url: 'https://github.com/github_user/private-repository.git')
                  }
              }
          }
      }
      

    Replace github_user/private-repository.git with your repository’s URL.

Preparing the Application

We’ll use a basic Maven Spring Boot web application project. You can create one at start.spring.io, selecting version 2.4.1 and Java 8.

Adding a Dockerfile

Create a Dockerfile in your project with the following content:

FROM openjdk:8-jdk-alpine
COPY . /data
WORKDIR /data
RUN ["mvn", "clean", "install"]
COPY target/*.jar ./app.jar
ENTRYPOINT ["java", "-Xmx750m", "-jar", "app.jar"]

Setting Up DockerHub

  1. Create a DockerHub Account:

  2. Add DockerHub Credentials to Jenkins:

    • In Jenkins, go to Manage Jenkins > Credentials > (Global) > Add Credentials.
    • Select “Username with password” as the kind.
    • Enter your DockerHub username and password.
    • Assign an ID, e.g., dockerhub-creds.
    • Click “OK” to save.

Docker Operations in Jenkinsfile

We’ll use Jenkins plugins to interact with Docker:

  1. Install Required Plugins:

    • Ensure the following plugins are installed:
      • Pipeline
      • Docker
      • Docker Pipeline

    Install them via Manage Jenkins > Manage Plugins > Available tab.

  2. Define Build and Push Stages:

    • Update your Jenkinsfile with the following stages:

      pipeline {
          agent any
          stages {
              stage('Checkout') {
                  steps {
                      git(branch: 'master', credentialsId: 'github-creds', url: 'https://github.com/github_user/private-repository.git')
                  }
              }
              stage('Build') {
                  steps {
                      script {
                          newImage = docker.build 'yourdockerhubuser/javaapp'
                      }
                  }
              }
              stage('Push') {
                  steps {
                      script {
                          docker.withRegistry('', 'dockerhub-creds') {
                              newImage.push("${env.BUILD_NUMBER}")
                          }
                      }
                  }
              }
          }
      }
      

    Replace yourdockerhubuser with your DockerHub username.

    • Build Stage:

      • Builds the Docker image with the tag yourdockerhubuser/javaapp.
    • Push Stage:

      • Logs in to DockerHub using the provided credentials.
      • Tags the image with the current Jenkins build number.
      • Pushes the image to DockerHub.

Next Steps

In the next episode, we will continue by preparing a deployment Ansible playbook, triggering the playbook within Jenkins, and setting up the monitoring baseline.

Stay tuned!

← Blog'a Dön