Abort Step from Script

I have a Step Template that I don’t want to to run when executed for a specific Tenant.

The built in checks available for checking Tenants can’t be used because the Step Template does not get access to those. (They are with the project using the Step Template). Additionally, when copying a project, all tenant specific settings are all lost.

I can make the check in PowerShell to know if I should abort the step. I just need a command to run that will cause the step to abort. But it needs to do so without causing the step to fail.

Is there a command I can run from a PowerShell Pre-Deployment script that will cause a step to abort execution?

Hi @OctopusSchaff,

Thanks for reaching out.

I think you have a couple of ways you can approach this.

You can bake the logic for the check into your step template, and Exit 0 after checking the tenant

or

Create a step that does the check and creates a true/false output variable then do one of the following:

  • Use that output variable as a run condition for your step (Recommended approach)
  • Use that output variable in your step template to Exit 0

Documentation to expand upon the above subjects:

Please let me know if you think that will work for your use case or if you have further questions.

Best,
Jeremy

I would rather not use two steps. And I can’t anyway, as the “Conditions” section of a step is not available in a Step Template. (I am trying to setup a template for all my projects to use.)

I was excited about using Exit 0 (your description of it seems to be exactly what I need), but it did not have any effect different from just calling return in my Pre-deployment script. (The rest of the Step Template was run despite calling Exit 0.)

Is there something I need to do to make Exit 0 cause the step to abort execution of the rest of the run?

Hi @OctopusSchaff,

You’re correct that you can’t add a run condition to a step template itself, but you can add a Run Condition to the Step Template after you’ve added it to the deployment process.

The advantage of doing it in 2 steps would be in the future you could use that output variable and run condition if you eventually end up needing multiple steps that you want to exclude that tenant from.

If you don’t want to add a new step, you could also do it in a pre-post deployment script step on another step.

Regarding Exit 0 not working, I tested both

Write-Highlight "Testing Return"

Return

Write-Highlight "Return didn't work"

and

Write-Highlight "Testing Exit 0"

Exit 0

Write-Highlight "Exit didn't work"

Both had the step stop processing and move on to the next step without failing. In your case, what happened?

When you say abort execution of the rest of the run, do you mean all of the following steps for that tenant too, or just the current step?

Please let me know.

Best,

Jeremy

You’re correct that you can’t add a run condition to a step template itself, but you can add a Run Condition to the Step Template after you’ve added it to the deployment process.

The advantage of doing it in 2 steps would be in the future you could use that output variable and run condition if you eventually end up needing multiple steps that you want to exclude that tenant from.

Normally I would just do that. However, I am creating an Octopus Deploy Project that will be copied to create all my new Octopus Deploy Projects. Since the conditions I want to put in are related to Tenants that will not work. (Because all Tenant related settings are lost when a project is copied.)

Regarding Exit 0 not working

For my Octopus Deploy Project, In both cases (Exit 0 and return), the script exits, but the step keeps on running. My step is a “Update Helm Chart” step. I run that script (with Exit 0 in it) as a Pre-Deployment script. The Pre-Deployment script ends when I call Exit 0, but then it goes on to try to run the Helm Chart (which fails because the script exited early so the needed variables are not set).

I am running this on a Linux worker hosted in Docker. If you are not seeing this in your tests, I would like to suggest you try it with a worker that is a Linux Docker based worker. (I have noticed that there are some functionality differences between the Linux Docker workers and the standard Windows workers.)

Hi @OctopusSchaff,

My mistake, I missed the pre-deployment script portion on my first response. I thought you were doing the logic inside of a normal script step. The pre-deployment script is its own instance of PowerShell, so Exit 0’ing that will just end the pre-deployment script, which will then cause the rest of the step to kick off. I apologize for missing that.

I’m going to reach out to some colleagues to see if there is a better way to approach this other than Run Conditions.

Please let me know if you have any other questions in the meantime.

Best,
Jeremy

I went to put in the conditional for the Tenant (as a temporary measure) and realized that I have added a Channel that aligns with the tenants that I don’t want to have the step run for. I tested adding a conditional based off the Channel and it seems to work.

Assuming that Channel settings do not get removed like Tenant settings during a project copy, I should be able to key off of that for skipping the step.

While I would prefer to keep that in the step template, since it seems there is no way to programmatically abort a Helm step from within the step, I should be good to use a conditional on the Channel.

Thank you for your help!

Hi @OctopusSchaff,

You’re very welcome. Thanks for letting me know how you got it resolved.

Good idea, I think that’s likely the path of least resistance.

Another thing that got floated would be to use our new Bento(import export) tool. Exporting and Importing Projects - Octopus Deploy

Your option is likely simpler, but I wanted to pass it along in case you need another option in the future. The caveat here would be that when you import the project, it will have the same name as when you exported it, so you would need to make your base project, export the project, rename your base project to something else, then as part of your automation for cloning, rename the project immediately after an import.

Please let us know if you have any other questions.

I hope you have a great weekend.

Best,
Jeremy

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.