How to use Jenkins free style projects for Docker application | #Day 23 | #90daysof devops

ยท

8 min read

How to use Jenkins free style projects for Docker application | #Day 23 | #90daysof devops

Hey there, fellow DevOps enthusiasts! In today's blog post, we're going to dive into the exciting world of CI/CD, understand what a Build job is, and explore the concept of Freestyle Projects. ๐Ÿค–

1. What is CI/CD? ๐Ÿ”„

CI/CD stands for Continuous Integration and Continuous Deployment (or Continuous Delivery). It's the backbone of modern software development and release processes.

๐Ÿ”„ Continuous Integration (CI): This practice involves frequently integrating code changes into a shared repository. Developers commit their code changes to this repository multiple times a day. Automated tests are run to ensure that these changes don't break existing functionality.

๐Ÿš€ Continuous Deployment (CD): CD takes CI a step further by automatically deploying code changes to production after they pass all tests. This rapid and automated deployment helps teams release software updates quickly and with minimal human intervention.

In a nutshell, CI/CD streamlines the software development lifecycle, leading to faster, more reliable, and efficient software releases. ๐ŸŒŸ

2. What is a Build Job? ๐Ÿ—๏ธ

A Build Job is a fundamental component of CI/CD pipelines. It's a dedicated task responsible for compiling, building, and packaging your application code. Think of it as a construction worker that assembles all the pieces of your software into a deployable package.

๐Ÿ—๏ธ Key Functions of a Build Job:

  • Code Compilation: It compiles the source code into executable binaries.

  • Dependency Management: It resolves and manages project dependencies.

  • Running Tests: Build jobs often include running unit tests to verify code quality.

  • Artifact Creation: It packages the application into deployable artifacts (e.g., JAR, WAR, Docker container).

The success of a Build Job is crucial because it ensures that your application is ready for the next stages of the pipeline, such as testing and deployment. ๐Ÿ’ช

3. What is Freestyle Projects? ๐ŸŽจ

In the world of CI/CD, "Freestyle Projects" refers to a flexible and customizable project type that Jenkins, a popular CI/CD tool, offers.

๐ŸŽจ Key Features of Freestyle Projects:

  • User-Friendly: Freestyle projects are beginner-friendly and provide a graphical interface for configuring build and deployment steps.

  • Customizable: You can define your entire build and deployment process using a combination of build steps, post-build actions, and plugins.

  • Versatile: Freestyle projects can handle a wide range of project types, making them suitable for various applications.

Freestyle projects are an excellent choice when you need a quick and easy way to set up your CI/CD pipelines, especially if you're just getting started with CI/CD.

In conclusion, CI/CD, Build Jobs, and Freestyle Projects are essential components of modern DevOps practices. They help teams deliver high-quality software faster and more efficiently. Embrace these concepts, and you'll be well on your way to mastering the art of DevOps! ๐Ÿš€๐Ÿ‘จโ€๐Ÿ’ป

Stay tuned for more exciting DevOps topics in future blog posts! If you have any questions or need more insights, feel free to drop a comment below. Happy coding! ๐Ÿค—๐ŸŒ

How to Create and Run a Freestyle Project for Docker Applications?

In this section, I will show you how to create and run two freestyle projects for building and running Docker applications.

To create a freestyle project for this task, follow these steps:

  1. Go to your Jenkins dashboard and click on New Item.

  2. Enter a name for your project (e.g., Django_todo_app) and select Freestyle project. Then click OK.

  3. On the configuration page, you can add some descriptions for your project if you want.

  4. Add the GitHub URL on which the project is present.

  5. Under Source Code Management, select Git and enter the repository URL: https://github.com/avanishnit08/react_django_demo_app.git You can leave the other fields as default.

  6. Specify the branch whether it is in main branch or master branch.

  7. Under Build Triggers, In Jenkins, a build trigger is a mechanism that initiates the execution of a Jenkins job (typically a build job or a pipeline) automatically based on certain events or conditions. Build triggers are essential for automating the Continuous Integration (CI) and Continuous Deployment (CD) processes. They ensure that builds and deployments are triggered without manual intervention, which is crucial for maintaining a fast and reliable development and delivery pipeline.

    For example, if you select Build periodically and enter a cron expression (e.g., * * * * *) to run your project every minute. For this task, we will leave this section blank and run our project manually.

  8. Under Build steps, this is the most important part of the freestyle project. click on the Add build step and select Execute shell (or Execute Windows batch command if you are using Windows). This will allow you to run any shell or batch command as part of your project.

  9. In the text area that appears, enter the command docker build . -t django-app to build the image for the web application using Docker. The -t flag specifies the name of the image (hello_flask) and the . specifies the current directory as the build context.

  10. In the text area that appears, enter the command docker run -d -p 8001:8001 --name django-app django-app:latest to run a container using the image created in step 7. The -d flag runs the container in detached mode, the -p flag maps port 5000 of the container to port 5000 of the host, and the --name flag specifies the name of the container (django-app).

  11. Click Save to save your project.

