Skip to main content

Command Palette

Search for a command to run...

Day 23: Jenkins Freestyle Project for DevOps Engineers.

DevOps Learning

Updated
5 min read
Day 23: Jenkins Freestyle Project for DevOps Engineers.

Before we dive in, let's first discuss:

Freestyle vs. Pipeline projects.

  1. Freestyle Projects:

    • GUI Lovers: Freestyle projects are like the old-school Jenkins approach. You know, the one where you click buttons and drag things around in the Jenkins web interface. It’s all about that graphical user interface (GUI).

    • Simple and Traditional: Freestyle projects are great for straightforward scenarios. If you’re just dipping your toes into Jenkins or CI/CD, this is where you start. Think of it as the training wheels for your Jenkins journey.

    • Less Complexity: When your CI/CD needs are relatively simple—like building, testing, and deploying without too much orchestration—Freestyle projects fit the bill.

    • Config.xml: Fun fact: You can peek behind the scenes by appending /config.xml to your Jenkins job URL in the browser. It reveals the entire job configuration in XML format. Handy for those curious souls who want to see what’s happening under the hood.

  2. Pipeline Jobs:

    • Code as Configuration: Now, let’s talk about the cool kid on the block—Pipeline! It’s all about code. Specifically, Groovy code (which is like Java’s hip cousin). With Pipeline, you define your entire CI/CD process in a script.

    • Flexibility and Power: Pipeline gives you superpowers. Seriously! You can break your build process into stages (think build, test, deploy) and even run them in parallel. If something goes haywire, you’ll know exactly which stage caused the ruckus.

    • Pipeline as Code: The magic lies in the Jenkinsfile—a script that lives alongside your source code. It’s like having a recipe for your CI/CD cake. You can version control it, revert changes, and track every tweak.

    • Declarative or Scripted: Pipeline can be either declarative (with a nice graphical editor if you have the Blue Ocean plugin) or scripted (where you write everything in Groovy). Choose your flavor!

So, Which One Should You Use?

  • If you’re a Jenkins veteran and love the idea of code-driven pipelines, go for Pipeline. It’s like upgrading from a flip phone to a smartphone.

  • But if you’re new to Jenkins or just need a straightforward build job, Freestyle projects are your starting point. You can always evolve into a Pipeline master later.

Task:

  • Create a agent for your app.

    In Jenkins, an agent (or node) represents a machine where your build jobs will run. You can set up different types of agents, such as a master (where Jenkins itself runs) or a slave (a separate machine).

  • To create an agent for your app:

    • Go to your Jenkins dashboard.

    • Click on “Manage Jenkins” in the left sidebar.

    • Choose “Manage Nodes and Clouds.”

    • Click on “New Node” or “New Agent.”

    • Provide a name for your agent (e.g., “my-app-agent”).

    • Select the appropriate launch method (e.g., “Launch agent via SSH” if you’re using a remote machine).

    • Configure the necessary credentials and connection details.

    • Save your agent configuration.

    • By default, your agent will be offline. To activate it, click on your node, and it will provide you with the commands to run your agent.

Create a new Jenkins freestyle project for your app.

Now let’s create a new Jenkins job to build and deploy your app:

  • Click on “New Item” on the Jenkins dashboard.

  • Choose “Freestyle project.”

  • Give your project a meaningful name (e.g., “my-app”).

  • Click “OK” to create the project.

To use the newly created agent in your project:

In the "Build" section of the project, add a build step to run the "docker build" command to build the image for the container.

In your newly created project:

  • Go to the project configuration.

  • Click on the “Build” section.

  • Add a build step:

    • Choose “Execute shell” (since we’ll be running Docker commands).

    • In the shell command box, enter the following:

        # Navigate to your app's source code directory
        cd /path/to/your/app
      
        # Build the Docker image
        docker build -t my-app-image .
      
        # Tag the image (optional but recommended)
        docker tag my-app-image my-registry/my-app-image:latest
      
    • Replace /path/to/your/app with the actual path to your app’s source code. Alternatively, you can use this flaskApp project mentioned in this blog.

    • One important thing to mention is the /path/to/your/app. Your Jenkins user might not have access to your project path, so you need to grant it. If you're unsure how to do this, you can follow this DevOps Learning: Day - 6.

Add a second step to run the "docker run" command to start a container using the image created in step 3.

  • Still in the “Build” section of your project configuration:

    • Click “Add build step” and choose “Execute shell” again.

    • Enter the following:

        # Run the Docker container
        docker run -d --name my-app-container -p 5000:5000 my-app-image
      
    • This command starts a container named my-app-container based on the image you built earlier. It maps port 5000 on the host to port 5000 inside the container.

  • Save and Run Your Jenkins Job:

    • Save your project configuration.

    • Trigger a build manually or set up a webhook to automatically trigger builds when changes occur in your app’s repository.

    • Jenkins will execute the build steps you defined, including building the Docker image and running the container.

      Your app should be running.

Thank you for reading😉.