Due to some maintenance issues, this service has been switched in read-only mode, you can find more information about the why

and how to migrate your plugin documentation in this blogpost

Skip to end of metadata
Go to start of metadata

Execute a global configured groovy script after each build of each job managed by the Jenkins.
This is typical for cases when you need to do something based on a shared set of parameters, such as triggering downstream jobs managed by the same Jenkins or remote ones based on the parameters been passed to the parameterized jobs.

Notice: jython script support removed since 1.1.0

Plugin Information

View Global Post Script on the plugin site for more information.

The current version of this plugin may not be safe to use. Please review the following warnings before use:

Global Configure


Default: the script will be executed when the build result is better or equal to UNSTABLE

Variables that could be used in the script file

Jenkins Built-in Variables

Variable

Description

Sample Data

BUILD_ID

Build timestamp as ID

2014-06-26_07-16-51

BUILD_NUMBER

Build No#

16

BUILD_RESULT

Build result

SUCCESS / UNSTABLE / FAILURE ...

BUILD_TAG

Job Name + Build No#

jenkins-test-job-16

BUILD_URL

The URL of this build

http://localhost:8080/job/test-job/16/

JENKINS_HOME

The path of the root folder of Jenkins

~/workspace-idea/global-post-script-plugin/./work

JENKINS_URL

The root URL of Jenkins

http://localhost:8080/

JOB_NAME

Name of the job

test-job

JOB_URL

URL of the job

http://localhost:8080/job/test-job/

MAVEN_CMD_LINE_ARGS

Maven command args

clean install

NODE_LABELS

Lables of the nodes where the build could be executed

master

NODE_NAME

Name of the node where the build executed

master

SVN_REVISION

SVN revision

185214

SVN_URL

SVN URL

 

WORKSPACE

The path of the workspace

~/workspace-idea/global-post-script-plugin/work/workspace/LOGANALYZE

GIT_BRANCH

Git branch

origin/master

GIT_COMMIT

Git commit ID

ddaf9c4576e375b42b6bc7c73daa12cb28a92031

GIT_URL

Git repo URL

https://github.com/jenkinsci/global-post-script-plugin.git

Extra variables

Parameters of parameterized build or parameters been passed in by -Dparameter_name=parameter_value are also available

the manager object

An extra object is available as groovy variables: manager, provided 4 methods:

Method

Description

isVar(String name)

Check if a variable is defined and usable in the script

isNotBlankVar(String name)

Check if a variable is defined and usable in the script, and with a non-blank value

addBadge(String icon, String text)

Add a badge to the build

addShortText(String text)

Add a text label to the build

triggerJob(String jobName)

Trigger a job managed by the same Jenkins

triggerRemoteJob(String url)

Trigger a job by URL

3rd-party libraries for groovy script

If your groovy script have dependencies that not shipped with Jenkins, you can put them into $JENKINS_HOME/global-post-script/lib.

Supported Scripts

Groovy

Groovy Sample
out.println("dropdeploy to: $dropdeploy_targets")
Groovy Sample
out.println("dropdeploy to: " + dropdeploy_targets)
Groovy Sample
if (manager.isVar("variable_name")) {
    ...
}
Groovy Sample
job = hudson.model.Hudson.instance.getItem("demo2")
build = job.getLastBuild()
println build
def action = build.getAction(hudson.plugins.claim.ClaimBuildAction.class)
print action.getReason()
Groovy Sample
def triggers = [
        wwwsqs8: {
            def params = [
                PARENT_BUILD_NUMBER: '$BUILD_NUMBER',
                PARENT_JOB_NAME: '$JOB_NAME',
                any_param_name: '$deploy_targets'
            ]
            manager.triggerJob("WWW_JBEHAVE_TEST", params)
            manager.triggerJob("WWW_MOBILE_API_TEST")
            manager.triggerRemoteJob("http://localhost/job/Dev_Launch_WWW_SQS_REGRESSION/build?token=88e4b5fd1d28949710a9c4924775ce40&delay=1800sec")
        },
        wwwsqm8: {
            manager.triggerRemoteJob("http://localhost/job/Dev_Launch_WWW_SQM_REGRESSION/build?token=88e4b5fd1d28949710a9c4924775ce40&delay=1800sec")
        },
        bsdsqs8: {
            manager.triggerJob("BSD_JBEHAVE_TEST")
            manager.triggerJob("BSD_MOBILE_API_TEST")
            manager.triggerRemoteJob("http://localhost/job/Dev_Launch_BSD_SQS_REGRESSION/build?token=88e4b5fd1d28949710a9c4924775ce40&delay=1800sec")
        },
        bsdsqm8: {
            manager.triggerRemoteJob("http://localhost/job/Dev_Launch_BSD_SQM_REGRESSION/build?token=88e4b5fd1d28949710a9c4924775ce40&delay=1800sec")
        },
        gmlsqs8: {
            manager.triggerJob("GMIL_JBEHAVE_TEST")
            manager.triggerRemoteJob("http://localhost/job/Dev_Launch_GMIL_SQS_REGRESSION/build?token=88e4b5fd1d28949710a9c4924775ce40&delay=1800sec")
        },
        gmlsqm8: {
            manager.triggerRemoteJob("http://localhost/job/Dev_Launch_GMIL_SQM_REGRESSION/build?token=88e4b5fd1d28949710a9c4924775ce40&delay=1800sec")
        },
        basepom: {
            manager.triggerJob("basepom")
        }
]

