There are many ways to test Jenkins...
Jenkins Pipeline Unit is great but it's generally recommended to keep all of your logic with stages and not get too crazy with custom Groovy in your pipeline. Though, it's a great option if you have a very advanced use-case and want to make sure your code is reliable.
The Replay Pipeline Run Option is awesome if you want to verify a quick change or iterate quickly on a previously setup pipeline. But it only allows for altering Jenkinsfile Code and runs against the production Jenkins Instance.
Sometimes it helps to have a playground where you can execute things without fear of hecking everything up. This option actually has a fairly lightweight setup and you can practice being your very own Jenkins admin.
This gist will go into how to set up your own local Jenkins server, the setup of a few dummy builds, and then point you in the direction of the Jenkins Script Console so you can copy Groovy scripts into your leisure and see howthey affect your Jenkins Server, Configuration, and Builds.
-
Install Jenkins with
brew install jenkins
(If you don't have Brew, Download it) -
Run Jenkins with
jenkins
in your terminal and navigate tohttp://localhost:8080/
in your web browser -
Go through Onboarding of Jenkins (Can leave at default
localhost:8080
) -
In Terminal:
cat ~/.jenkins/secrets/initialAdminPassword
(Make sure to save this admin password somewhere) -
Auto install suggested plugins Can install any extra needed plugins later
-
Create Jobs!
- New Item
- Pipeline
- Paste pipeline code in Pipeline Script section
- Pipeline
I created 3 Pipelines with a * * * * *
cron trigger to build once a minute, so that I could queue up a lot of builds, and cap out my running executors.
node {
echo 'Sleeping for 5 minutes'
sleep time: 5, unit: 'MINUTES'
echo 'Finished Sleeping'
}
Adding to and augmenting Pipeline Script here was a great way for me to test out this method:
@NonCPS // See: https://jenkins.io/blog/2017/02/01/pipeline-scalability-best-practice/#best-practices-for-Pipeline-code
def cancelPreviousBuilds() {
// Check for other instances of this particular build, cancel any that are older than the current one
def jobName = env.JOB_NAME
def currentBuildNumber = env.BUILD_NUMBER.toInteger()
def currentJob = Jenkins.instance.getItemByFullName(jobName)
// Loop through all instances of this particular job/branch
for (def build : currentJob.builds) {
if (build.isBuilding() && (build.number.toInteger() < currentBuildNumber)) {
echo "Older build still queued. Sending kill signal to build number: ${build.number}"
build.doStop()
}
}
}
(Note: Keep this particular script outside of a node block, or else it will use a heavyweight executor and not execute whilst in the queue)
(Note: You may want to download the Groovy plugin if trying to execute Groovy as a build step within a node)
This setup is a great way to test out scripts without fear of altering your production Jenkins instance.
By going to Manage Jenkins -> Script Console, you can execute Groovy scripts in the console there.
Try this to kill all queued builds:
import hudson.model.*
def q = Jenkins.instance.queue
q.items.each { q.cancel(it.task) }
Check out these repos for more scripts: