Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to loop through an array and pass each value into a gradle task

I'm trying to do a series of things which should be quite simple, but are causing me a lot of pain. At a high level, I want to loop through an array and pass each value into a gradle task which should return an array of its own. I then want to use this array to set some Jenkins config.

I have tried a number of ways of making this work, but here is my current set-up:

project.ext.currentItemEvaluated = "microservice-1"

task getSnapshotDependencies {
    def item = currentItemEvaluated
    def snapshotDependencies = []

    //this does a load of stuff like looping through gradle dependencies,
    //which means this really needs to be a gradle task rather than a 
    //function etc. It eventually populates the snapshotDependencies array.

    return snapshotDependencies
}

jenkins {
    jobs {
        def items = getItems() //returns an array of projects to loop through
        items.each { item ->
           "${item}-build" {
                project.ext.currentItemEvaluated = item
                def dependencies = project.getSnapshotDependencies
                dsl {
                    configure configureLog()
                    //set some config here using the returned dependencies array
             }
         }
     }
}

I can't really change how the jenkins block is set-up as it's already well matured, so I need to work within that structure if possible.

I have tried numerous ways of trying to pass a variable into the task - here I am using a project variable. The problem seems to be that the task evaluates before the jenkins block, and I can't work out how to properly evaluate the task again with the newly set currentItemEvaluated variable.

Any ideas on what else I can try?

like image 581
MDalt Avatar asked Oct 18 '22 06:10

MDalt


1 Answers

After some more research, I think the problem here is that there is no concept of 'calling a task' in Gradle. Gradle tasks are just a graph of tasks and their dependencies, so they will compile in an order that only adheres to those dependencies.

I eventually had to solve this problem without trying to call a Gradle task (I have a build task printing the relevant data to a file, and my jenkins block reads from the file)

See here

like image 125
MDalt Avatar answered Oct 21 '22 10:10

MDalt