Dynamically Creating Release/Gate tasks with Jython and linking with createdReleaseId

I have some variable/dynamic input and I need to create multiple subreleases & related gates for waiting for the subrelease to complete, and am struggling. I have gotten the subrelease to get created, but I can’t not figure out how to get the createdReleaseId variable created & linked. Google searching hasn’t provided clear guidance on how do do this. Can you help with examples?

for host in releaseVariables['prfHosts'].strip('[').strip(']').split(","):
    cln_host=host.strip('\'')
    taskToAddType = Type.valueOf("xlrelease.CreateReleaseTask")
    taskToAdd = taskToAddType.descriptor.newInstance("nonamerequired")
    taskToAdd.setTitle(cln_host+" - Pre Test, Install, Test, Review")
    taskToAdd.templateId = templateApi.getTemplates('Environment Install/Upgrade')[0].id
    taskToAdd.folderId = folderApi.find('Master/App', 0).id
    taskToAdd.newReleaseTitle(cln_host+" - Pre Test, Install, Test, Review")

    
    host_parm=StringVariable()
    host_parm.setKey("host")
    host_parm.setValue(cln_host)
    port_parm=StringVariable()
    port_parm.setKey('port')
    port_parm.setValue(version_ports_arr[1])
    taskToAdd.templateVariables = [host_parm,port_parm]#,listvar]

    gateToAdd = taskApi.newTask("xlrelease.GateTask")
    gateToAdd.setTitle("wait for "+cln_host+" subrelease to complete")
    
    taskApi.addTask(prfTaskGroup, taskToAdd)

    listvar=Variable()
    listvar.setKey(cln_host+'rlsid')
    listvar.setValue(taskToAdd.createdReleaseId)
    listvar.type = "xlrelease.StringVariable"
    releaseApi.createVariable(release.id,listvar ) 
    
    taskApi.addTask(prfTaskGroup, gateToAdd)
    taskApi.addDependency(gateToAdd.id,taskToAdd.id)

Please have a look here:

On the same page are also more dynamic examples, please let us know if this helps !

Another approach would be to wrap it into a ‘repeat’ block. See code here that may help

Kind regards,

Hes.

@Hes - Did you post this second reply in the wrong flow? or am I just not following?

I’ve looked at that page quite a bit and much of my starting stop has come from the section Dynamically create Create Release Tasks in a release (in a parallel block) and dynamically set a related subrelease template variable. The problem I was having was getting the new release task tied to the gate task. I was able to get that working with these 2 lines:

taskToAdd.createdReleaseId = "$""{"+cln_host+"rlsid}"
taskApi.addDependency(gateToAdd.id,"$""{"+cln_host+"rlsid}")

Here’s my full code now:

for host in releaseVariables['tstHost'].strip('[').strip(']').split(","):
    cln_host=host.strip('\'')
    taskToAddType = Type.valueOf("xlrelease.CreateReleaseTask")
    taskToAdd = taskToAddType.descriptor.newInstance("nonamerequired")
    taskToAdd.setTitle(cln_host+" - Pre Test, Install, Test, Review")
    taskToAdd.templateId = templateApi.getTemplates('Environment Install/Upgrade')[0].id
    taskToAdd.folderId = folderApi.find('Master/App', 0).id
    taskToAdd.newReleaseTitle = cln_host+" - Pre Test, Install, Test, Review"
    
    host_parm=StringVariable()
    host_parm.setKey("host")
    host_parm.setValue(cln_host)
    
    port_parm=StringVariable()
    port_parm.setKey('port')
    port_parm.setValue(version_ports_arr[1].strip('\''))

    listvar=ListStringVariable()
    listvar.setValue(releaseVariables['LanesList'])
    listvar.setKey("Trips")

    #taskToAdd.variableMapping = {"pythonScript.MapOut" : "$""{dicoPackageMap}"}
    taskToAdd.createdReleaseId = "$""{"+cln_host+"rlsid}"
    taskToAdd.templateVariables = [host_parm,port_parm,listvar]

    gateToAdd = taskApi.newTask("xlrelease.GateTask")
    gateToAdd.setTitle("wait for "+cln_host+" subrelease to complete")
    taskApi.addTask(tstTaskGroup, taskToAdd)

    taskApi.addTask(tstTaskGroup, gateToAdd)
    taskApi.addDependency(gateToAdd.id,"$""{"+cln_host+"rlsid}")

Hi, sorry for jumping the gun there a bit – what I meant was that you could use a foreach construct that let’s you define a sequential task that contains a repeater task that gets copied while executing, based on the values of a list variable. This allows you to model your logic in the release flow itself and defer the repeating to the custom task. Not sure if it applies in your situation, but if you are interested in this approach let me know.