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
- Job Types: Jenkins supports various types of jobs, including Freestyle projects, Pipeline projects, Multi-configuration projects, and more.
- 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.
- Build Triggers: These are conditions that trigger the execution of a job, such as SCM changes, scheduled times, or manual triggers.
- Build Steps: These are the actions that Jenkins performs during the job execution, such as compiling code, running tests, or deploying applications.
- 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
-
Access Jenkins Dashboard:
- Open your Jenkins instance in a web browser.
- Log in with your credentials.
-
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".
-
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.
-
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
-
Navigate to the Job:
- From the Jenkins dashboard, click on the job name (e.g., "MyFirstJob").
-
Build the Job:
- Click on the "Build Now" link on the left-hand side.
-
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
- Objective: Create a Freestyle job that pulls code from a Git repository and runs a shell script.
- 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.
- Expected Outcome: The job should run successfully, and the console output should display "Hello, Jenkins!".
Solution
-
Create a New Job:
- Name: ExerciseJob
- Type: Freestyle project
-
Configure the Job:
- Source Code Management:
- Git:
- Repository URL:
https://github.com/example/repo.git
- Repository URL:
- Git:
- Build Steps:
- Execute shell:
- Script:
echo "Hello, Jenkins!"
- Script:
- Execute shell:
- Source Code Management:
-
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.
Jenkins: From Beginner to Advanced
Module 1: Introduction to Jenkins
Module 2: Jenkins Basics
- Jenkins Dashboard Overview
- Creating and Running Jobs
- Understanding Jenkins Pipelines
- Using Jenkins Plugins
Module 3: Jenkins Pipelines
Module 4: Advanced Jenkins Pipelines
- Pipeline Stages and Steps
- Parallel Execution in Pipelines
- Using Environment Variables
- Pipeline Best Practices
Module 5: Jenkins Administration
Module 6: Integrating Jenkins
- Integrating with Version Control Systems
- Integrating with Build Tools
- Integrating with Testing Tools
- Integrating with Deployment Tools