Running a script after deployment in the final destination folder instead of the work folder

I’m using the “Run a script” step after deploying the package to my server. The script itself is stored within the package, as I want to be able to update it in future releases.

Now the script runs on the server correctly, but it’s running within the context of the temporary folder. So with the Linux tentacle I’m using, it’s running in a temporary folder in /etc/octopus/default/Work/. After the script runs, that folder is moved to it’s final destination, /home/Octopus/Applications/dev/my-app/1.0.0.

The problem is I really need the script to be running in the context of the final destination, as one step of the script is registering a new service that references the content of the package. Since the temporary folder gets cleaned up after the deployment is done, the service ends up referencing files that don’t exist anymore.

So in short, how can I run a script that’s present in the deployed package, after it has finished deploying to its final destination?

Hi @mathieu

I think the easiest way would be to utilise the run a script step, and then to locate the script that’s been deployed as part of your package deployment.

Suppose we had a deploy a package step named Deploy a Package. When executing it will output the final extract location as you mentioned:

Using a second step, a Run a Script step, you can utilise a built-in Octopus output variable to get that path, and can then execute any scripts from within that directory. I’m using PowerShell core in the below example, but you could do something similar in Bash:

$InstalledPath = $OctopusParameters["Octopus.Action[Deploy a Package].Output.Package.InstallationDirectoryPath"]
Write-Host "InstalledPath: $InstalledPath"
Set-Location $InstalledPath

 #Execute script to register service here

The key part to this is getting the final extraction directory. The octopus variable Octopus.Action[Deploy a Package].Output.Package.InstallationDirectoryPath is what stores this.

You’d replace Deploy a Package in the above example with the name given to your deploy a package step.

I hope that helps!

Hey @mark.harrison, thanks for the detailed instructions. I’m trying it right now and it’s working great!

I tried a lot of things but none of it was working. Using that octopus variable was key, calling the newly deployed bash script in my package now and registering the new service is all done through my Octopus deployment.

Hi @mathieu

That’s excellent news, I’m glad to hear that worked for you :slight_smile:

Happy deployments!