First Job on Jenkins- Day 3
Introduction
In this post, we’ll begin creating our first job in Jenkins. We’ll create a simple freestyle project executing a basic shell script and scheduling the same. By the end of this guide, you’ll have a good understanding of how Jenkins jobs work and be ready to explore more advanced features.
Objective of the article
The objective of this article is to have hands-on experience in creating our first job on Jenkins and explore a couple of functionalities while playing with the job!
Setting Up Jenkins
Jenkins setup steps can be accessed here.
Creating the First Job
Understanding Jenkins jobs
Jenkins jobs are the fundamental units of work in Jenkins, and they are used to automate various tasks in the software development lifecycle, such as building code, running tests, deploying applications, or any custom automation script.
Step-by-step guide to creating a new job
Step 1: Access the Jenkins Dashboard
Log in to your Jenkins instance by navigating to
http://<your-server-ip>:8080
in your browser. Make sure you have logged in to the AWS console and the EC2 instance on which Jenkins is installed is started.You’ll be greeted by the Jenkins dashboard, which is your main workspace.
Step 2: Create a New Item (Job)
On the left-hand side, click on “New Item”.
Enter a name for your job, such as “First-Jenkins-Job.”
Select “Freestyle project” from the list of job types. Freestyle projects are versatile and great for basic automation tasks.
Click “OK” to proceed.
Step 3: Configure Your Job
You’ll be directed to the job configuration page, where you can define the job’s behavior.
Description (Optional): Add a brief description of what the job does, e.g., “This job prints “Hello World” to the console.”
Source Code Management: We will leave this section as “None” since this job does not use any version control systems (like Git).
Build Triggers: We will skip this for now, as it’s used to set up automated triggers (like scheduling builds).
Build Environment: We will leave the default options as is.
Step 4: Add Build Steps
Scroll down to the “Build” section.
Click on “Add build step” and select “Execute shell” (for Linux) or “Execute Windows batch command” (for Windows users).
In the command box, type the following simple script:
echo "Hello World"
Step 5: Save and Apply the Configuration
Click “Save” at the bottom of the page to apply the changes.
On saving, we would see a similar page:
Step 6: Run Your First Job
You will be redirected to the job’s main page.
Click “Build Now” on the left-hand side to trigger your first job.
Step 7: View Build Output
After the job runs, you’ll see the build history under Build History on the left.
Click on the build number (#1) to view the build details.
Click on “Console Output” to see the results of your job.
You should see the message “Hello, World!” printed in the console output, indicating that your job ran successfully!
Step 8: Exploring Failed Job Output
We will intentionally provide the wrong command in “Build Steps” and run the job
The job should ideally fail with an error. Click on Configure to the left and edit the Build steps.
Save and Run the Build
The build fails with an expected cause mentioned in the output
Rectify the error, rerun the job, and check the dashboard
Step 9: Parameterizing the Job
Let’s configure the current job for various parameters. Inside configure, check the box beside “This project is parameterized” . We will find a bundle of parameters.
Click on “String Parameter” to explore and edit the details as below:
Click on “Choice Parameter”
The rest of the parameters can also be explored.
Apply and save the changes. On running the build, we will see the following output:
These parameters can be edited before running the build.
The output:
Step 10: Scheduling a Job
Let’s create another job with a simple Shell Script which will run at a predefined period.
Write a simple shell script in the Build Steps
Scroll up to “Build Triggers” and check the box “Build periodically”
Provide 5 stars as
* * * * *
These stars represent Minutes(0-59), Hours(0-23), Days_month(1-31), Month(1-12), Day_week(0-6) respectivelySave the build and DO NOT run the build. It should automatically get triggered every minute.
Delete the Project
Conclusion
Congratulations! You’ve just created and executed your first Jenkins job. This simple exercise demonstrates the basics of setting up and running jobs in Jenkins, laying the groundwork for more complex tasks like integrating source code, automating tests, and deploying applications.
Feel free to modify the job, experiment with different build steps, and explore Jenkins’ powerful features. Stay tuned for more posts as we dive deeper into Jenkins!
Call to Action:
Was this guide helpful? Share your experience in the comments, and let me know what you’d like to see next!