Skip to content

Commit

Permalink
groovy to list all jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
dennyzhang committed May 8, 2019
1 parent 135cf3e commit 2f09d5c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
25 changes: 12 additions & 13 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ File me [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/issues][Iss
** Jenkins Job Via Groovy
| Name | Comment |
|-----------------------------------------+---------------------------------------------------------|
| List all jenkins jobs | =println Jenkins.instance.projects.collect { it.name }= |
| List all my jenkins jobs | =println Jenkins.instance.projects.collect { it.name }= |
| List all jenkins jobs | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/list-all-jobs.groovy][list-all-jobs.groovy]] |
| Add a list of jobs by regexp to a view | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/addjobstoview-byregexp.groovy][addjobstoview-byregexp.groovy]] |
| Create jenkins views and add jobs to it | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/jenkins-views.groovy][jenkins-views.groovy]] |
| Create and trigger a job | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/create-jenkins-job.groovy][create-jenkins-job.groovy]] |
Expand Down Expand Up @@ -89,6 +90,9 @@ File me [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/issues][Iss
| [[https://wiki.jenkins-ci.org/display/JENKINS/thinBackup][ThinBackup Plugin]] | Backup jenkins |
| [[https://plugins.jenkins.io/jobConfigHistory][JobConfigHistory Plugin]] | Backup job configuration |
| [[https://wiki.jenkins.io/display/JENKINS/Build+User+Vars+Plugin][Build User Vars Plugin]] | Describe the user who started the build |
#+BEGIN_HTML
<a href="https://www.dennyzhang.com"><img align="right" width="185" height="37" src="https://raw.githubusercontent.com/USDevOps/mywechat-slack-group/master/images/dns_small.png"></a>
#+END_HTML
** Jenkins Git Via Groovy
| Name | Comment |
|----------------------------------------+-----------------------------------|
Expand All @@ -97,13 +101,12 @@ File me [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/issues][Iss
| List git tags and branches | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/git-list-tags-and-branches.groovy][git-list-tags-and-branches.groovy]] |

** Jenkins networking Via Groovy
| Name | Comment |
|----------------+---------------------------------------------------|
| Get hostname | =println InetAddress.localHost.canonicalHostName= |
| Get IP address | =println InetAddress.localHost.hostAddress= |
#+BEGIN_HTML
<a href="https://www.dennyzhang.com"><img align="right" width="185" height="37" src="https://raw.githubusercontent.com/USDevOps/mywechat-slack-group/master/images/dns_small.png"></a>
#+END_HTML
| Name | Comment |
|---------------------------------+--------------------------------------------------------------------------|
| Get hostname | =println InetAddress.localHost.canonicalHostName= |
| Get IP address | =println InetAddress.localHost.hostAddress= |
| Get hostname by ip | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/get-ip-by-hostname.groovy][get-ip-by-hostname.groovy]] |
| validate user input: ip address | =assert ip_address.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")= |
** Jenkins with Kubernetes/Docker
| Name | Comment |
|----------------------------------------------------+-------------------------------------------------------------------------|
Expand Down Expand Up @@ -157,11 +160,6 @@ File me [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/issues][Iss
| [[http://groovy-lang.org/json.html][Convert string to json]] | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/string-to-json.groovy][string-to-json.groovy]] |
| Convert dictionary to json | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/dict-to-json.groovy][dict-to-json.groovy]] |
| Read and write json files | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/json-file.groovy][json-file.groovy]] |
** Groovy Network
| Name | Comment |
|---------------------------------+--------------------------------------------------------------------------|
| Get hostname by ip | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/get-ip-by-hostname.groovy][get-ip-by-hostname.groovy]] |
| validate user input: ip address | =assert ip_address.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")= |
#+BEGIN_HTML
<a href="https://www.dennyzhang.com"><img align="right" width="185" height="37" src="https://raw.githubusercontent.com/USDevOps/mywechat-slack-group/master/images/dns_small.png"></a>
#+END_HTML
Expand All @@ -176,6 +174,7 @@ File me [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/issues][Iss
|-------------------------------------+--------------------------------|
| Check jenkins slave jar version | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/check-slave-jar-version.groovy][check-slave-jar-version.groovy]] |
| Find dead executors and remove them | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/find-dead-executors.groovy][find-dead-executors.groovy]] |
| Set env for each agent | [[https://github.com/dennyzhang/cheatsheet-jenkins-groovy-A4/blob/master/set-agent-env.groovy][set-agent-env.groovy]] |
** Jenkins Maintenance
| Name | Comment |
|---------------------------------+---------------------------------------------------------|
Expand Down
Binary file modified cheatsheet-jenkins-groovy-A4.pdf
Binary file not shown.
19 changes: 19 additions & 0 deletions list-all-jobs.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!groovy
//-------------------------------------------------------------------
// @copyright 2018 DennyZhang.com
// Licensed under MIT
// https://www.dennyzhang.com/wp-content/mit_license.txt
//
// File: list-all-jobs.groovy
// Author : Denny <https://www.dennyzhang.com/contact>
// Link: https://cheatsheet.dennyzhang.com/cheatsheet-jenkins-groovy-a4
// --
// Created : <2018-04-20>
// Updated: Time-stamp: <2019-05-07 14:14:04>
//-------------------------------------------------------------------
// http://paweloczadly.github.io/dev/2014/07/03/jenkins-groovy-script-list-all-jobs
import hudson.model.*

hudson.model.Hudson.instance.items.findAll{job -> job}.each {
job -> println("Job: " + job.name)
}
20 changes: 20 additions & 0 deletions set-agent-env.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!groovy
//-------------------------------------------------------------------
// @copyright 2018 DennyZhang.com
// Licensed under MIT
// https://www.dennyzhang.com/wp-content/mit_license.txt
//
// File: set-agent-env.groovy
// Author : Denny <https://www.dennyzhang.com/contact>
// Link: https://cheatsheet.dennyzhang.com/cheatsheet-jenkins-groovy-a4
// --
// Created : <2018-04-20>
// Updated: Time-stamp: <2019-05-07 14:07:50>
//-------------------------------------------------------------------
// http://paweloczadly.github.io/dev/2014/07/03/jenkins-groovy-script-set-up-java_home-on-each-agent
import hudson.slaves.*

def javaHome = new EnvironmentVariablesNodeProperty(
new EnvironmentVariablesNodeProperty.Entry('JAVA_HOME', '/usr/lib/jvm/java-7-oracle-amd64'))

hudson.model.Hudson.instance.slaves.each { it.nodeProperties.add(javaHome) }

0 comments on commit 2f09d5c

Please sign in to comment.