> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Working with Freestyle Job

> Learn to set up a Jenkins Freestyle project that fetches advice from an API and renders it as ASCII art using the Cowsay library.

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](https://github.com/shiena/cowsay). 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**.

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870854/notes-assets/images/Certified-Jenkins-Engineer-Demo-Working-with-Freestyle-Job/jenkins-dashboard-new-item-projects.jpg)
</Frame>

## 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](https://plugins.jenkins.io/timestamper/)).

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870855/notes-assets/images/Certified-Jenkins-Engineer-Demo-Working-with-Freestyle-Job/jenkins-generate-ascii-artwork-config.jpg)
</Frame>

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870857/notes-assets/images/Certified-Jenkins-Engineer-Demo-Working-with-Freestyle-Job/project-configuration-settings-diagram.jpg)
</Frame>

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870857/notes-assets/images/Certified-Jenkins-Engineer-Demo-Working-with-Freestyle-Job/software-build-system-configuration.jpg)
</Frame>

## 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.

```bash theme={null}
#!/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)"
```

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870859/notes-assets/images/Certified-Jenkins-Engineer-Demo-Working-with-Freestyle-Job/build-environment-configuration-screen.jpg)
</Frame>

### Script Breakdown

| Script Section           | Purpose                                                      |
| ------------------------ | ------------------------------------------------------------ |
| `curl` fetch             | Download JSON from AdviceSlip API                            |
| `jq` + `wc`              | Extract text and enforce a minimum of 6 words                |
| `apt-get install cowsay` | Install the Cowsay package                                   |
| `export PATH`            | Add `/usr/games` and `/usr/local/games` to the PATH          |
| `cowsay` render          | Pipe advice into a random cowsay template to produce artwork |

### Sample AdviceSlip API Response

```json theme={null}
{
  "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:

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870859/notes-assets/images/Certified-Jenkins-Engineer-Demo-Working-with-Freestyle-Job/jenkins-dashboard-error-workspace.jpg)
</Frame>

## 5. Investigate a Failed Build

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

```console theme={null}
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
```

<Callout icon="triangle-alert" color="#FF6B6B">
  The Jenkins service user’s default environment may exclude `/usr/games`. Always export or globally configure PATH so that Jenkins can locate installed binaries.
</Callout>

## 6. Update the Script and Rebuild

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

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870861/notes-assets/images/Certified-Jenkins-Engineer-Demo-Working-with-Freestyle-Job/jenkins-dashboard-generate-ascii-artwork.jpg)
</Frame>

```console theme={null}
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:

<Frame>
  ![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."](https://kodekloud.com/kk-media/image/upload/v1752870862/notes-assets/images/Certified-Jenkins-Engineer-Demo-Working-with-Freestyle-Job/jenkins-dashboard-generate-ascii-artwork-2.jpg)
</Frame>

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752870864/notes-assets/images/Certified-Jenkins-Engineer-Demo-Working-with-Freestyle-Job/jenkins-dashboard-generate-ascii-artwork-3.jpg)
</Frame>

## 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`

<Callout icon="lightbulb" color="#1CB2FE">
  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](https://plugins.jenkins.io/envinject/) to append directories.
</Callout>

<Frame>
  ![The image shows a Jenkins system configuration page where environment variables are being set, with fields for "Name" and "Value" under "Global properties."](https://kodekloud.com/kk-media/image/upload/v1752870866/notes-assets/images/Certified-Jenkins-Engineer-Demo-Working-with-Freestyle-Job/jenkins-system-configuration-environment-variables.jpg)
</Frame>

***

## Links and References

* [Jenkins Freestyle Project](https://www.jenkins.io/doc/book/managing/projects/)
* [AdviceSlip REST API](https://api.adviceslip.com)
* [Cowsay GitHub Repository](https://github.com/shiena/cowsay)
* [Timestamp Plugin](https://plugins.jenkins.io/timestamper/)
* [Environment Injector Plugin](https://plugins.jenkins.io/envinject/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/certified-jenkins-engineer/module/7ab00946-0edd-4a13-b5c8-1b5001779f1c/lesson/a0827f66-dfb3-4e34-a723-5bc7c82e88fc" />
</CardGroup>