Now, go back to your Jenkins dashboard and click on your project name (e.g., Django-to-do app). Then click on Build Now to run your project.

You will see a new build number appear under Build History with a blue ball indicating that the build is in progress. You can click on it to see the details of the build.

Click on Console Output to see the logs of the build. You should see something like this:

Started by user Avanish Singh
Running as SYSTEM
Building in workspace C:\ProgramData\Jenkins\.jenkins\workspace\Django_to_do app
The recommended git tool is: NONE
No credentials specified
 > C:\Program Files\Git\bin\git.exe rev-parse --resolve-git-dir C:\ProgramData\Jenkins\.jenkins\workspace\Django_to_do app\.git # timeout=10
Fetching changes from the remote Git repository
 > C:\Program Files\Git\bin\git.exe config remote.origin.url https://github.com/avanishnit08/react_django_demo_app.git # timeout=10
Fetching upstream changes from https://github.com/avanishnit08/react_django_demo_app.git
 > C:\Program Files\Git\bin\git.exe --version # timeout=10
 > git --version # 'git version 2.42.0.windows.2'
 > C:\Program Files\Git\bin\git.exe fetch --tags --force --progress -- https://github.com/avanishnit08/react_django_demo_app.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > C:\Program Files\Git\bin\git.exe rev-parse "refs/remotes/origin/main^{commit}" # timeout=10
Checking out Revision c2dca3f30890792a3f6eb6078f05f4d886b40129 (refs/remotes/origin/main)
 > C:\Program Files\Git\bin\git.exe config core.sparsecheckout # timeout=10
 > C:\Program Files\Git\bin\git.exe checkout -f c2dca3f30890792a3f6eb6078f05f4d886b40129 # timeout=10
Commit message: "Update index.html"
 > C:\Program Files\Git\bin\git.exe rev-list --no-walk c2dca3f30890792a3f6eb6078f05f4d886b40129 # timeout=10
[Django_to_do app] $ C:\Windows\system32\cmd.exe -xe C:\Windows\TEMP\jenkins18246077082840183520.sh
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\ProgramData\Jenkins\.jenkins\workspace\Django_to_do app>Finished: SUCCESS

Create a Freestyle Project for Building and Running Multiple Containers

In this task, I will create a freestyle project that builds and runs multiple containers using a docker-compose file.

The docker-compose file defines two services: a web application and a database. The web application is the same as in task-01, but it connects to the database using SQLAlchemy. The database is a PostgreSQL server that stores some data for the web application.

The source code for the web application and the database is available on GitHub: github.com/LondheShubham153/two-tier-flask-..

The docker-compose file for the application and the database is:

version: '3'
services:

  backend:
    build:
      context: .
    ports:
      - "5000:5000"
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: your_username
      MYSQL_PASSWORD: your_password
      MYSQL_DB: your_database
    depends_on:
      - mysql

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: your_root_password
      MYSQL_DATABASE: your_database
      MYSQL_USER: your_username
      MYSQL_PASSWORD: your_password
    volumes:
      - ./message.sql:/docker-entrypoint-initdb.d/message.sql   # Mount sql script into container's /docker-entrypoint-initdb.d directory
      - mysql-data:/var/lib/mysql  # Mount the volume for MySQL data storage

volumes:
  mysql-data:

To create a freestyle project for this task, follow these steps:

  1. Go to your Jenkins dashboard and click on New Item.

  2. Enter a name for your project (e.g., Flask DB App) and select Freestyle project. Then click OK.

  3. On the configuration page, you can add some descriptions for your project if you want.

  4. Under Source Code Management, select Git and enter the repository URL: github.com/LondheShubham153/two-tier-flask-... You can leave the other fields as default.

  5. Under Build Triggers, you can choose when you want your project to run automatically. For example, you can select Build periodically and enter a cron expression (e.g., * * * * *) to run your project every minute. For this task, we will leave this section blank and run our project manually.

  6. Under Build, click on Add build step and select Execute shell (or Execute Windows batch command if you are using Windows). This will allow you to run any shell or batch command as part of your project.

  7. In the text area that appears, enter the command docker-compose up -d to start the multiple containers defined in the docker-compose file using Docker. The -d flag runs the containers in detached mode.

  8. Under Post-build Actions, click on Add post-build action and select Execute shell (or Execute Windows batch command if you are using Windows). This will allow you to run any shell or batch command after your build is finished.

  9. In the text area that appears, enter the command docker-compose down to stop and remove the containers defined in the docker-compose file using Docker.

  10. Click Save to save your project.

    Now, go back to your Jenkins dashboard and click on your project name (e.g., Flask DB App). Then click on Build Now to run your project.

    You will see a new build number appear under Build History with a blue ball indicating that the build is in progress. You can click on it to see the details of the build.

    Click on Console Output to see the logs of the build.

    By following these steps, You can see that the containers are created, started, stopped, and removed successfully.

I hope this blog post helps you understand how to use the Jenkins Freestyle project for building and running Docker applications.

Thank you for reading! ๐Ÿ˜Š

ย