Skip to content

Instantly share code, notes, and snippets.

@mrk-han
Last active August 18, 2023 02:14
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrk-han/f8af09469feebf33f4f683d1e74eea9d to your computer and use it in GitHub Desktop.
Save mrk-han/f8af09469feebf33f4f683d1e74eea9d to your computer and use it in GitHub Desktop.
Setting up a Jenkins Playground: Download, Setup, Test and Run Groovy Scripts on a Local Jenkins Server Instance with MacOS

Test a Local Jenkins Instance on MacOS: Download, Setup, and Run Groovy Scripts Locally

About

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.

Steps

  1. Install Jenkins with brew install jenkins (If you don't have Brew, Download it)

  2. Run Jenkins with jenkins in your terminal and navigate to http://localhost:8080/ in your web browser

  3. Go through Onboarding of Jenkins (Can leave at default localhost:8080)

  4. In Terminal: cat ~/.jenkins/secrets/initialAdminPassword (Make sure to save this admin password somewhere)

  5. Auto install suggested plugins Can install any extra needed plugins later

  6. Create Jobs!

  • New Item
    • Pipeline
      • Paste pipeline code in Pipeline Script section

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)

Using Script Console

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment