Day23:Jenkins Freestyle Project for DevOps Engineers

Day23:Jenkins Freestyle Project for DevOps Engineers


What is CI/CD?

CI/CD stands for Continuous Integration and Continuous Deployment (or Delivery). It is a software development approach that aims to automate and streamline the processes of integrating code changes and deploying software applications.

Continuous Integration (CI) refers to the practice of frequently merging code changes from multiple developers into a shared repository. With CI, developers integrate their changes into a central version control system, such as Git, regularly. Each integration triggers an automated build process that compiles the code, runs tests, and performs other quality checks to identify issues early on.

Continuous Deployment (CD) is the next step after continuous integration. It involves automatically deploying the successfully tested and validated code changes to a staging or production environment. CD eliminates the need for manual intervention in the deployment process, allowing developers to rapidly and reliably release new features and bug fixes.

  1. Automated Build and Testing: CI/CD systems typically include automated build and testing processes. When developers integrate their code changes, the CI/CD system automatically builds the application, runs unit tests, and performs other types of tests such as integration, performance, or security tests. This ensures that the code changes are functional and meet the required quality standards.

  2. Version Control and Branching: CI/CD relies heavily on version control systems like Git. Developers work on their code changes in separate branches and commit their changes to the central repository. CI/CD systems monitor the repository for changes and trigger the build and testing processes accordingly. Branching allows multiple developers to work concurrently on different features or bug fixes without interfering with each other's code.

  3. Continuous Integration: CI focuses on integrating code changes frequently to detect integration issues early. By integrating code changes regularly, developers can identify conflicts, dependencies, and other issues in the early stages of development. CI systems automatically build and test the integrated code, providing quick feedback to developers about the health of their changes.

  4. Continuous Deployment/Delivery: CD takes the process further by automating the deployment of successfully tested code changes to a staging or production environment. This automation eliminates the need for manual deployment steps, reducing the chances of human error and improving the speed and reliability of deployments. CD allows teams to release new features, bug fixes, and enhancements to end-users rapidly.

  5. Infrastructure as Code (IaC): CI/CD often involves the use of infrastructure automation tools like Terraform or CloudFormation, which enable teams to define and provision the required infrastructure components (such as servers, databases, and networking) as code. This approach, known as Infrastructure as Code (IaC), ensures that the infrastructure is consistent, reproducible, and version-controlled, making it easier to manage and deploy applications.

  6. Monitoring and Rollbacks: CI/CD systems are often integrated with monitoring and alerting tools. Once the code changes are deployed, the system monitors the application's performance and health in real time. If any issues or anomalies are detected, alerts are generated, and the system can automatically roll back the deployment to a previously known good state.

CI/CD practices have become increasingly popular in modern software development workflows. They empower development teams to deliver software faster, with higher quality, and more reliably. By automating repetitive tasks, minimizing manual intervention, and providing rapid feedback, CI/CD helps streamline the entire software development and deployment lifecycle.

Benefits of CI/CD:

CI/CD offers several benefits to software development teams and organizations. Here are some key advantages:

  1. Faster Time to Market: CI/CD enables faster software releases by automating build, testing, and deployment processes. Developers can integrate and deliver code changes more frequently, reducing the time between feature development and its availability to end-users.

  2. Improved Code Quality: With CI/CD, code changes are automatically built and tested in a consistent and reproducible manner. This helps identify issues early in the development process, such as compilation errors, test failures, or code regressions. By catching and addressing problems early.

  3. Reduced Risk and Faster Issue Detection: The automated testing and validation processes in CI/CD catch bugs, performance issues, or security vulnerabilities before they reach production. This reduces the risk of deploying faulty or compromised software and enables faster issue detection and resolution.

  4. Streamlined Collaboration: CI/CD promotes collaboration and coordination among team members. By integrating code changes frequently and automating the build and testing processes, developers can identify conflicts or dependencies early. It encourages communication and facilitates the resolution of conflicts on time

  5. Continuous Feedback and Learning: By receiving immediate test results and deployment feedback, developers can quickly iterate and improve their code. This continuous feedback loop facilitates a culture of learning, continuous improvement, and knowledge sharing within the development team.

  6. Reliable and Repeatable Deployments: CD automates the deployment process, ensuring consistent and reliable software releases. By using infrastructure as code (IaC) and standardized deployment scripts, CI/CD systems can provision and configure the required infrastructure consistently across different environments. This minimizes the risk of configuration errors and allows for repeatable and predictable deployments.

What is a build job?

A build job, in the context of CI/CD, refers to a specific task or process within a CI/CD pipeline that is responsible for building the software application from source code. It is an automated process that compiles the code, resolves dependencies and generates an executable or deployable artifact.

