Jenkins For Beginners
Extending Jenkins
Installing Plugins
In this tutorial, you will learn how to install several Jenkins plugins and configure jobs to incorporate their functionalities. In our previous lesson, the ASCII build job executed successfully while the ASCII test job failed because it could not access a file generated during the build. To fix this issue, we will install the Copy Artifact plugin using the Jenkins plugin system.
Navigating the Jenkins Plugin Manager
Start by clicking on "Manage Jenkins" and then "Plugins." In this section, you can add, remove, disable, or enable plugins. If updates are available for your installed plugins, they will be listed in the updates section. To view all installed plugins, click on the "Installed" tab. This interface makes it very simple to manage plugins using toggles or by clicking the corresponding icons.
Installing the Copy Artifact Plugin
To install a new plugin, switch to the "Available" plugins tab and search for "Copy Artifact."
- Find the plugin and check its box.
- Click on "Install."
- Optionally, review the plugin's documentation to learn more about its functionality.
There are two installation methods:
- Install immediately.
- Install after Jenkins restarts.
For this guide, choose to install the plugin immediately.
After scrolling down, you will see that the Copy Artifact plugin installation completes successfully. Some plugins require a Jenkins restart after installation. Always review the documentation to understand its options, including usage in declarative pipelines. For example, define the plugin in a pipeline using this Groovy snippet:
pipeline {
agent any
options {
copyArtifactPermission('job1,job2,...')
}
stages {
// Your pipeline stages here
}
}
properties {
copyArtifactPermission('job1,job2,...'),
}
node {
// Your node code here
}
The plugin documentation also details releases, installation via CLI, download options, dependencies, known issues, and the health score. To install the plugin using the CLI tool, run:
jenkins-plugin-cli --plugins copyartifact:749.vfb_dca_a_9b_6549
Configuring the Build Job
Now that the Copy Artifact plugin is installed, update the build job configuration:
- Open the ASCII build job and click "Configure."
- Scroll down to locate the new option "Permission to Copy Artifact."
Note
Set the permission to restrict artifact copying only for specific projects. For this example, enter "ASCII test job" so that only this job is allowed to copy artifacts from the build job.
- Next, configure the post-build action to archive artifacts (such as the generated advice.json file). To share the file with other jobs, use a simple script like:
curl -s https://api.adviceslip.com/advice > advice.json
cat advice.json
- Then, configure the post-build action to archive the advice.json file. You also have options to include or exclude specific files and set the post-build action to run only on successful builds. Click "Save" to update the configuration.
Updating the Test Job Configuration
To ensure the test job functions correctly:
- Update the test job configuration.
- Before executing any shell commands, add a build step that uses the Copy Artifact plugin to copy the artifact from the build job. Configure it to fetch the latest successful build artifact from the build job and specify "advice.json" as the artifact.
- Add the following shell script as a new build step to process the copied file:
# Test to ensure the advice message contains more than 5 words.
ls advice.json
cat advice.json | jq -r '.slip.advice' > advice.message
[ $(wc -w < advice.message) -gt 5 ] && echo "Advice - $(cat advice.message) has more than 5 words" || (echo "Advice - $(cat advice.message)")
Ensure that the Copy Artifact step is the first executed in the test job since subsequent steps depend on the advice.json file. Click "Save" after updating the configuration.
After saving, the test job's configuration will correctly reference the build job as its upstream project.
Configuring the Deploy Job
Next, configure the deploy job to utilize the advice message:
- Create a new Freestyle Project called "ASCII deploy job."
- Add an "Execute Shell" build step with the following commands to install the cowsay command and generate ASCII artwork:
# Deploy
sudo apt-get install cowsay -y
export PATH="$PATH:/usr/games:/usr/local/games"
cat advice.message | cowsay -f $(ls /usr/share/cowsay/cows | shuf -n 1)
Reminder
Ensure that the advice.message file is archived as a post-build artifact in the test job, so the deploy job can access it.
- Optionally, configure the deploy job to be triggered automatically after the test job completes by using the "Build after other projects are built" option, specifying "ASCII test job" as the upstream project and triggering only on stable upstream builds.
When you run the build job, it will trigger the test job which, in turn, will automatically trigger the deploy job upon a successful build.
Review the deploy job console output; you should see messages indicating that:
- The deploy job was started by the test job.
- The artifact (advice.json) was copied successfully.
- The deploy job installed cowsay, read the advice message, and generated ASCII art using a randomly selected cow file.
Example console output excerpt:
Started by upstream project "ascii-test-job" build number 3
originally caused by:
Started by upstream project "ascii-build-job" build number 4
originally caused by:
Started by user Dasher Admin
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/ascii-deploy-job
Copied 1 artifact from "ascii-test-job" build number 3
[ascii-deploy-job] $ /bin/sh -xe /tmp/jenkins8625965086975148801.sh
+ sudo apt-get install cowsay -y
Reading package lists...
Building dependency tree...
Reading state information...
cowsay is already the newest version (3.03+dfsg-8).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
+ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/snap/bin:/usr/games:/usr/local/games
+ cat advice.message
+ ls /usr/share/cowsay/cows
+ shuf -n 1
+ cowsay -f elephant-in-snake.cow
...
Installing Plugins via HPI Files
Another method for installing Jenkins plugins is by using an HPI file. This may be particularly useful when installing plugins such as "Yet Another Build Visualizer," which provides clear visual representations of chained projects.
To install a plugin using an HPI file:
- Navigate to the plugin's documentation page.
- Click the "Download direct link" to save the HPI file (for example, yet-another-build-visualizer.hpi).
- In the "Advanced" tab of the plugin manager, upload the HPI file and click "Deploy."
Alternatively, install via the CLI using:
jenkins-plugin-cli --plugins yet-another-build-visualizer:1.16
Once installed, navigate to any chained project (for example, the build job) and view the visualized job relationships, including build numbers, statuses, and full build histories.
Conclusion
By following these detailed steps, you can install and configure Jenkins plugins to enable artifact sharing between jobs and create visual job chains. This setup enhances your continuous integration process and simplifies troubleshooting across interconnected jobs.
Happy building with Jenkins!
Watch Video
Watch video content