book
  • README
  • cheatsheet
    • bash
      • builtin
      • syntactic sugar
      • cmd
      • havefun
    • text-processing
      • awk
      • sed
      • html
      • json
      • regex
      • unicode
    • osx
    • curl
    • tricky
    • widget
    • proxy
    • colors
    • math
    • media
    • ssl
      • keystore
      • verification
      • server
      • client
      • tricky
    • windows
      • powershell
      • choco
      • wsl
      • wt
      • shortcut
      • clsid
      • env
      • shell:folder
  • vim
    • nvim
    • install
    • color
    • plugins
      • usage
      • other plugins
      • deprecated
    • tricky
    • viml
    • windows
    • troubleshooting
  • devops
    • admin tools
    • ssh
    • git
      • config
      • alias
      • submodule
      • eol
      • example
      • gerrit
        • gerrit API
      • github
      • troubleshooting
      • tricky
      • statistics
    • pre-commit
    • release-tools
    • tmux
      • cheatsheet
    • ansible
    • vault
    • artifactory
      • api
      • cli
      • aql
      • nginx cert
    • klocwork
      • kwadmin
      • kwserver
      • api
      • q&a
    • elk
    • mongodb
    • android
    • mobile
  • jenkins
    • config
      • windows
    • appearance
    • troubleshooting
    • jenkinsfile
      • utility
      • parallel
      • build
      • envvar
      • properties
      • trigger
      • node
    • script
      • job
      • build
      • stage
      • agent
      • security & authorization
      • exception
      • monitor
      • tricky
    • api
      • blueocean
    • cli
    • plugins
      • kubernetes
      • docker
      • shared-libs
      • lockable-resource
      • ansicolor
      • badge
      • groovy-postbuild
      • simple-theme
      • customizable-header
      • artifactory
      • jira-steps
      • job-dsl
      • build-timeline
      • crumbIssuer
      • coverage
      • uno-choice
      • tricky
  • virtualization
    • kubernetes
      • init
        • kubespray
        • kubeadm
          • environment
          • crio v1.30.4
          • docker v1.15.3
          • HA
        • addons
        • etcd
      • kubectl
        • pod
        • deploy
        • replicasets
        • namespace
        • secrets
      • node
      • certificates
      • events
      • kubeconfig
      • kubelet
      • troubleshooting
      • cheatsheet
      • auth
      • api
      • tools
        • monitor
        • helm
        • network
        • minikube
    • docker
      • run & exec
      • voume
      • remove
      • show info
      • dockerfile
      • dockerd
      • tricky
      • troubleshooting
      • windows
    • crio
    • podman
  • ai
    • prompt
  • osx
    • apps
      • init
      • brew
    • defaults
    • system
    • network
    • script
    • tricky
  • linux
    • devenv
    • util
      • time & date
      • output formatting
      • params
      • tricky
    • nutshell
    • disk
    • network
    • troubleshooting
    • system
      • apt/yum/snap
      • authorization
      • apps
      • x11
    • ubuntu
      • systemctl
      • x
    • rpi
  • programming
    • groovy
    • python
      • config
      • basic
      • list
      • pip
      • q&a
    • others
    • archive
      • angular
      • maven
      • mysql
        • installation
        • logs
      • ruby
        • rubyInstallationQ&A
  • tools
    • fonts
    • html & css
    • Jira & Confluence
    • node & npm
      • gitbook
      • hexo
      • github.page
      • code themes
    • app
      • microsoft office
      • vscode
      • virtualbox
      • iterm2
      • browser
      • skype
      • teamviewer
      • others
  • quotes
  • english
Powered by GitBook
On this page
  • DSL
  • jobs
  • agents
  • others

Was this helpful?

  1. jenkins
  2. script

tricky

PreviousmonitorNextapi

Last updated 7 months ago

Was this helpful?

references:

DSL

class C implements Serializable {
 def stuff(steps) {
   steps.node {
     steps.sh 'echo hello'
   }
 }
}
def c = new C()
c.stuff(steps)
  • or

    class C implements Serializable {
      def stuff(script) {
        script.node {
          script.echo "running in ${script.env.JENKINS_URL}"
        }
      }
    }
    def c = new C()
    c.stuff(this)

jobs

import hudson.model.*
import jenkins.model.*
import hudson.slaves.*
import hudson.slaves.EnvironmentVariablesNodeProperty.Entry

/**
 * INSERT "Launch Method" SNIPPET HERE
**/
// define a "Permanent Agent"
Slave agent = new DumbSlave (
                              "agent-node",
                              "/home/jenkins",
                              launcher
                            )
