Octopus lets you define variables with values that change based on the scope you've assigned to the variables and the scope of your deployments. For instance, as you define your deployment processes there will be steps in your projects that need different database connection strings (or any number of other parameters, i.e., application settings, web service URLs, etc) depending on the environment you're deploying to. Using variables lets you define a variable with one connection string scoped, for instance, to the test environment, and another connection string scoped to the production environment.
Using variables means you don't need to hardcode any of these values. You define your variables and the values you provide will be used at deployment time, allowing you to create applications and deployments that are agnostic of the target environment.
Creating Hello World Variables
In this example, we'll add a variable to a Hello World project that runs a script to say hello. The project uses a variable to vary the message it displays based on the environment the script is deployed to.
- To add a variable to your project, navigate to the Project's Overview page, and click Variables to access the variable editor.
- Give the variable a name, for instance, Greeting.
- Enter the first value for the variable, for instance, Hello, Test, in the value field.
- Define the scope for the value, for instance, by selecting the Test environment.
- Click ADD ANOTHER VALUE and enter the second value for the variable, for instance, Hello, Production.
- Define the scope for this value, for instance, by selecting the Production environment.
- Save the variable by clicking SAVE.
- In this example, we'll reference this variable from a Run a Script step.
- Define your step and in the Script Content section, enter the following PowerShell script into the script editor:
- Select the variable Greeting from the insert variable tool (#) next to the script editor, and click SAVE
When a release of the project is deployed, the script step will run with the string Hello, Test on the Test environment, and with the string Hello, Production, on the Production environment.
Variables in Octopus
Variables are an important and useful concept in Octopus, so this section describes different ways in which variables can be used.
|Scoping variables||Variables can be scoped to different environments, deployment targets, target roles, deployment steps, channels, and tenants which allows you to provide different values for the variables for each scope.|
|Binding syntax||Octopus's binding syntax lets you reference variables throughout Octopus. You can also reference variables from other variables.|
|Library variable sets||Library variable sets let you re-use common variables between projects rather than creating them for every project that needs them.|
|Prompted variables||Sometimes the value of a variable changes for each deployment. You can prompt a user for a variable value when scheduling deployments.|
|Sensitive variables||Octopus can securely store sensitive values, for instance, passwords and API keys.|
|System variables||Many built-in variables are available within Octopus, such as the current environment name.|
|Output variables||Output variables let you programmatically set variables during a deployment, and then use those values in subsequent steps.|
|Variable templates||Variable template are used with projects that are deployed to Multi-tenants and let you define which variables are required by tenants for your projects to be successfully deployed.|
|Certificate variables||Octopus supports a certificate variable type that lets you create a variable with a certificate managed by Octopus as the value.|
|Variable Substitution Syntax||Variable substitutions are a flexible way to adjust configuration based on your variables and the context of your deployment.|
|AWS Account Variables||AWS accounts are included in a project through a project variable of the type Amazon Web Services Account.|
|Azure Account Variables||Azure accounts can be referenced in a project through a project variable of the type Azure Account.|