Setting output variable for task in Jython

#1

I am using the “createScriptBasedTask” method found at https://blog.xebialabs.com/2015/08/11/creating-xl-release-tasks-dynamically-using-jython-api/ to successfully create a task.

NEW (MOSTLY) WORKING JYTHON - Only misses specifying the “output” variable:
newTask = createScriptBasedTask(newGroup.id, “remoteScript.WindowsSmb”, newTaskTitle, None,
{
“script” : newScript,
“connectionType” : “WINRM_NATIVE”,
“remotePath” : newRemotePath,
“port” : “”,
“address” : releaseVariables[‘azureGatewayAddress’],
“username” : auth[0],
“password” : auth[1]
})

I still need, however, to set an output variable for this step. It is unclear how to specify a value in the property map to set this.

Here is the original Groovy script export from our template:

GROOVY EXPORT - Successfully sets “output” variable:
custom(‘Call GetAppSvcPlans - EmpAPI’) {
script {
type ‘remoteScript.WindowsSmb’
script ‘powershell .\get-app-service-list.ps1 -Environment "’${coreEnv}’" -DataCenter “’${Deployment.DataCenter}’” -ApplicationKey “’${Deployment.FlextimeSvc.ApplicationKey}’”’
remotePath ‘${Deployment.DeployInfrastructureIDPath}${envGroup}\${Deployment.DeployScripts.ApplicationName}\${coreBranch}_${Deployment.DeployScripts.BuildNumber}\Deploy’
output variable(‘getListResponse’)
address ‘${azureGatewayAddress}’
username ‘${Deployment.Username}@company.com
password variable(‘Deployment.Password’)
connectionType ‘WINRM_NATIVE’
}
}

The Groovy snippet has been entirely translated into Jython (shown above) except for the output variable.

“output” : releaseVariables[‘myXMLResponseOutput’] of course does not work

How do i link this value to a variable reference?
e.g. releaseVariables[‘myXMLResponseOutput’]

#2

Updated code looks like this. (Now creating a variable. But setting the variable in the property map tries to coerce it into a string.)

	xmlResponseVar = Variable()
	xmlResponseVar.value = 'OUTPUT FOR ' + newAppName
	xmlResponseVar.key = appSvcConfig['xmlListResponse']
	xmlResponseVar.type = "xlrelease.StringVariable"
	XLRUtil.createScriptBasedTask(newGroup.id, "remoteScript.WindowsSmb", newTaskTitle, None,
		{
		"script"         : newScript,
		"connectionType" : "WINRM_NATIVE",
		"remotePath"     : newRemotePath,
		"port"           : "",
		"address"        : releaseVariables['azureGatewayAddress'],
		"username"       : auth[0],
		"password"       : auth[1],
		"output"         : xmlResponseVar
		})

Produces the following ERROR:

Error: Exception of type java.lang.ClassCastException thrown on line 24 while executing remote script: com.xebialabs.xlrelease.api.v1.forms.Variable cannot be cast to java.lang.String

#3

Finally figured it out. I am now successfully (1) creating a new variable and (2) attaching it to the property map.

Updated code looks like this.

	xmlResponseVar = Variable()
	xmlResponseVar.value = 'OUTPUT FOR ' + newAppName
	xmlResponseVar.key = appSvcConfig['xmlListResponse']
	xmlResponseVar.type = "xlrelease.StringVariable"
	releaseApi.createVariable(release.id, xmlResponseVar)
	XLRUtil.createScriptBasedTask(newGroup.id, "remoteScript.WindowsSmb", newTaskTitle, None,
		{
		"script"         : newScript,
		"connectionType" : "WINRM_NATIVE",
		"remotePath"     : newRemotePath,
		"port"           : "",
		"address"        : releaseVariables['azureGatewayAddress'],
		"username"       : auth[0],
		"password"       : auth[1],
		"output"         : "${" + xmlResponseVar.key + "}"
		})
#4

Looking good – thanks for sharing.

1 Like
#5

hi,

Good to see that blog i wrote in 2015 still works. i have created a repo over time that also has a ton more scripting including that. please refer to this. https://github.com/xebialabs-community/xlr-jython-code-snippets

2 Likes