How to reference files within a package

Hi,

I need to reference files (exe, txt etc) within a package to do things to them or to execute them. I’m sure there is more than one way to do this and would like to see all the options.

I want to reference and manipulate files both pre and post deploy via powershell scripts (non embedded) and am aware that there may be different methods to achieve each goal.

Some ideas i have picked up from the web are:

  • just providing a relative path eg) “./subfolder/file.txt”
  • referencing the custom install directory “Octopus.Action.Package.CustomInstallationDirectory” or “$OctopusParameters[“Octopus.Action.Package.CustomInstallationDirectory”]”

If you could provide some examples for both pre and post deploy that would be great

Thanks

Hi Leigh,

Thanks for getting in touch!

To reference files from the package you can use the below as a guide to where the paramters are pointing to, then it’s just a case of using the values of these parameters to build up the absolute path to the file(s) that you want to manipulate or execute.

Relative paths will depend on a couple of things, if it’s in a pre- or post-deploy script and if a custom installation directory has been defined. See below sections for differences.

$OctopusParameters['Octopus.Action.Package.CustomInstallationDirectory'] will only be available if the Custom installation directory feature of the deploy step has been enabled.

In pre-deploy scripts:

  • $OctopusParameters['Octopus.Action.Package.CustomInstallationDirectory'] will be the path to where the contents of the package will be copied to once extracted, this could either not exist or contain the files from the previous release, not the new files being deployed.

  • $OctopusParameters['Octopus.Action.Output.Package.InstallationDirectoryPath'] will be where the contents of the package has been extracted

  • The working directory for the Tentacle is the directory where the content of the package has been extracted to

In post-deploy scripts:

  • $OctopusParameters['Octopus.Action.Package.CustomInstallationDirectory'] will be the path to where the contents of the package has been copied to.

  • $OctopusParameters['Octopus.Action.Output.Package.InstallationDirectoryPath'] will be the same as the above

  • $OctopusParameters['OctopusOriginalPackageDirectoryPath'] will be where the contents of the package has been extracted

  • The working directory for the Tentacle is the final destination for the contents of the package, either $OctopusParameters['Octopus.Action.Output.Package.InstallationDirectoryPath'], or if it’s been specified $OctopusParameters['Octopus.Action.Package.CustomInstallationDirectory']

So if you want to reference file.txt in subfolder within the package, you could do the following:

    $extractPath = $OctopusParameters['Octopus.Action.Output.Package.InstallationDirectoryPath'] # in pre-deploy, in post-deploy if custom installation directory has not been defined
    $customPath = $OctopusParameters['Octopus.Action.Package.CustomInstallationDirectory'] # if a custom installation directory has been defined
    # original extract path
    Get-Content $extractPath\subfolder\file.txt
    # or when a custom installation directory has been define
    Get-Content $customPath\subfolder\file.txt
    # or as a relative path from the Tentacle's working directory
    Get-Content .\subfolder\file.txt

Hope that helps!

Henrik

Awesome, very thorough explanation. Thanks.

Hi!

Has this changed since the release of Octopus 3.0? I think it would be a great addition to the documentation. I have been searching high and low for information about how Tentacles treat working directories when executing scripts.

If there is something I can do to help, I would be happy to help.

Regards,
Johan Suleiko Allansson

Hi Johan,

I’ve raised a GitHub issue to add a documentation page about how to reference files in a package from pre- and post-deploy scripts. You can track the progress of the issue here

Thank you and kind regards,
Henrik

Hi,

We have now created a docs page that outlines how to reference files in a package, http://docs.octopusdeploy.com/display/OD/Reference+files+within+a+package

Thanks,
Henrik