Certified Jenkins Engineer

Jenkins Setup and Interface

Demo Working with Freestyle Job

In this tutorial, you’ll learn how to set up a Jenkins Freestyle project that fetches a random piece of advice from the AdviceSlip REST API and renders it as fun ASCII artwork using the Cowsay library. This example demonstrates REST integration, shell scripting, and environment configuration in Jenkins.

1. Create a New Freestyle Project

  1. Log into Jenkins and click New Item.
  2. Enter generate ASCII artwork as the project name.
  3. Select Freestyle project and click OK.

The image shows a Jenkins dashboard where a user is creating a new item, with options to select different project types such as Freestyle project, Pipeline, and Multibranch Pipeline.

2. Configure General Settings

Under General, set up the project metadata:

  • Description: Generate ASCII artwork using the Cowsay library and AdviceSlip REST API
  • Leave Discard old builds, This project is parameterized, and other options at their defaults.
  • Source Code Management: None
  • Build Triggers: (none)
  • Build Environment: Enable Add timestamps to the console output (requires the Timestamp Plugin).

The image shows a Jenkins configuration page for a project titled "Generate ASCII Artwork," with options for build settings and a description mentioning the use of the Cowsay library and AdviceSlip Rest API.

The image shows a configuration page for a project, likely in a software development environment, with options for setting parameters, source code management, build triggers, and more. There are checkboxes for various settings and a section explaining parameterized builds.

The image shows a configuration screen for a software build system, with options for build triggers, environment settings, and post-build actions. The interface includes checkboxes and dropdown menus for various settings.

3. Add Build Step: Execute Shell

Choose Add build step → Execute shell and paste this script. It fetches advice, validates it, installs Cowsay, adjusts the PATH, and prints the ASCII art.

#!/bin/bash
set -e

# 1. Fetch advice from AdviceSlip API
curl -s https://api.adviceslip.com/advice > advice.json

# 2. Extract the advice text and validate word count (>5)
jq -r '.slip.advice' advice.json > advice.message
WORD_COUNT=$(wc -w < advice.message)
if [ "$WORD_COUNT" -le 5 ]; then
  echo "Advice: $(cat advice.message) has $WORD_COUNT words or less."
  exit 1
fi

# 3. Install cowsay and ensure the binary is in PATH
sudo apt-get update -y
sudo apt-get install cowsay -y
export PATH="$PATH:/usr/games:/usr/local/games"

# 4. Generate ASCII artwork
cat advice.message | cowsay -f "$(shuf -n 1 /usr/share/cowsay/cows)"

The image shows a configuration screen for a build environment, with options to execute shell commands and add timestamps to the console output. There are sections for build steps and post-build actions, with buttons to save or apply changes.

Script Breakdown

Script SectionPurpose
curl fetchDownload JSON from AdviceSlip API
jq + wcExtract text and enforce a minimum of 6 words
apt-get install cowsayInstall the Cowsay package
export PATHAdd /usr/games and /usr/local/games to the PATH
cowsay renderPipe advice into a random cowsay template to produce artwork

Sample AdviceSlip API Response

{
  "slip": {
    "id": 135,
    "advice": "If you want to be happily married, marry a happy person."
  }
}

4. Save and Run the Job

  • Click Save, then Build Now.
  • On the first run, you may see an error because the workspace doesn’t exist yet:

The image shows a Jenkins dashboard with an error message stating "Error: no workspace," indicating that a project needs a build to create a workspace. Various options like "Build Now" and "Configure" are visible on the left sidebar.

5. Investigate a Failed Build

A common failure occurs when Cowsay isn’t found in the PATH:

09:23:12 + curl -s https://api.adviceslip.com/advice
09:23:13 + sudo apt-get install cowsay -y
...
09:23:16 + cowsay -f unipony-smaller.cow
/tmp/jenkins*.sh: line 14: cowsay: not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE

Warning

The Jenkins service user’s default environment may exclude /usr/games. Always export or globally configure PATH so that Jenkins can locate installed binaries.

6. Update the Script and Rebuild

After adding the export PATH line, save and run Build Now again. Success output:

The image shows a Jenkins dashboard displaying the status of a build job titled "Generate ASCII Artwork." It includes details such as the start time, user, and duration of the build process.

14:58:24 + sudo apt-get install cowsay -y
14:58:24 + export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/snap/bin:/usr/games:/usr/local/games
14:58:24 + cat advice.message
14:58:24 + cowsay -f vader.cow
  / Some people would be better off if they    \
  | took their own advice.                    |
   -------------------------------------------
          \   ^__^
           \  (oo)\_______
              (__)\       )\/\
                  ||----w |
                  ||     ||
              Cowth Vader

Finished: SUCCESS

7. Review Build History and Workspace

You can inspect past runs, access permalinks, and browse the workspace files:

The image shows a Jenkins dashboard for a project called "Generate ASCII Artwork," displaying build history and permalinks for recent builds. The interface includes options like "Build Now," "Configure," and "Delete Project."

The image shows a Jenkins dashboard displaying the status of a build job named "Generate ASCII Artwork #2," which was completed successfully in 1.5 seconds. The build was started by a user and shows details like waiting time and build duration.

8. (Optional) Configure Global Environment Variables

To avoid repeating export PATH in every job:

  1. Go to Manage Jenkins → Configure System.
  2. Under Global properties, check Environment variables.
  3. Add:
    • Name: PATH
    • Value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/snap/bin:/usr/games:/usr/local/games

Note

When you override PATH globally, include the full default PATH. Jenkins does not expand environment variables in the value field. Alternatively, adjust /etc/default/jenkins or use the Environment Injector Plugin to append directories.

The image shows a Jenkins system configuration page where environment variables are being set, with fields for "Name" and "Value" under "Global properties."


Watch Video

Watch video content

Previous
Types of Jenkins Projects