In this section, we will cover the fundamental concepts of creating and running jobs in Jenkins. Jobs are the basic building blocks in Jenkins, and understanding how to create and manage them is essential for automating tasks and workflows.

Key Concepts

  1. Job Types: Jenkins supports various types of jobs, including Freestyle projects, Pipeline projects, Multi-configuration projects, and more.
  2. Job Configuration: Each job has a configuration page where you can define its settings, such as source code management, build triggers, build steps, and post-build actions.
  3. Build Triggers: These are conditions that trigger the execution of a job, such as SCM changes, scheduled times, or manual triggers.
  4. Build Steps: These are the actions that Jenkins performs during the job execution, such as compiling code, running tests, or deploying applications.
  5. Post-build Actions: These are actions that Jenkins performs after the build steps are completed, such as sending notifications or archiving artifacts.

Creating a Freestyle Job

Step-by-Step Guide

  1. Access Jenkins Dashboard:

    • Open your Jenkins instance in a web browser.
    • Log in with your credentials.
  2. Create a New Job:

    • Click on the "New Item" link on the left-hand side of the dashboard.
    • Enter a name for your job (e.g., "MyFirstJob").
    • Select "Freestyle project" and click "OK".
  3. Configure the Job:

    • General: Provide a description for your job.
    • Source Code Management: Configure the repository from which Jenkins will pull the code. For example, you can use Git and provide the repository URL.
    • Build Triggers: Set up triggers for the job. For example, you can choose "Poll SCM" and set a schedule.
    • Build Steps: Add build steps. For example, you can add a "Execute shell" step and provide a shell script to run.
    • Post-build Actions: Add any post-build actions. For example, you can set up email notifications.
  4. Save the Job:

    • Click the "Save" button at the bottom of the configuration page.

Example Configuration

Here is an example configuration for a simple Freestyle job that pulls code from a Git repository and runs a shell script:

General:
  - Description: This is a simple job to demonstrate Jenkins Freestyle project.

Source Code Management:
  - Git:
    - Repository URL: https://github.com/example/repo.git

Build Triggers:
  - Poll SCM: H/5 * * * *

Build Steps:
  - Execute shell:
    - Script: |
        #!/bin/bash
        echo "Building the project..."
        ./build.sh

Post-build Actions:
  - Email Notification:
    - Recipients: [email protected]

Running the Job

Manual Trigger

  1. Navigate to the Job:

    • From the Jenkins dashboard, click on the job name (e.g., "MyFirstJob").
  2. Build the Job:

    • Click on the "Build Now" link on the left-hand side.
  3. Monitor the Build:

    • You can monitor the build progress in the "Build History" section.
    • Click on the build number to view the build details and console output.

Automatic Trigger

If you have configured build triggers (e.g., SCM polling), Jenkins will automatically run the job based on the specified conditions.

Practical Exercise

Exercise 1: Create and Run a Freestyle Job

  1. Objective: Create a Freestyle job that pulls code from a Git repository and runs a shell script.
  2. Steps:
    • Create a new Freestyle job named "ExerciseJob".
    • Configure the job to pull code from https://github.com/example/repo.git.
    • Add a build step to run a shell script that prints "Hello, Jenkins!".
    • Save the job and run it manually.
  3. Expected Outcome: The job should run successfully, and the console output should display "Hello, Jenkins!".

Solution

  1. Create a New Job:

    • Name: ExerciseJob
    • Type: Freestyle project
  2. Configure the Job:

    • Source Code Management:
      • Git:
        • Repository URL: https://github.com/example/repo.git
    • Build Steps:
      • Execute shell:
        • Script: echo "Hello, Jenkins!"
  3. Run the Job:

    • Navigate to "ExerciseJob".
    • Click "Build Now".
    • Check the console output for "Hello, Jenkins!".

Summary

In this section, we learned how to create and run Freestyle jobs in Jenkins. We covered the key concepts, including job types, configuration, build triggers, build steps, and post-build actions. We also walked through a practical example and provided an exercise to reinforce the learned concepts. Understanding how to create and manage jobs is crucial for automating tasks and workflows in Jenkins. In the next section, we will delve deeper into Jenkins Pipelines, which offer more advanced and flexible ways to define and manage jobs.

© Copyright 2024. All rights reserved