What is CI/CD?
CI/CD stands for Continuous Integration and Continuous Delivery (or Continuous Deployment). It is a set of practices and tools that enable developers to automate the process of building, testing, and deploying software applications.
In simple terms, CI/CD helps teams deliver software faster and more reliably. Here's how it works:
Continuous Integration: Developers frequently integrate their code changes into a shared repository. With CI, these changes are automatically built and tested, ensuring that they don't break the existing codebase. This helps catch issues early on and maintains code quality.
Continuous Delivery: Once code changes pass the CI process, they are automatically deployed to a staging or testing environment. This allows for further testing and validation by stakeholders, including quality assurance teams and product owners.
Continuous Deployment: In the case of continuous deployment, code changes that successfully pass all tests and validations are automatically deployed to production, making them instantly available to users.
The benefits of CI/CD include faster time to market, improved code quality, and reduced risk of deployment failures. It enables teams to iterate quickly, deliver features more frequently, and respond to customer feedback faster.
What Is a Build Job?
In simple terms, a Build job is a specific task performed by a build tool or a CI/CD system like Jenkins. It involves the process of compiling, testing, and packaging source code to create a deployable artifact, such as an executable file, library, or container image.
A build job is a task performed by a build tool or CI/CD system. It involves:
Compiling source code into executable code or libraries.
Running tests to check code functionality and stability.
Packaging code into a deployable artifact.
Managing dependencies required by the code.
Ensuring code quality and identifying bugs.
Preparing code for deployment.
Facilitating collaboration among developers.
Supporting continuous integration and delivery of software.
What are Freestyle Projects?
Freestyle projects in Jenkins are flexible and customizable projects where you can define build steps, scripts, and triggers. They allow you to build, test, and deploy software with control and adaptability to project needs.
Task-01
create an agent for your app. ( which you deployed from docker in the earlier task)
Create a new Jenkins freestyle project for your app.
In the "Build" section of the project, add a build step to run the "docker build" command to build the image for the container.
Add a second step to run the "docker run" command to start a container using the image created in step 3.
In This Task, we are using React Django App.
Step 1 --> Proceed with creating New Item.
Step 2 --> Name the Project "React_Django_app" choose Freestyle Project and click ok.
Step 3 --> Now give the description and give the Github project URL of the project "React_Django_app"
Step 4 --> In Source Code Management choose Git because we need to provide the Repositories URL and Branch name in Branch Specifier. Since the project branch is attached to the main branch so we will specify the main in Branch Specifier.
Step 5 --> Add a build step by clicking on "Add build step" and selecting "Execute shell".
Enter the following commands to build the Docker image for your app and to start a container using the Docker image created.
docker build . -t react_django_app
#####docker build:--> This command is used to build a Docker image.
#####(.):--> The period (dot) represents the build context, indicating that the Dockerfile and application files are present in the current directory.
#####-t react_django_app:--> The -t flag is used to specify the tag or name for the Docker image. In this case, the image will be tagged as react_django_app
docker run -d --name react_django_app -p 8001:8001 react_django_app:latest
#####docker run:--> This command is used to start a Docker container.
#####-d: The -d flag runs the container in detached mode, which means it will run in the background.
#####--name react_django_app:--> The --name flag is used to give a name to the container. In this case, the container will be named react_django_app.
#####-p 8001:8001:-->The -p flag is used to map the port of the host machine to the port of the container. In this case, port 8001 of the host machine is mapped to port 8001 of the container.
#####react_django_app:latest:-->The react_django_app:latest specifies the image to be used for creating the container.The :latest tag is used to specify the latest version of the image.
Step 6 --> Now before running the build now make sure that the below commands are installed in the Ubuntu machine.
sudo apt-get install docker.io -y
sudo usermod -aG docker jenkins
sudo reboot
Note:- It is compulsory to add jenkins user to docker group otherwise it will show error while building.
Adding the jenkins
user to the docker
group using the sudo usermod -aG docker jenkins
command is a common step to grant the Jenkins user the necessary permissions to interact with Docker. This allows Jenkins to execute Docker commands without encountering permission errors.
sudo usermod -aG docker jenkins
After completing these steps, the jenkins
user should have the necessary permissions to interact with Docker.
Step 7--> Now click on Build Now.
Step 8 --> Once the output shows Finished: SUCCESS that means our application is ready.
Step 9 --> Access your Application.
Task-02
Create Jenkins project to run the "docker-compose up -d" command to start the multiple containers defined in the compose file (Hint- use day-19 Application & Database docker-compose file)
Set up a cleanup step in the Jenkins project to run the "docker-compose down" command to stop and remove the containers defined in the compose file.
Step 1--> Using the same React_django_app project
Before running the "docker-compose up -d" we need to install docker-compose in our Ubuntu machine.
Use the below command to install docker-compose
sudo apt-get install docker-compose -y
Step 2--> Now give the docker-compose commands in Execute shell.
docker-compose down
docker-compose up -d
Note: Docker-Compose uses the YAML file which is present in the project.
Step 3--> Now go back to Build Now and click on it and check for console output.
Step 4--> Once the output shows Finished: SUCCESS that means our application is ready.
Thank you for reading. I hope you were able to understand and learn something new from my blog.
Happy Learning!
Please follow me on Hashnode and do connect with me on LinkedIn ArnavSingh