Day 23 Task: Jenkins Freestyle Project for DevOps Engineers
Hello, everyone!
Today we will show you how to use Jenkins freestyle project for building and running Docker applications.
What is CI/CD?
CI/CD stands for continuous integration and continuous delivery or deployment. It is a set of practices that help software development teams deliver code changes more frequently and reliably. CI/CD is part of DevOps, which helps shorten the software development lifecycle.
The main concepts attributed to CI/CD are:
Continuous integration: The practice of automating the integration of code changes from multiple developers into a single codebase. It involves building, testing, and merging the code regularly using tools like Jenkins.
Continuous delivery: The practice of automating the delivery of code changes to a staging or production environment where they can be tested further and verified by the stakeholders. It involves deploying the code using tools like Docker.
Continuous deployment: The practice of automating the release of code changes to the end-users without human intervention. It involves deploying the code using tools like Kubernetes.
What is a Build Job?
A build job in Jenkins is a configuration for automating a specific task or step in the application-building process. These tasks include gathering dependencies, compiling, archiving, transforming, testing, and deploying the code.
Jenkins supports different types of build jobs, such as:
Freestyle project: A flexible and easy-to-use type of build job that allows you to configure any build job using a graphical user interface. You can add multiple build steps and post-build actions using various plugins.
Pipeline project: A powerful and expressive type of build job that allows you to define your build job using a script or a graphical editor. You can create complex workflows and stages using Groovy syntax or declarative syntax.
Multi-configuration project: A type of build job that allows you to run your build job on multiple platforms or environments using different parameters or configurations. You can use matrices or axes to define the combinations of parameters or configurations.
Folder: A type of build job that allows you to organize your build jobs into folders or subfolders. You can use folders to group related build jobs or create nested views.
Multibranch pipeline: A type of build job that allows you to create pipelines for different branches of your source code repository. You can use Jenkinsfiles to define your pipelines in each branch.
Organization folder: A type of build job that allows you to create pipelines for multiple repositories in an organization or a team. You can use Jenkinsfiles to define your pipelines in each repository.
What is a Freestyle Project?
A freestyle project in Jenkins is a type of build job that allows you to automate simple jobs, such as running tests, creating and packaging applications, producing reports, or executing commands.
A freestyle project consists of:
General settings: The settings that define the name, description, parameters, options, and triggers of your project.
Source code management: The settings that define how to access your source code from a version control system such as Git, SVN, CVS, etc.
Build environment: The settings that define how to set up your build environment such as environment variables, credentials, tools, etc.
Build steps: The steps that define what commands or actions to execute during your build such as shell scripts, batch commands, Ant tasks, Maven goals, etc.
Post-build actions: The actions that define what to do after your build such as archiving artifacts, publishing reports, sending notifications, etc.
💼Task: 1
Create an Agent for Your App
In Jenkins, go to "Manage Jenkins" > " Nodes."
Click "New Node" to create a new agent. Give it a name, choose "Permanent Agent," and configure the necessary settings.
Click on the save. The new node is ready
Create a new Jenkins freestyle project for your app.
Click on the "New Item" button on the dashboard to create a new project.
Select "Freestyle project" and give the project a name (django-todo-cicd)
Click on the "OK" button to create the project.
In the "Build" section of the project, add a build step to run the "docker build" command to build the image for the container.
Give a project description and also give the project GitHub URL(node-todo-cicd project).
Give the git repository URL in the source code management section as scm is git here & give the git branch name
On the build steps section choose to execute shell and give the shell commands to create a docker image(as per the dockerfile it will create the image), after the creation of the docker image run a container of the same image. Click on save.
echo "………………Devops CI/CD Started………………"
echo "CI/CD started by User"
whoami
echo "code stage"
echo "Build stage"
docker build -t django-app .
echo "Test stage"
echo "Deploy stage"
docker run -d -p 8000:8000 django-app:latest
Click on Build
got an error output permission denied while trying to connect to the Docker daemon socket
for this error, we need to add Jenkins user to the docker group
sudo usermod -aG docker jenkins
sudo reboot
Try to build it again
Check on the Web browser
💼Task: 2
Create a Jenkins project to run "docker-compose up -d" command to start the multiple containers defined in the compose file.
Give a project name and choose a freestyle project.
Give a project description and GitHub project URL & add git repository URL
This is the docker-compose command in a shell script in the build step
Set up a cleanup step in the Jenkins project to run docker-compose down
command to stop and remove the containers defined in the compose file.
Make some changes to build the step
📍 Conclusion :
In this blog, we cover the Jenkins Freestyle Project, we have created two freestyle pipelines. In the next blog, we will cover some advanced topics.
Thank you for reading!
Contact me on Linkedin 🤝
Check out my GitHub for more resources 📚