Your browser was unable to load all of the resources. They may have been blocked by your firewall, proxy or browser configuration.
Press Ctrl+F5 or Ctrl+Shift+R to have your browser try again.

[Groovy Script] Jenkins EnvInject for QuickBuild - No Plugins required #3733

narwhal ·

So we are evaluating switching our process from Jenkins to QuickBuild. Using the latest version 8.0, I was able to get this working. So if this helps someone adopt QuickBuild because of EnvInject no longer being a requirement, here it is. Basically it pulls all the variables from the current configuration, resolves them, and then pushes them into the current Environment context. This is not without risk, but it does allow legacy scripts to be ported fairly seamlessly some work may need to be done to represent booleans as a good value for your script, but otherwise it works great.

There are two pieces

  1. The pre-execute groovy script
  2. The groovy script which lives out on disk on the executing agent

Part 1) Pre-execute script
Note: Define a Variable "QB_Scripts_Root", and assign it the path for the executing node through other methods.

groovy:

def script_path = vars.getValue("QB_Scripts_Root");
def script = new File( script_path + "/EnvInject.groovy").text;

def sharedData = new Binding();
def shell = new GroovyShell(sharedData);

// add classes needed for the groovy script
sharedData.setProperty('current', current);
sharedData.setProperty('system', system);
sharedData.setProperty('util', util);
sharedData.setProperty('grid', grid);
sharedData.setProperty('node', node);
sharedData.setProperty('user', user);
sharedData.setProperty('configuration', configuration);
sharedData.setProperty('build', build);
sharedData.setProperty('request', request);
sharedData.setProperty('vars', vars);
sharedData.setProperty('repositories', repositories);
sharedData.setProperty('steps', steps);
sharedData.setProperty('params', params);
sharedData.setProperty('step', step);
sharedData.setProperty('logger', logger);

shell.evaluate( script );

Part 2) EnvInject.groovy
Note: "groovy:" is omitted intentionally when executing from an already established groovy shell

import com.pmease.quickbuild.variable.VariableWrapper;
import com.pmease.quickbuild.Property;

// script reference:
// http://docs.groovy-lang.org/next/html/documentation/guide-integrating.html

if( step instanceof com.pmease.quickbuild.stepsupport.SequentialStep )
{
    logger.debug( "=== Begin Reading QuickBuild Variables ===" );

    java.util.List<Property> props = [];

    for( var in configuration.findVariables() )
    {
        varname = var.getName();
        varval = vars.getValue(varname);  // this hack forces the variable to resolve

        Property prop = new Property(varname, varval);
        props.add(prop);

        logger.warn( varname + "=" + varval );
    }

    logger.debug( "=== End Reading QuickBuild Variables ===" );

    logger.debug( "=== Properties Content ===" );

    for( p in props )
    {
        logger.warn( p.getName() + "=" + p.getValue() );
    }

    logger.warn( "=== Injecting Variables ===" );
    step.setEnvironments( props );
}
else
{
    logger.error( "EnvInject.groovy may only be used from the 'master' step, Sequential Step, or other steps containing Environment Variables" );
    throw new com.pmease.quickbuild.QuickbuildException( "[EnvInject.groovy] is only intended for pre-execute conditions" );
}

As an added bonus you can now version your groovy scripts and use them just like shell/batch commands. You can use the boiler plate code from step 1 for the execute Script function. In reality this boiler plate should probably be a core feature of QuickBuild, but until it is here you go. With the boiler plate - your on disk groovy scripts will function exactly as groovy written as the "Execute a Script" function with access to all the same classes.

There are a couple of caveats but you get the idea.

  • replies 0
  • views 57
  • stars 1