agent.nodeDescription   = "Agent node description"
agent.numExecutors      = 1
agent.labelString       = "agent-node-label"
agent.mode              = Node.Mode.NORMAL
agent.retentionStrategy = new RetentionStrategy.Always()

List<Entry> env = new ArrayList<Entry>()
env.add( new Entry('key1','value1') )
env.add( new Entry('key2','value2') )
EnvironmentVariablesNodeProperty envPro = new EnvironmentVariablesNodeProperty(env)

agent.getNodeProperties().add( envPro )

// create a "Permanent Agent"
jenkins.model.Jenkins.instance.addNode( agent )

return "Node has been created successfully."
  • import com.cloudbees.jenkins.plugins.sshslaves.verification.*
    import com.cloudbees.jenkins.plugins.sshslaves.SSHConnectionDetails
    
    // Pick one of the strategies from the comments below this line
    ServerKeyVerificationStrategy serverKeyVerificationStrategy = new TrustInitialConnectionVerificationStrategy(false)
    // = new TrustInitialConnectionVerificationStrategy(false /* "Require manual verification of initial connection" */) // "Manually trusted key verification Strategy"
    // = new ManuallyConnectionVerificationStrategy("<your-key-here>")            // "Manually provided key verification Strategy"
    // = new KnownHostsConnectionVerificationStrategy()                           // "~/.ssh/known_hosts file Verification Strategy"
    // = new BlindTrustConnectionVerificationStrategy()                           // "Non-verifying Verification Strategy"
    
    // define a "Launch method": "Launch agents via SSH"
    ComputerLauncher launcher = new com.cloudbees.jenkins.plugins.sshslaves.SSHLauncher(
            "host",                                                               // Host
            new SSHConnectionDetails(
                    "credentialsId",                                              // Credentials ID
                    22,                                                           // port
                    (String)null,                                                 // JavaPath
                    (String)null,                                                 // JVM Options
                    (String)null,                                                 // Prefix Start Agent Command
                    (String)null,                                                 // Suffix Start Agent Command
                    (boolean)false,                                               // Log environment on initial connect
                    (ServerKeyVerificationStrategy) serverKeyVerificationStrategy // Host Key Verification Strategy
            )
    )

agents

import hudson.node_monitors.*
import hudson.slaves.*
import java.util.concurrent.*

jenkins = jenkins.model.Jenkins.instance

import javax.mail.internet.*
import javax.mail.*
import javax.activation.*

def sendMail ( agent, cause ) {
  message     = agent + " agent is down. Check http://JENKINS_HOSTNAME:JENKINS_PORT/computer/" + agent + "\nBecause " + cause
  subject     = agent + " agent is offline"
  toAddress   = "JENKINS_ADMIN@YOUR_DOMAIN"
  fromAddress = "JENKINS@YOUR_DOMAIN"
  host        = "SMTP_SERVER"
  port        = "SMTP_PORT"

  Properties mprops = new Properties()
  mprops.setProperty( "mail.transport.protocol", "smtp" )
  mprops.setProperty( "mail.host", host                 )
  mprops.setProperty( "mail.smtp.port", port            )

  Session lSession = Session.getDefaultInstance(mprops,null)
  MimeMessage msg  = new MimeMessage( lSession )

  //tokenize out the recipients in case they came in as a list
  StringTokenizer tok = new StringTokenizer(toAddress,";")
  ArrayList emailTos  = new ArrayList()
  while( tok.hasMoreElements() ) {
    emailTos.add( new InternetAddress(tok.nextElement().toString()) )
  }
  InternetAddress[] to = new InternetAddress[emailTos.size()]
  to = (InternetAddress[]) emailTos.toArray(to)
  msg.setRecipients(MimeMessage.RecipientType.TO,to)
  InternetAddress fromAddr = new InternetAddress(fromAddress)
  msg.setFrom( fromAddr )
  msg.setFrom( new InternetAddress(fromAddress) )
  msg.setSubject( subject )
  msg.setText( message )

  Transport transporter = lSession.getTransport("smtp")
  transporter.connect()
  transporter.send( msg )
}

def getEnviron( computer ) {
  def env
  def thread = Thread.start( "Getting env from ${computer.name}", { env = computer.environment } )
  thread.join(2000)
  if ( thread.isAlive() ) thread.interrupt()
  env
}

def agentAccessible( computer ) {
  getEnviron(computer)?.get('PATH') != null
}

