Notes for Jenkins a CI/ CD Tool for DevOps
- One of the Approach of SDLC.
- That means instead of deploying your code at the end of day, week or anytime you want you will deploy it the moment you commit and push it in your remote repository.
- CI includes the build, test, and merge process of your project.
- That means making your source code ready to be released in production/ live environment. Also having a specific version of your project ready to be delivered in your client or to your co-workers or deployed in different environments(To be discussed later).
First we identify the manual process of how our source code will reach upto deployment.
- Commit and push your latest code in remote repository.
- Run unit test before building your project.
- Build your project to create war or jar file.
- Create an docker image and push it in docker registry (Docker hub).
- Deploy your app.
As you can see theres nothing wrong in this stepd but imagine doing it every day, every week, or anytime you want its a lot of work and labor needed.
- Repeated Work.
- Takes a lot of time and effort.
- Deploying code in multiple environment is just nightmare.
- Error prone.
Thats why we need CI/ CD to resolved all of this increasing our productivity and less time and effort for this repeated work.
- Development Environment
- Quality Assurance Environment
- User Assurance Test Environment
- Production Environment
- Development Team: Responsible for writing project source code.
- Quality Assurance Team: Responsible for testing the development team delivered project and also verify and validate all the system requirements.
- Operation Team: Responsible for Deployment of project.
- Free and open-source software
- Written in Java Languange
- Used for automation pf Build and Deployment process.
- Using Jenkins we can implement CI/ CD
- It is the series of steps to automate the build and deployment process of your project.
Its like your house water pipeline how will the water flow through the pipeline to reach the water destination.
- Where your source code coming from (Usually Github is used in this)
- When you want to execute and automate the build process.
- POLL SCMM is used to continously pull and automate build process with specified cron job
- Setup the environment for build process.
- How you want to do the build?
- What you want to do after build process is finished either create an docker image and push it in docker registry
pipeline {
agent any
tools { // Add tools here
}
environment { // Add environment variables here to be access in this Jenkinsfile
}
stages { // Series of Stage to be executed to Automate the build and deployment process of your project
stage("<stage_name>") {
steps { // This is where your command will go
}
}
}
post {
always {} // Execute any command no matter what happen
success {} // Execute any command only in build success
failure {} // Execute any command only in build failure
}
}
- Create DockerHub Credential(Access Token) for jenkins. The idea is for jenkins to have permission to access your dockerhub account
- Goto Your DockerHub Account > Click your Profile > My Account > Security > Click New Access Token > Provide the required fields > Click Generate > Click copy and close.
- Add the DockerHub Credentials(Access Token) in Jenkins Server
- Goto Your Jenkins Webb App > Manage Jenkins > Under Security Tab (Click Credentials) > Under Stores scoped to Jenkins (Click System) > Click Global credentials (unrestricted) > Click Add Credential > Add Username(Your DockerHub Username), Add Password (Your DockerHub Credential Access Token), Add ID (You will use this ID to access your credentials in Jenkins pipeline code) > Click Create
- Create a Jenkins Pipeline
- New Item
- Provide Job Name and Click Pipeline
- Add GitHub project link 5 Under Build Trigger > Check POLL SCM (Everytime you push in specified repo this job will be triggereed automatically) and supply the cron expression.
- Add Pipeline Script (I will use my own api the security-question-api as example).
pipeline {
agent any
tools {
maven 'Maven 3.9.6' // Maven 3.9.6 is the name of maven I previously provide in setting up maven in jenkins
}
environment {
DOCKERHUB_CREDENTIALS = credentials('docker-hub-access-token-for-jenkins') // docker-hub-access-token-for-jenkins is the ID of my previously created credential
}
stages {
stage("Clone Security Question API from Github") {
steps {
echo "Cloning Security Question API from Github. Please Wait..."
git branch: 'main',
url: 'https://github.com/Elleined/security-question-api'
echo "Cloning Security Question API from Github. Success!"
}
}
stage("Build project using maven") {
steps {
echo "Cleaning and Generating jar file using maven. Please Wait..."
sh 'mvn clean install'
echo "Cleaning and Generating jar file using maven. Success!"
}
}
stage("Create Docker Image") {
steps {
echo "Creating docker image. Please Wait..."
sh 'docker build -t sqa:latest .'
echo "Creating docker image. Success!"
}
}
stage("Log in to DockerHub") {
steps {
echo "Logging in to DockerHub. Please Wait..."
sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
echo "Logging in to DockerHub. Success!"
}
}
stage("Push docker image to DockerHub") {
steps {
echo "Pushing docker image to DockerHub. Please Wait..."
sh 'docker tag sqa:latest elleined/sqa:latest'
sh 'docker push elleined/sqa:latest'
echo "Pushing docker image to DockerHub. Success!"
}
}
} // End of Stages
post {
always {
sh 'docker logout'
}
}
} // End of Pipeline
- Go to Manage Jenkins > Click Plugins > Available Plugins
- Search ThinBackup and install
- Restart the jenkins server by http://<jenkins_server_ip>:<jenkins_server_port>/restart
- Go to Manage Jenkins > Under Tools and Action > Click ThinBackup
- Click Settings
- Fill out the required configuration fields to backup and restore jenkins server (!!!Note backup directory must be /var/lib/jenkins/backup to avoid headache)
- Usually backups runs automatically depends on cron expression you set in thin backup settings. Otherwise to run backup manually heres the steps.
- Go to Manage Jenkins > Under Tools and Action > Click ThinBackup
- Click Backup Now and that's it
- Go to Manage Jenkins > Under Tools and Action > Click ThinBackup
- Click Restore and Select the backup you want to restore.
- Lastly restart the jenkins server to reflect the restored backup
- Restart the jenkins server by http://<jenkins_server_ip>:<jenkins_server_port>/restart