Skip to content

Instantly share code, notes, and snippets.

@mrk-han
Last active June 24, 2024 06:45
Show Gist options
  • Select an option

  • Save mrk-han/f8af09469feebf33f4f683d1e74eea9d to your computer and use it in GitHub Desktop.

Select an option

Save mrk-han/f8af09469feebf33f4f683d1e74eea9d to your computer and use it in GitHub Desktop.

Revisions

  1. Mark Han revised this gist Jan 9, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ### Test a Local Jenkins Instance on MacOS: Download, Setup, and Run Groovy Scripts to
    ### Test a Local Jenkins Instance on MacOS: Download, Setup, and Run Groovy Scripts Locally

    ## About

  2. Mark Han revised this gist Jan 9, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ### Download, Setup, and Run Groovy Scripts to Test a Local Jenkins Instance on MacOS
    ### Test a Local Jenkins Instance on MacOS: Download, Setup, and Run Groovy Scripts to

    ## About

  3. Mark Han renamed this gist Oct 3, 2019. 1 changed file with 0 additions and 0 deletions.
  4. Mark Han revised this gist Oct 2, 2019. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions Jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -88,13 +88,10 @@ Check out these repos for more scripts:
    - [CloudBees jenkins-scripts][cb].
    - [JenkinsCI jenkins-scripts][js].
    - [samrocketman's jenkins-bootstrap-shared contains script console scripts in `scripts/`][jbs]
    - [samrocketman's sandscape contains script console scripts][ss]


    [cb]: https://github.com/cloudbees/jenkins-scripts
    [jbs]: https://github.com/samrocketman/jenkins-bootstrap-shared
    [js]: https://github.com/jenkinsci/jenkins-scripts
    [sc]: https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console
    [ss]: https://github.com/sandscape/sandscape
    [jccs]: https://github.com/samrocketman/jenkins-script-console-scripts

  5. Mark Han revised this gist Oct 2, 2019. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions Jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -82,5 +82,19 @@ def q = Jenkins.instance.queue
    q.items.each { q.cancel(it.task) }
    ```

    Check out these repos for more scripts:

    - [samrocketman's jenkins script console scripts](jccs).
    - [CloudBees jenkins-scripts][cb].
    - [JenkinsCI jenkins-scripts][js].
    - [samrocketman's jenkins-bootstrap-shared contains script console scripts in `scripts/`][jbs]
    - [samrocketman's sandscape contains script console scripts][ss]


    [cb]: https://github.com/cloudbees/jenkins-scripts
    [jbs]: https://github.com/samrocketman/jenkins-bootstrap-shared
    [js]: https://github.com/jenkinsci/jenkins-scripts
    [sc]: https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console
    [ss]: https://github.com/sandscape/sandscape
    [jccs]: https://github.com/samrocketman/jenkins-script-console-scripts

  6. Mark Han revised this gist Oct 2, 2019. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions Jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -21,8 +21,7 @@ This gist will go into how to set up your own local Jenkins server, the setup of
    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
    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
  7. Mark Han revised this gist Oct 2, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ This gist will go into how to set up your own local Jenkins server, the setup of
    3) Go through Onboarding of Jenkins (Can leave at default `localhost:8080`)


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

    5) Auto install suggested plugins
  8. Mark Han revised this gist Oct 2, 2019. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions Jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -18,8 +18,7 @@ This gist will go into how to set up your own local Jenkins server, the setup of

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

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


    4) `cat ~/.jenkins/secrets/initialAdminPassword`
  9. Mark Han revised this gist Oct 2, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ This gist will go into how to set up your own local Jenkins server, the setup of

    1) Install Jenkins with `brew install jenkins` (If you don't have Brew, [Download it](https://brew.sh/))

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

    3) Go through Setup of Jenkins
    Can leave at default localhost:8080
  10. Mark Han revised this gist Oct 2, 2019. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions Jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -14,9 +14,7 @@ This gist will go into how to set up your own local Jenkins server, the setup of

    ## Steps

    1) Install Jenkins with `brew install jenkins`

    If you don't have brew, download it here: `/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"`
    1) Install Jenkins with `brew install jenkins` (If you don't have Brew, [Download it](https://brew.sh/))

    2) Run Jenkins with `jenkins` in your terminal

  11. Mark Han revised this gist Oct 2, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ This gist will go into how to set up your own local Jenkins server, the setup of

    1) Install Jenkins with `brew install jenkins`

    If you don't have brew, download it here: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    If you don't have brew, download it here: `/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"`

    2) Run Jenkins with `jenkins` in your terminal

  12. Mark Han revised this gist Oct 2, 2019. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions Jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -10,8 +10,6 @@ There are many ways to test Jenkins...

    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.

    - Setting up a local instance, throwing up "dummy" builds, and running scripts on localhost.

    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
  13. Mark Han revised this gist Oct 2, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,9 @@

    There are many ways to test Jenkins...

    [Jenkins Pipeline Unit is great](https://github.com/jenkinsci/JenkinsPipelineUnit), 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.
    [Jenkins Pipeline Unit is great](https://github.com/jenkinsci/JenkinsPipelineUnit) 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](https://jenkins.io/doc/book/pipeline/running-pipelines/#replay) 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.
    [The Replay Pipeline Run Option](https://jenkins.io/doc/book/pipeline/running-pipelines/#replay) 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.

  14. Mark Han revised this gist Oct 2, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,9 @@

    There are many ways to test Jenkins...

    (https://github.com/jenkinsci/JenkinsPipelineUnit)[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.
    [Jenkins Pipeline Unit is great](https://github.com/jenkinsci/JenkinsPipelineUnit), 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.

    (https://jenkins.io/doc/book/pipeline/running-pipelines/#replay)[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.
    [The Replay Pipeline Run Option](https://jenkins.io/doc/book/pipeline/running-pipelines/#replay) 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.

  15. Mark Han revised this gist Oct 2, 2019. 1 changed file with 10 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions Jenkins-playground-setup.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,18 @@
    ### Download, Setup, and Run Groovy Scripts to Test a Local Jenkins Instance on MacOS

    ## Ways to test Jenkins
    ## About

    There are many ways to test Jenkins...

    (https://github.com/jenkinsci/JenkinsPipelineUnit)[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.

    (https://jenkins.io/doc/book/pipeline/running-pipelines/#replay)[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.

    - Plugins such as Jenkins Pipeline Unit https://github.com/jenkinsci/JenkinsPipelineUnit ,
    - Replay step to rebuild a build while altering the Jenkinsfile code
    - Setting up a local instance, throwing up "dummy" builds, and running scripts on localhost.

    This gist will go into a rough how-to set up your own local Jenkins server to get into a state where you can copy Groovy scripts into your Jenkins script console to see how they affect your Jenkins Server, Configuration, and Builds.
    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

  16. Mark Han renamed this gist Oct 2, 2019. 1 changed file with 0 additions and 0 deletions.
  17. Mark Han revised this gist Oct 2, 2019. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions jenkins-local-instance-setup.md
    Original file line number Diff line number Diff line change
    @@ -10,11 +10,11 @@ This gist will go into a rough how-to set up your own local Jenkins server to ge

    ## Steps

    1) Install Jenkins
    `brew install jenkins`
    1) Install Jenkins with `brew install jenkins`

    2) Run Jenkins
    `jenkins`
    If you don't have brew, download it here: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    2) Run Jenkins with `jenkins` in your terminal

    3) Go through Setup of Jenkins
    Can leave at default localhost:8080
  18. Mark Han revised this gist Oct 2, 2019. No changes.
  19. Mark Han revised this gist Oct 2, 2019. 1 changed file with 11 additions and 9 deletions.
    20 changes: 11 additions & 9 deletions jenkins-local-instance-setup.md
    Original file line number Diff line number Diff line change
    @@ -10,29 +10,30 @@ This gist will go into a rough how-to set up your own local Jenkins server to ge

    ## Steps

    Install Jenkins
    1) Install Jenkins
    `brew install jenkins`

    Run Jenkins
    2) Run Jenkins
    `jenkins`

    Go through Setup of Jenkins
    3) Go through Setup of Jenkins
    Can leave at default localhost:8080


    `cat ~/.jenkins/secrets/initialAdminPassword`
    Save admin password somewhere

    Auto install suggested plugins
    4) `cat ~/.jenkins/secrets/initialAdminPassword`
    Make sure to save this admin password somewhere

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

    Create Jobs!
    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'
    @@ -61,14 +62,15 @@ def cancelPreviousBuilds() {
    }
    ```
    (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.
    By going to **Manage Jenkins -> Script Console**, you can execute Groovy scripts in the console there.

    Try this to kill all queued builds:

  20. Mark Han renamed this gist Oct 2, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  21. Mark Han created this gist Oct 2, 2019.
    84 changes: 84 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    ### Download, Setup, and Run Groovy Scripts to Test a Local Jenkins Instance on MacOS

    ## Ways to test Jenkins

    - Plugins such as Jenkins Pipeline Unit https://github.com/jenkinsci/JenkinsPipelineUnit ,
    - Replay step to rebuild a build while altering the Jenkinsfile code
    - Setting up a local instance, throwing up "dummy" builds, and running scripts on localhost.

    This gist will go into a rough how-to set up your own local Jenkins server to get into a state where you can copy Groovy scripts into your Jenkins script console to see how they affect your Jenkins Server, Configuration, and Builds.

    ## Steps

    Install Jenkins
    `brew install jenkins`

    Run Jenkins
    `jenkins`

    Go through Setup of Jenkins
    Can leave at default localhost:8080


    `cat ~/.jenkins/secrets/initialAdminPassword`
    Save 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

    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) }
    ```