Day 23 Task: Jenkins Freestyle Project for DevOps Engineers
What is CI/CD?
CI (Continuous Integration) is the practice of automating the integration of code changes from multiple developers into a single codebase. It involves developers frequently committing their work into a central code repository (such as GitHub or Stash). Automated tools then build the newly committed code and perform tasks like code review, ensuring that the code is integrated smoothly. The key goals of Continuous Integration are to find and address bugs quickly, make the integration process easier across a team of developers, improve software quality, and reduce the time it takes to release new features.
CD (Continuous Delivery) follows Continuous Integration and ensures that new changes can be released to customers quickly and without errors. This includes running integration and regression tests in a staging environment (similar to production) to ensure the final release is stable. Continuous Delivery automates the release process, ensuring a release-ready product at all times and allowing deployment at any moment.
What Is a Build Job?
A Jenkins build job contains the configuration for automating specific tasks or steps in the application building process. These tasks include gathering dependencies, compiling, archiving, transforming code, testing, and deploying code in different environments.
Jenkins supports several types of build jobs, such as freestyle projects, pipelines, multi-configuration projects, folders, multibranch pipelines, and organization folders.
What is a Freestyle Project?
A freestyle project in Jenkins is a type of project that allows you to build, test, and deploy software using various options and configurations.
Task 1:
Create an agent for your app
- Go to the manage Jenkins and choose Nodes
- Click on new node and add name of the node, select permanent agent and click on create.
- Fill out the form for creating the agent like below demonstrate
Here in remote root directory, we have to give workspace library where we store our development work, so created one directory in jenkins slave server to store the task and give the directory name.
Give the IP address of your jenkins slave at host option and leave credentials as now and click on save.
After saving the node we will get the node or agent in our screen however the credentials haven’t set yet.
Go to the manage jenkins and choose credentials option to add credentials
In stored scope to jenkins click on global to add new credentials.
Add the esstainal things for your agent access.
In private key direct entry give the private key of your jenkins master and create.
Again go to the manage jenkins and select nodes and click on drop down of your agent and select configure.
After choosing configure add SSH-key of your agent in credentials row and click on save button.
Once you save it relauch the agent and you will see the agents got created and connected successfully.
Create a new Jenkins freestyle project for your app.
Create a New Jenkins Freestyle Project:
Go to Jenkins dashboard and click on "New Item."
Enter a name for the project and select "Freestyle project."
Configure the Build Steps:
- In the "Build" section, add a build step to run the Docker build command to build the image for the container:
docker build -t flask-app:latest .
- Add another build step to run the Docker run command to start a container using the image created in the previous step:
docker run -d -p 8000:8000 --name flask_app flask-app-jenkins:latest
Output
Started by user Vibhuti Jain
Running as SYSTEM
Building remotely on vibhuti2 (dev2) in workspace /home/ubuntu/jenkins/workspace/Shopping-website
[Shopping-website] $ /bin/sh -xe /tmp/jenkins12232045254549684030.sh
+ cd two-tier-flask-app
+ docker build -t flask-app-jenkins:latest .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
Install the buildx component to build images with BuildKit:
https://docs.docker.com/go/buildx/
Sending build context to Docker daemon 157.2kB
Step 1/8 : FROM python:3.9-slim
---> 144b33e63fab
Step 2/8 : WORKDIR /app
---> Using cache
---> 947ad3442ec7
Step 3/8 : RUN apt-get update && apt-get upgrade -y && apt-get install -y gcc default-libmysqlclient-dev pkg-config && rm -rf /var/lib/apt/lists/*
---> Using cache
---> 45f330cbad25
Step 4/8 : COPY requirements.txt .
---> Using cache
---> 917bdfb18282
Step 5/8 : RUN pip install mysqlclient
---> Using cache
---> 4ff658d6b103
Step 6/8 : RUN pip install --no-cache-dir -r requirements.txt
---> Using cache
---> ca6e9b92efff
Step 7/8 : COPY . .
---> Using cache
---> 1837822b2daf
Step 8/8 : CMD ["python", "app.py"]
---> Using cache
---> d699e85635c7
Successfully built d699e85635c7
Successfully tagged flask-app-jenkins:latest
[Shopping-website] $ /bin/sh -xe /tmp/jenkins14566463593093409561.sh
+ docker run -d -p 8000:8000 --name flask_app flask-app-jenkins:latest
5af778b244cff4b896b6e06f0f12c9c1ef989ca38bacdd4dd257a43771199384
Finished: SUCCESS
Task 2
Create a Jenkins project to run the docker-compose up -d
command to start multiple containers defined in the compose file.
Create a Jenkins Project for Docker Compose:
Go to Jenkins dashboard and click on "New Item."
Enter a name for the project and select "Freestyle project."
Configure the Build Steps:
- In the "Build" section, add a build step to run the Docker Compose up command to start multiple containers defined in the compose file:
docker compose up -d
Set Up a Cleanup Step:
- Add a post-build action to run the Docker Compose down command to stop and remove the containers defined in the compose file:
docker compose down
Subscribe to my newsletter
Read articles from Vibhuti Jain directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vibhuti Jain
Vibhuti Jain
Hi, I am Vibhuti Jain. A Devops Tools enthusiastic who keens on learning Devops tools and want to contribute my knowledge to build a community and collaborate with them. I have a acquired a knowledge of Linux, Shell Scripting, Python, Git, GitHub, AWS Cloud and ready to learn a lot more new Devops skills which help me build some real life project.