def numberOfflineNodes = 0
def numberNodes        = 0
for ( agent in jenkins.getNodes() ) {
  def computer = agent.computer
  numberNodes ++
  println "\nChecking computer ${computer.name}:"
  def isOK = ( agentAccessible(computer) && !computer.offline )
  if ( isOK ) {
    println "\t\tOK, got PATH back from slave ${computer.name}."
    println( '\tcomputer.isOffline: '            + slave.getComputer().isOffline()            )
    println( '\tcomputer.isTemporarilyOffline: ' + slave.getComputer().isTemporarilyOffline() )
    println( '\tcomputer.getOfflineCause: '      + slave.getComputer().getOfflineCause()      )
    println( '\tcomputer.offline: '              + computer.offline                           )
  } else {
    numberOfflineNodes ++
    println "  ERROR: can't get PATH from agent ${computer.name}."
    println( '\tcomputer.isOffline: '            + agent.getComputer().isOffline()            )
    println( '\tcomputer.isTemporarilyOffline: ' + agent.getComputer().isTemporarilyOffline() )
    println( '\tcomputer.getOfflineCause: '      + agent.getComputer().getOfflineCause()      )
    println( '\tcomputer.offline: '              + computer.offline                           )
    sendMail( computer.name, agent.getComputer().getOfflineCause().toString() )
    if ( agent.getComputer().isTemporarilyOffline() ) {
      if ( ! agent.getComputer().getOfflineCause().toString().contains("Disconnected by") ) {
        computer.setTemporarilyOffline( false, agent.getComputer().getOfflineCause() )
      }
    } else {
      computer.connect( true )
    }
  }
}
println ( "Number of Offline Nodes: " + numberOfflineNodes )
println ( "Number of Nodes: "         + numberNodes        )

others

[!NOTE|label:references:]

  • scripts:

def exec(cmd) {
  println cmd
  def process = new ProcessBuilder([ "sh", "-c", cmd])
                      .directory(new File("/tmp"))
                      .redirectErrorStream(true)
                      .start()
  process.outputStream.close()
  process.inputStream.eachLine { println it }
  process.waitFor()
  return process.exitValue()
}

[
  "echo hello world",
  "ls -al"
].each { exec(it) }

get all running thread

Thread.getAllStackTraces().keySet().each() {
  println it.getName()
}
  • [!TIP] or using t.stop() instead of t.interrupt()

    Thread.getAllStackTraces().keySet().each() { t ->
      if ( t.getName()=="YOUR THREAD NAME" ) { t.interrupt(); }
    }
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.thoughtslive.jenkins.plugins.jira.JiraStepsConfig
import org.thoughtslive.jenkins.plugins.jira.Site

//global user-defined configuration
JSONArray sitesConf = [
  [
    name        : 'another'            ,
    url         : 'http://example.com' ,
    timeout     : 10000                ,
    readTimeout : 10000                ,
    loginType   : 'BASIC'              ,
    userName    : 'foo'                ,
    password    : 'some pass'
  ],
  [
    name        : 'moar jira'          ,
    url         : 'http://example.com' ,
    timeout     : 10000                ,
    readTimeout : 10000                ,
    loginType   : 'OAUTH'              ,
    consumerKey : 'my consumer key'    ,
    privateKey  : 'my private key'     ,
    secret      : 'super secret'       ,
    token       : 'my token'
  ]
] as JSONArray

// get global Jenkins configuration
JiraStepsConfig.ConfigDescriptorImpl config = jenkins.model.Jenkins.instance.getExtensionList(JiraStepsConfig.ConfigDescriptorImpl.class)[0]

ArrayList<Site> sites = new ArrayList<Site>()

// configure new sites from the above JSONArray
sitesConf.each { s ->
  String loginType = s.optString('loginType', '').toUpperCase()
  if( loginType in ['BASIC', 'OAUTH'] ) {
    Site site = new Site(s.optString('name',''), new URL(s.optString('url', '')), s.optString('loginType', ''), s.optInt('timeout', 10000))

    if( loginType == 'BASIC' ) {
      site.setUserName( s.optString('userName', '') )
      site.setPassword( s.optString('password', '') )
      site.setReadTimeout( s.optInt('readTimeout', 10000) )
    } else { //loginType is OAUTH
      site.setConsumerKey( s.optString('consumerKey', '') )
      site.setPrivateKey( s.optString('privateKey', '') )
      site.setSecret( s.optString('secret', '') )
      site.setToken( s.optString('token', '') )
      site.setReadTimeout( s.optInt('readTimeout', 10000) )
    }

    sites.add(site)
  }
}

//set our defined sites
config.setSites(sites.toArray(new Site[0]))