if (manager.isVar("deploy") && manager.isNotBlankVar("deploy_targets") && "true" == deploy) {
    dropped = false
    deploy_targets.split(',').each {
        trigger = triggers[it]
        if (trigger) {
            trigger()
            dropped = true
        }
    }
    if (dropped) {
        manager.addBadge("server.png", "[SQ: " + deploy_targets + "]")
    }
}

bat/sh

NO variables will passed into the script

Open Issues

T Key Summary Assignee Reporter P Status Resolution Created Updated Due
Loading...
Refresh

Changes

1.1.4

1.1.3

  • Fixed JENKINS-37796 Loading jars in global-post-script/lib (if exist) as classpath entries when running groovy script.

1.1.2

  • Not the cache the parsed GroovyShell

1.1.1.1

1.1.1

  • *Do NOT use this version*
  • Parsing and caching the groovy script, to make a smaller footprint. JENKINS-33358

1.1.0

  • Added `aborted` condition, to allow triggering the script even the build is aborted.
  • Removed support for jython scripts. (jython dependency is too big, and Groovy is much better supported by Jenkins.)

1.0.12

  • Fixed issue about script been executed for each sub-module of Maven multi-module projects.

1.0.11

  • Fixed issue about groovy script works in script console but fails as global post script

1.0.10

  • Allow to run also for failed builds (Global configure).
  • Added BUILD_RESULT to script.

1.0.9

  • Supports parameters when triggering local jobs (parameterized downstream jobs)

4 Comments

  1. Unknown User (shumss)

    Hi,
    could you please assist with next case:

    How I could connect to mysql db using your plugin?

    I use windows OS, the mysql-connector-java-5.1.37-bin.jar has been installed on system
    when I execute print(sys.path) I could see:\\Program Files (x86)\\Jenkins\\plugins\\global-post-script\\WEB-INF\\lib\\Lib', 'C:\\Program Files (x86)\\Jenkins\\plugins\\global-post-script\\WEB-INF\\lib\\jython-standalone-2.7-b2.jar\\Lib', '__classpath__', '__pyclasspath__/', '"C:\\Program Files (x86)\\Jenkins\\plugins\\global-post-script\\WEB-INF\\lib\\mysql_connector_java_5.1.37_bin.jar"'

    But When I'm trying to execute:
    from _future_ import with_statement
    from com.ziclix.python.sql import zxJDBC
    import sys
    sys.path

    1. for example
      jdbc_url = "jdbc:postgresql:test"
      username = "postgres"
      password = "jython25"
      driver = "com.mysql.jdbc.Driver"
    1. obtain a connection using the with-statment
      with zxJDBC.connect(jdbc_url, username, password, driver) as conn:
          with conn:
              with conn.cursor() as c:
                  c.execute("select name from country")
                  c.fetchone()

    I could see next error:
    ERROR Failed to execute: test.py, zxJDBC.DatabaseError: driver com.mysql.jdbc.Driver not found in <script> at line number 10

    1. Unknown User (orctom)

      You got to place the jar in Jenkins' lib folder (outside of jenkins home), instead of this plugin's lib folder

    2. Unknown User (orctom)

      Since 1.1.3 jars in $JENKINS_HOME/global-post-script/lib will be loaded into classpath.

  2. Unknown User (boiky)

    Hi. Many thanks for useful plugin.

    I've realized that it runs on the master not slave.

    We need to do some cleanup pre- or post- every job, but on slaves - are you aware of any plugin like this?