A Jenkins job to run a Python script that accesses the Dad Jokes API.
- Jenkins Installed: Ensure you have Jenkins installed and running on your machine or server.
- Python Installed: Ensure Python is installed on the Jenkins server.
- Dad Jokes API Access: You will be using a public API, so no special authentication is required.
- Python Plugin: Install the Python plugin (if necessary) to ensure Jenkins can run Python scripts. Go to
Manage Jenkins->Manage Plugins->Availableand search for "Python Plugin" to install it.
-
Create a Script: Write a Python script to access the Dad Jokes API. Save the script in a repository or on your Jenkins server. Below is an example script:
import requests def get_dad_joke(): url = "https://icanhazdadjoke.com/" headers = {"Accept": "application/json"} response = requests.get(url, headers=headers) if response.status_code == 200: joke = response.json().get("joke") print(f"Dad Joke: {joke}") else: print("Failed to fetch a dad joke.") if __name__ == "__main__": get_dad_joke()
-
Dependencies: If you use external libraries (like
requests), ensure they are listed in arequirements.txtfile.
-
Create a New Job:
- Go to Jenkins Dashboard.
- Click on
New Item. - Enter an item name (e.g.,
DadJokesJob). - Select
Freestyle project. - Click
OK.
-
Configure the Job:
- Source Code Management:
- If your script is in a version control system (e.g., Git), configure the repository URL and credentials.
- Build Environment:
- Add a build step to execute a shell command.
- Use a shell script to set up the environment and run your Python script. Example:
#!/bin/bash # Navigate to the workspace directory cd $WORKSPACE # Create a virtual environment in the workspace python3 -m venv venv # Activate virtual environment source venv/bin/activate # Install dependencies pip install -r requirements.txt # Run the Python script python /path/to/your_script.py # Deactivate the virtual environment deactivate
- Post-build Actions:
- You can add notifications or other post-build actions if desired.
- Source Code Management:
-
Save and Run:
- Click
Saveto save your job configuration. - Run the job manually by clicking
Build Nowon the job page.
- Click
- Check Console Output:
- After the job runs, click on the build number in the
Build History. - Click
Console Outputto see the results of your Python script execution, including the Dad Joke fetched from the API.
- After the job runs, click on the build number in the
- Automate Builds:
- Set up build triggers if you want to automate the execution, such as running the job at regular intervals.
- Monitor:
- Use Jenkins monitoring tools and notifications to monitor job statuses and receive alerts for failures.
By following these steps, you will have a Jenkins job that can run a Python script to fetch and display a Dad Joke from the API. Adjust paths and configurations according to your specific setup and environment.
External libraries (like requests), are listed in a requirements.txt file.