What is CI/CD?
CI or Continuous Integration is the practice of automating the integration of code changes from multiple developers into a single codebase. It is a software development practice where the developers commit their work frequently to the central code repository (Github or Stash). Then there are automated tools that build the newly committed code and do a code review, etc. as required upon integration. The key goals of Continuous Integration are to find and address bugs quicker, make the process of integrating code across a team of developers easier, improve software quality and reduce the time it takes to release new feature updates.
CD or Continuous Delivery is carried out after Continuous Integration to make sure that we can release new changes to our customers quickly in an error-free way. This includes running integration and regression tests in the staging area (similar to the production environment) so that the final release is not broken in production. It ensures to automate the release process so that we have a release-ready product at all times and we can deploy our application at any point in time.
What is a Build Job?
A Jenkins build job contains the configuration for automating a specific task or step in the application building process. These tasks include gathering dependencies, compiling, archiving, or transforming code, and 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 is a flexible and user-friendly way to create automation tasks and build jobs. Unlike more complex pipeline projects, freestyle projects are designed for simplicity and are ideal for beginners. In freestyle projects, you can define build steps, set up source code management and specify post-build actions. Freestyle projects are a great starting point for automating various aspects of software development and deployment in Jenkins.
Task 01
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.
Steps:
First, set up your Jenkins by installing it in your system. If you don't know how to install it, read the day22 blog. You will find it.
After setting up your Jenkins, make sure to install docker as well.
Then add the Jenkins and Ubuntu (USER) to the docker group.
#To add the jenkins to docker group sudo usermod -aG docker jenkins #To add ubuntu to docker group sudo usermod -aG docker $USER
Then go to your Jenkins and click on Create a job.
Select freestyle projects as we did in the previous day's task.
Add the description for your project.
Add the GitHub project URL if you are taking a project from Git Hub.
Then select Git in source management. Add the repository URL of the project. And make sure you enter the same branch as in Github.
After setting up the GitHub project, go to execute shell for the further steps.
Add a build step to build the image for the container
Then add another build to run the container from the image you created with the port given in the docker file.
Save the project.
Click on Build Now to check whether your app is running or not.
After the build is completed, go to the Console output to check the output.
The build is successful. Your app is up and running.
Task 02
Create a Jenkins project to run the "docker-compose up -d" command to start the multiple containers defined in the 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.
Steps:
All the steps are the same for this task as well. You just need to install the docker-compose on your system before running the docker-compose command.
sudo apt install docker-compose
After installing the docker-compose, go to the configure of the same freestyle project of the previous task.
Edit the execute shell. Write the docker-compose command instead of the docker build and docker run command.
To stop and remove the previous container, must use the docker-compose down before the docker-compose up command.
--no-deps mean no dependencies. --build web means accessing the service mentioned in docker-compose.yaml file.
Just save and click on Build Now to run the application. Go to the console output to check the output.
Congratulations, your application is up and running with docker-compose.
<That's all for today. Hope you like it. FOLLOW to join me in the journey of DevOps>