Table of contents
- What is CI/CD in Jenkins?
- What is a Build Job?
- What is a Freestyle Projects?
- Task-01
- Create a agent for your app. ( which you deployed from docker in 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.
- Task-02
- Create Jenkins project to run "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 "docker-compose down" command to stop and remove the containers defined in the compose file.
What is CI/CD in Jenkins?
Continuous Integration: CI or Continuous Integration is a DevOps Software Development practice where as soon as the code is ready and committed to the central code repository(Ex-GitHub or Bitbucket), it gets automatically build and tested. The key goals of Continuous Integration is 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.
Continuous Delivery: Continuous delivery is a software development practice where code changes are automatically prepared for a release to production. It is carried out just after Continuous Integration to release the new changes in a error-free way by running all types of tests like unit testing, regression testing, load testing, integration testing, API reliability testing, etc in a non-production or staging area so that the code is not broken in a production environment and then deploying all code changes to a testing environment and/or a production environment after the build and test stage.
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.
What is a Freestyle Projects?
Jenkins freestyle projects allow users to automate simple jobs, such as running tests, creating and packaging applications, producing reports, or executing commands. It is suitable for simple or custom-build processes.
Task-01
Create a agent for your app. ( which you deployed from docker in earlier task)
An agent is typically a machine, or container, which connects to a Jenkins controller and executes tasks when directed by the controller.
To create an agent for your app, after logging to Jenkins, select "Set up an agent" under "Set up a distributed build" section
The below screen appears on clicking "Set up an agent". Provide the agent name, select Permanent and click create.
Provide the details such as description, number of executors, path of remote root directory, labels, etc and save the configuration.
Create a new Jenkins freestyle project for your app.
- Once Jenkins is logged in, click on "New Item" present in left side of the bar or "Create a Job" present in the middle of the screen
- Clicking on either of the icons will open the below page
Enter the name of the project, select Freestyle project and click "OK".
The Configuration screen opens up where user is required to enter the Configuration details.
Let's assume we pick up a project "node-todo-cicd" from GitHub. So, we need to select GitHub Project under General section and Git under Source Code Management and provide the GitHub url
In the "Build" section of the project, add a build step to run the "docker build" command to build the image for the container.
- Under "Build Step", click on Add Build Step. The below dropdown opens up. Select "Execute Shell" from the dropdown
- Enter the docker build command in the Execute Shell box -
docker run . -t node-todo-cicd
.
Add a second step to run the "docker run" command to start a container using the image created in step 3.
- Enter the command "
docker run -d -p 8000:8000 node-todo-cicd
" to start the container and click on Save.
- After Save, the below screen opens up. Click on "Build Now" option present in left side bar to build the job. Once the build is successful, a green color tick mark appears on the screen.
- Click on the #1 hyperlink and select Console Output to view the Console Output.
- User can check whether the docker is up and running by running the command "
docker ps
" in the EC2 instance as below -
- User can also view the app by providing the exposed port in the IP address. Make sure the exposed port is added in the Inbound Rules of Security Group.
The Freestyle Project is created and the app is up and running.
Task-02
Create Jenkins project to run "docker-compose up -d" command to start the multiple containers defined in the compose file (Hint- use day-19 Application & Database docker-compose file)
- Let's create a Jenkins project using the same way as earlier and put the GitHub url of the two-tier-flask app(Day -19) in the GitHub Configuration. Instead of using docker command, we will use docker-compose command to start the multiple containers contained in the two-tier-flask app. Enter the
docker-compose up -d
command to start both the containers and save.
- Once the configuration is saved, we will build the job. Job should run successfully and we should be able to see the success message in the Console Output.
- We can check whether the container is up or not by giving the commad
docker ps
in the EC2 instance. We can see both the containers are up and running.
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.
- Enter the docker-compose down command to stop and remove the containers and run the build.
- Check the Console Output and we can see that it has stopped and removed all the running containers.
- We can also validate it in the EC2 instance by giving the command docker ps. We can see below it do not shows any running container which means the running containers has been stopped and removed.
Thanks for reading!
Happy Learning ~ Shilpi!