简体   繁体   中英

Groovy Script in Jenkins: dependency management

I'm trying to get this Groovy script to run in Jenkins:

import java.lang.ProcessBuilder.Redirect
import hudson.model.*
import hudson.util.*
import hudson.scm.*
//I'm not sure that these 2 imports are correct:
import hudson.plugins.tfs.model.ChangeSet;
import hudson.plugins.tfs.model.ChangeSet.Item;

// work with current build
def build = Thread.currentThread()?.executable

// get ChangesSets with all changed items
def changeSet= build.getChangeSet()
List<Item> items = changeSet.getItems()

def affectedFiles = items.collect { it.paths }

// get file names
def fileNames = affectedFiles.flatten().findResults

// setup log files
def stdOutFile = "${build.rootDir}\\stdout_groovy.txt"
def stdErrFile = "${build.rootDir}\\stderr_groovy.txt"

//execute a command for each file
fileNames.each
{
    def params = ["cmd.exe", "/C", "dir ${it}"]
    def processBuilder = new ProcessBuilder(params)

    // redirect stdout and stderr to log files
    processBuilder.redirectOutput(new File(stdOutFile))
    processBuilder.redirectError(new File(stdErrFile))

    def process = processBuilder.start()
    process.waitFor()

    // print log files
    println new File(stdOutFile).readLines()
    System.err.println new File(stdErrFile).readLines()
}

My goal is to be able to iterate over only the files that have changed and copy those files to another location. I'm trying to create a version of the code in Process only changed files that works with TFS. I get 2 errors:

unable to resolve class hudson.plugins.tfs.model.ChangeSet.Item

unable to resolve class hudson.plugins.tfs.model.ChangeSet

This makes sense because Groovy doesn't know where to get the TFS code. I'm using the TFS plugin at https://wiki.jenkins-ci.org/display/JENKINS/Team+Foundation+Server+Plugin . I've read that Groovy can use Grape for dependency management. If the TFS plugin was in http://mvnrepository.com/ then I could use it to let Groovy get the latest code which would look something like this:

@Grapes(
    @Grab(group='org.apache.maven.scm', module='maven-scm-provider-tfs', version='1.9.4')
)

However, the TFS plugin is not in http://mvnrepository.com/ . The plugin source is in https://github.com/jenkinsci/tfs-plugin . So, how can I tell Groovy where to get the code? Ideally, I wouldn't have to copy the plugin code to my build machine and deal with keeping it up to date. (I'm sure there are errors in my code. I'm just trying to get the dependency management worked out (but feel free to point out coding errors)). Thanks.

As long as you have the plugin installed, you can access plugin classes from groovy running in jenkins. That being said, it sounds like you may not have the plugin installed!?!

Ideally, I wouldn't have to copy the plugin code to my build machine and deal with keeping it up to date.

If this is the case, I really don't understand why you would try to use plugin code that isn't installed in jenkins.

As I see, you're running Groovy script inside a job with "groovy" plugin.

The plain "Groovy Script" is run in a forked JVM, on the slave where the build is run. It's the basically the same as running the "groovy" command and pass in the script. So you would need to add classpath entries like:

groovy -cp <path-to-jars> script.groovy

The system groovy script, runs inside the Jenkins master's JVM. Thus it will have access to all the internal objects of Jenkins, so you can use this to alter the state of Jenkins. It is similar to the Jenkins Script Console functionality.

So, you should choose "System Groovy Script", in order to have access to classes that Jenkins knows about (its plugins).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM