Add variable to project from Python script output

Hello,
I have some step running on my Runhook that compares image sha between ECR and docker container and if it’s different it triggers the release.
I would like to store image tag from ECR as a global variable and use it on my release as release version.
Here is my Python code:


client = boto3.client(‘ecr’, region_name=‘’, aws_access_key_id= '’, aws_secret_access_key= ‘***********’)
response = client.describe_images(
registryId=’****’,
repositoryName=‘octopus-deploy’,
imageIds=[
{
‘imageTag’: ‘latest’
},
],
filter={
‘tagStatus’: ‘TAGGED’
}
)
#GET latest image tag from ECR
tag = (response[‘imageDetails’][0][‘imageTags’])
image_tag = (tag[0])
print(image_tag)


The question is how can i save image_tag Python object as global variable.
Thank you.

Hi @andreybyhalenko,

Thank you for getting in touch.

I am wondering for your scenario, you may benefit using the Set-OctopusVariable as described here: Output variables - Octopus Deploy.

In the basic example below, here is an example for how you could achieve this:

As part of your Runbook process you can add a Run a Script step to to get the image_tag from your script and store this into a variable using the Set-OctopusVariable to store the image tag.

Please note that when using Set-OctopusVariable Octopus creates an output variable for use in:

  • The currently executing deployment
  • The step as part of a redeployment

Your script may look something similar to this (apologies, I am a PowerShell user but the Python equivalent can be found in the link above):

# Use your script here to retrieve the ECR Image Tag and store this into a variable.

$ECRImageTag =  "image_tag-XYZ987-current"
Set-OctopusVariable -name "ECR_Image_Tag" -value "$ECRImageTag"

The concept here being that you retrieve the Image Tag and store this into a variable for the -value

Then you can look to install (if you are authorized to do so) the Community Step Template Save Octopus Output Variable.

Add this step to your process to update the variable in you Global Variable Set.

So my example would look like this:

Runbook Process:

Global Variable Before:

Configuration for step Save Octopus Output Variable To Global Variables:

Variable after my Runbook has completed successfully:

I hope this helps get you started.

Thanks
Doug

1 Like

Thank you Doug,
It’s definitely solves my problem!

1 Like