Basic DevOps CI/CD Pipeline #2
- #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
-
Access Jenkins GUI:
- Navigate to your Jenkins instance in a web browser.
-
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.
![]()
Creating the Jenkins Pipeline
We’ll create a Jenkins pipeline to check out the source code from GitHub.
-
Create a New Pipeline:
- In Jenkins, create a new pipeline job.
-
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.gitwith 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
-
Create a DockerHub Account:
- Sign up at hub.docker.com.
-
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:
-
Install Required Plugins:
- Ensure the following plugins are installed:
- Pipeline
- Docker
- Docker Pipeline
Install them via Manage Jenkins > Manage Plugins > Available tab.
- Ensure the following plugins are installed:
-
Define Build and Push Stages:
-
Update your
Jenkinsfilewith 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
yourdockerhubuserwith your DockerHub username.-
Build Stage:
- Builds the Docker image with the tag
yourdockerhubuser/javaapp.
- Builds the Docker image with the tag
-
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!