Update template variable using jython

#1

want to update template variable using jython before creating release

#2

Here is Jython API
You can get the VariableID to be changed from the RestCall
https://docs.xebialabs.com/xl-release/8.5.x/rest-docs/#find-template-release-by-id

obj = releaseApi.getVariable('Applications/Foldera281a4293f4c40/Releasebe3cfbfdf3384d38bc7a250e4235b98b/Variable72232b7cc65c4b')
obj.value = "newValue"
releaseApi.updateVariable(obj)

This should update your variable.

2 Likes
#3

@gsajwan I may be missing something, but I can’t quite understand your answer. I came across this question while trying to find a solution to the scenario below:

  • I’d like to add code to my template to populate a variable calling an external REST API. Let’s say myVar=abc (abc being the response from the REST API that I called in Jython).

  • Then I’d like to reuse myVar in another step of the template (say, several steps ahead).

Your answer contains a folder ID and a release ID. First, in my case there wouldn’t be a release ID since I want to add this code to the template and ensure the code gets run when a release is created (so I don’t have a release ID yet because a release has not been created from the template). Second, how do I find the folder ID?

#4

I tried that code, and it’s not updating the variable.

I have this code:

for p in release.phases:
    for t in p.tasks:
        if str(t.getTaskType()).split('.')[1] == 'CreateReleaseTask':
            for template_var in t.getTemplateVariables():
                if template_var.key == 'sn_instance':
                    rel_var = releaseApi.getVariable(template_var.id)
                    rel_var.value = 'oclc'
                    releaseApi.updateVariable(rel_var)

Output:

Exception during execution: com.xebialabs.deployit.exception.NotFoundException: com.xebialabs.deployit.exception.NotFoundException:
Variable [Applications/Folder943a7b3816ea41e0b1cb4e4995efdac4/Release7fbb020c0ebb40e9944cfbbb2f2360c7/Phase61006a649e944b90ad7053a0da49c29e/Task83e770a664b44d2491f471552d344fec/Variable33b513d9ea964b71b117f2ab069a57f8] does not exist in the repository or archive in <script> at line number 8

What doesn’t make sense to me is that XLR’s API already found the variable. How can it not find it again?

For reference, here is the variable in my browser:

{
"id": "Applications/Folder943a7b3816ea41e0b1cb4e4995efdac4/Released55946a970104a39a5c70bebee5a0441/Phase61006a649e944b90ad7053a0da49c29e/Task83e770a664b44d2491f471552d344fec/Variable33b513d9ea964b71b117f2ab069a57f8",
"type": "xlrelease.StringVariable",
"key": "sn_instance",
"requiresValue": false,
"showOnReleaseStart": true,
"value": "oclcdev"
}

Please let me know if you want me to create a new post for this issue.

#5

bump

How is this variable getting returned by getTemplateVariables(), but the releaseApi can’t find it when it goes to update it.

Here’s the kicker, it doesn’t throw an error when I go to do getVariable(), just when I do updateVariable().

FWIW I’m getting the exact same error when I use the templateApi.updateVariable() call instead.

EDIT:

Also, getTemplateVars() doesn’t seem to be documented. There’s no documented way to set variables that should be passed to a release cretaed by a CreateReleaseTask task.

#6

Bump.

This is affecting us as well.

#7

Hi dixle/smacz,

Try this:

for t in phase.tasks:
    if str(t.getTaskType()).split('.')[1] == 'CreateReleaseTask':
        for template_var in t.templateVariables:
            if template_var.key == 'dpackage':
                template_var.value = 'oclc'
                taskApi.updateTask(t.id, t)

I am trying this out with a template that has a script task and then also has a createRelease task. the template referred in createRelease task requires few variable mappings and i am updating the value in those mapping field dynamically using the script task.

What you were doing incorrectly earlier is releaseApi.updateVariable(rel_var) will try to release the value of that variable in the core template. But template_var.id has a different dynamic id for the variable as its being referred for what’s being provided in the task VS. the id for the realvariable defined in the actual template and that’s why you get error.

Within the context of current release, you have to update the value of the keypair that you fetched , update it and then update the task rather than the release.

Hope it helps…

Amit

#8

I think that was the key that I was missing. I’ve tried that, and it works.

The one caveat is that the variable has to be populated with a value in the task in order for t.templateVariables to return that variable. Otherwise it doesn’t get returned. However, since the whole reason we’re doing this is to overwrite that variable, for my use case, setting an initial value doesn’t matter to me, as long as I remember to do it.

#9

Glad it worked. Happy to help.

take care

#10

Also added it here for reference

#11

And just as a follow-up for anyone (read: future me) that stumbles across this again, keep in mind that having the variable pre-populated is required for ListBox variables too. They need to be selected initially as one of the options that the release provides before they will be able to be change programatically. So literally select the default variable at least once for every listbox variable in your tasks in your release.