The build job typically incorporates multiple steps to transform the source code into a runnable or deployable form. These steps may include:

  1. Compilation: The build job compiles the source code files into executable code. This process involves translating the human-readable source code into machine-readable instructions that the computer can execute. The specific compilation steps depend on the programming language and build tools used in the project.

  2. Dependency Management: Many software projects rely on external libraries, frameworks, or modules. The build job resolves and fetches the required dependencies from specified repositories or package managers. It ensures that all the necessary dependencies are available and compatible with the project.

  3. Testing: The build job may include running automated tests to verify the functionality and correctness of the code. This typically involves executing unit tests, integration tests, and potentially other types of tests, such as performance or security tests. The test results help ensure that the code changes introduced in the build are functioning as expected and have not introduced any regressions.

  4. Code Quality Checks: The build job may incorporate code analysis tools to enforce coding standards, identify code smells, or detect potential issues or vulnerabilities. These checks help maintain code quality, consistency, and adherence to best practices.

  5. Artifact Generation: Once the build steps are completed successfully, the build job generates an artifact—a packaged and deployable version of the software. The artifact can take various forms, such as a compiled executable, a container image, or a deployment package. The generated artifact is then passed to subsequent stages in the CI/CD pipeline for further testing, deployment, or release.

some of the key benefits of build job:

  • Code Quality Assurance

  • Early Detection of Integration Issues

  • Faster Feedback Loop

  • Automated and Consistent Build Process

  • Faster Time to Deployment

  • Scalability and Efficiency

Types Of Jobs

Majorly used job types are :

  1. Freestyle project:

    • One of the most basic job types is the freestyle project. It allows you to define a build sequence using a graphical interface. Each step can consist of actions such as compiling code, running tests, deploying artifacts, and more. While it provides flexibility, it may necessitate more manual configuration than other job types.
  2. Pipeline Project:

    • Jenkins is in charge of the pipeline. Pipeline is a powerful and adaptable programming language for defining build and deployment processes. You can script your entire build pipeline, including stages, steps, conditional logic, and parallel execution. This type is commonly used for complex and adaptable automation workflows.
  3. Multibranch Pipeline:

    • When dealing with multiple branches of code, which is common in software development, this type of job is critical. It detects and builds branches automatically, ensuring continuous integration for each branch. It is especially useful for keeping separate pipelines for different branches.
  4. Parameterized Build:

    • Parameterized builds are commonly used when it is necessary to customize builds based on various inputs. This job type allows users to provide parameters when triggering a build, allowing for build process variations.
  5. Scheduled Build:

    • Scheduled builds are used to automate repetitive tasks at predefined times or regularly. They are frequently used for tasks such as nightly builds, backups, and maintenance operations.
  6. GitHub Organisation:

    • As more projects are hosted on platforms like GitHub, the GitHub Organisation job type aids in the automation of project discovery and creation within a GitHub organization. It simplifies the integration of GitHub projects with Jenkins.
  7. Distributed Build:

    • This job type is critical for improving resource utilization and shortening build times. It entails distributing build tasks across multiple agents or nodes, allowing builds to run concurrently on different machines.

What is Freestyle Projects?

Jenkins freestyle projects allow users to automate simple jobs, such as running tests, creating and packaging applications, producing reports, or executing commands. Freestyle projects are repeatable and contain both build steps and post-build actions. Even though freestyle jobs are highly flexible, they support a limited number of general build and post-build actions.

Task 1 :

Let's create a freestyle project todo app in node js which will create an image.

Create an agent for your app. ( which you deployed from docker in the earlier task)

  • Click on "Set up an agent"

  • Give Node name, click on Permanent Agent, and click on Create.

  • Provide remote root directory location give labels to this node and select "Use WebSocket".

  • Click on save to create an agent and now the agent is created.

  • Create a new Jenkins freestyle project for the todo app.

    • In the build steps, select "Execute Shell" and add a few commands and now apply and click save.

      • Now click on build now

  • Check on the environment

Task-02: Managing Docker Compose with Jenkins

  • 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.

    Step 1: Create a New Jenkins Project

    1. Log in to your Jenkins dashboard.

    2. Click on "New Item" to create a new project.

    3. Enter a name for your project, choose "Freestyle project," and click "OK."

Step 2: Configure Jenkins Project

On the project configuration page:

  • Under "General," you can provide a project description.

  • Under "Source Code Management," select the appropriate option if you're using version control.

    Step 3: Configure Build Steps

    1. In the "Build" section, click "Add build step" and choose "Execute shell."

    2. In the shell command box, enter the following command to start the containers using Docker Compose:

       echo "code cloned"
       docker-compose down
       docker-compose up -d --no-deps --build web
       echo "code deploy"

  • Used docker-compose to bring up the container

  • We used docker-compose down to bring down the container if it's already running.

Step 4: Save and Run the Jenkins Project

  1. Click "Save" to save your Jenkins project configuration.

  2. You can manually run the project by clicking "Build Now" or wait for the configured triggers to initiate the build.

    yayy 🥳🥳 !!!, You have successfully created a freestyle Jenkins project !! we explore more on Jenkins in the next articles....

Thank you for 📖reading my blog, 👍Like it and share it 🔄 with your friends if you find it knowledgeable.

Happy learning 😊😊!!