//persist configuration to disk as XML
config.save()
  • unclassified:
      jiraStepsConfig:
        sites:
        - name: 'another'
          url: 'http://example.com'
          timeout: 10000
          readTimeout: 10000
          loginType: 'BASIC'
          userName: 'foo'
          password: 'some pass'
        - name: 'moar jira'
          url: 'http://example.com'
          timeout: 10000
          readTimeout: 10000
          loginType: 'OAUTH'
          consumerKey: 'my consumer key'
          privateKey: 'my private key'
          secret: 'super secret'
          token: 'my token'
  • [
      "ant",
      "artifactdeployer",
      "build-failure-analyzer",
      "build-name-setter",
      "build-pipeline-plugin",
      "build-timeout",
      "claim",
      "clone-workspace-scm",
      "cobertura",
      "collapsing-console-sections",
      "conditional-buildstep",
      "configurationslicing",
      "copy-to-slave",
      "credentials",
      "cvs",
      "disk-usage",
      "ec2",
      "email-ext",
      "external-monitor-job",
      "git",
      "git-client",
      "global-build-stats",
      "gravatar",
      "groovy-postbuild",
      "javadoc",
      "jobConfigHistory",
      "ldap",
      "mailer",
      "mask-passwords",
      "maven-plugin",
      "openid",
      "pam-auth",
      "parameterized-trigger",
      "run-condition",
      "shelve-project-plugin",
      "ssh-credentials",
      "ssh-slaves",
      "subversion",
      "svn-release-mgr",
      "token-macro",
      "translation",
      "view-job-filters",
      "ws-cleanup"
    ].each { plugin ->
      e = Hudson.instance.updateCenter.getPlugin(plugin).deploy().get().getError()
      if ( e != null )
        println e.message
    }
    • import jenkins.model.*
      import java.util.logging.Logger
      
      def logger      = Logger.getLogger("")
      def installed   = false
      def initialized = false
      
      def pluginParameter = "gitlab-plugin hipchat swarm"
      def plugins = pluginParameter.split()
      logger.info( "" + plugins )
      def instance = jenkins.model.Jenkins.instance
      def pm       = instance.getPluginManager()
      def uc       = instance.getUpdateCenter()
      uc.updateAllSites()
      
      plugins.each {
        logger.info( "Checking " + it )
        if ( !pm.getPlugin(it) ) {
          logger.info( "Looking UpdateCenter for " + it )
          if ( !initialized ) {
            uc.updateAllSites()
            initialized = true
          }
          def plugin = uc.getPlugin(it)
          if ( plugin ) {
            logger.info( "Installing " + it )
            plugin.deploy()
            installed = true
          }
        }
      }
      
      if ( installed ) {
        logger.info( "Plugins installed, initializing a restart!" )
        instance.save()
        instance.doSafeRestart()
      }
new File(System.getProperty("user.home"),'.nestedViewsSearch').createNewFile()
println( "Groovy: ${GroovySystem.version}" )
println( "Jenkins: ${jenkins.model.Jenkins.instance.getVersion()}" )
println( "OS: ${System.getProperty('os.name')} - ${System.getProperty('os.version')}" )
println( "Java: ${System.getProperty('java.version')} - ${System.getProperty('java.vm.vendor')} (${System.getProperty('java.vm.name')})" )

[!NOTE|label:references:]

import hudson.tasks.test.AbstractTestResultAction

@NonCPS
def testStatuses() {
  def testStatus = ""
  AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
  if ( testResultAction != null ) {
    def total   = testResultAction.totalCount
    def failed  = testResultAction.failCount
    def skipped = testResultAction.skipCount
    def passed  = total - failed - skipped
    testStatus  = "Test Status:\n  Passed: ${passed}, Failed: ${failed} ${testResultAction.failureDiffString}, Skipped: ${skipped}"

    if ( failed == 0 ) {
      currentBuild.result = 'SUCCESS'
    }
  }
  return testStatus
}

or:

or

Create a Permanent Agent from Groovy Console
CloudBees SSH Build Agents plugin 2.0 and newer
Monitor and Restart Offline Agents
jira-publisher.groovy
run shell scripts in a cluster-operation
kill running thread
Automate configuring via Jenkins Script Console
via Configuration as Code plugin
install plugin
or
Jenkins search results missing /ci/ URL component
jenkins server info
How to access Junit test counts in Jenkins Pipeline project
Using pipeline & groovy, How can I extract test results from Jenkins for my current build?
Jenkins Features Controlled with System Properties
How do I access Pipeline DSLs from inside a Groovy class?
DSL
How do I access Pipeline DSLs from inside a Groovy class?
jobs
Create a Permanent Agent from Groovy Console
agents
Monitor and Restart Offline Agents
others
run shell scripts in a cluster-operation
get all running thread
Automate configuring via Jenkins Script Console
Jenkins search results missing /ci/ URL component
jenkins server info
How to access Junit test counts in Jenkins Pipeline project