Parameters and Secrets
Parameters are name-value pairs, defined by a user or provided by Automation. The main goal of parameters is to pass various data to your jobs. For example, this might be a Docker image name, a URL, a command-line argument, etc. Or it can be an access token or a password. Such sensitive parameters are called secrets.
Use parameters in jobs
To get a parameter in a job, specify its name in a string inside double curly braces: "{{ my-param }}"
. You can do this in any string inside any DSL block excluding startOn
, git
, and kotlinScript
.
If your script contains strings with curly braces that are not used for parameters, you should escape them: \\{{ ... }}
and/or {{ ... \\}}
.
For example, this is how you can reference project parameters:
You can also define parameters right in a job using the job.parameters.text
block.
Use secrets in jobs
You can reference secrets the same way as you can reference parameters, but in a limited number of places. Learn more
You can also save secrets in files. For example, this is how you can provide an SSH key:
Use parameters and secrets in kotlinScript
To get parameters inside the kotlinScript
blocks, use one of the options:
Environment variables
Project-wide parameters and secrets
Project parameters are available inside any Automation job within a project. There are two types of project parameters:
Plain text parameters – typically, these are parameters frequently used throughout your Automation scripts. For example, a URL of an external service.
Secrets – parameters stored in the AES-encrypted format. For example, a password or an access token to an external service. Automation hides secret values in job logs for security purposes.
Once you delete a secret from a project, it is instantly deleted from the storage (if there are running jobs that use the secret, it will be deleted once the jobs finish running).
To define project secrets and parameters
On the project sidebar menu, choose Settings, then Secrets and Parameters.
Click Create and choose Secret or Parameter.
Specify
Key: a parameter name. You will use this name to reference this parameter in your scripts.
Value: a parameter value.
Limitations:
Secrets and parameters exist only in the scope of a particular project. So, if you create a secret or a parameter in one project, you cannot use them in other projects.
Keys of secrets and parameters must be unique within a project. A secret and a parameter with the same key are also not allowed.
A key must be no longer than 128 characters and can only contain alphanumeric characters (
[a-z]
,[A-Z]
,[0-9]
), dashes (-
), or underscores (_
).Max secret's or parameter's value size is 30KB. Note that the max total size of all container arguments, environment variables, secrets, and parameters is also limited by 30KB. For example, if you provide a secret of 30KB and an argument of 1KB to a step, the step will fail.
To use project secrets and parameters
Follow the rules:
To get a project parameter or secret, use its key preceded with the
project:
prefix:"{{ project:param-key }}"
.You can get secrets only inside the
job.parameters
,job.container.env
,job.container.shellScript.args
, andjob.container.args
blocks (same refers to all correspondingjob.host
blocks).
For example:
job("Use project params") { // E.g., a parameter and a secret // 'bintray-repo-url' and 'bintray-repo-password' // exist in the project settings parameters { // you can assign project secrets and params to configured params text("url", value = "{{ project:bintray-repo-url }}") secret("password", value = "{{ project:bintray-repo-password }}") } // Get project parameter values in a shell script container(displayName = "Print param value shell", image = "ubuntu") { env["URL"] = "{{ project:bintray-repo-url }}" // The only way to get a secret in a shell script is an env variable env["PSWRD"] = "{{ project:bintray-repo-password }}" shellScript { content = """ echo "Url from conf param - {{ url }}" echo "Url from env var - ${'$'}URL" echo "Password from env var - ${'$'}PSWRD" echo "Url from project param directly - {{ project:bintray-repo-url }}" # It's not possible to directly reference secrets from a shell script. # The next commented lines would fail. # echo "Password from conf param - {{ password }}" # echo "Password from project param - {{ project:bintray-repo-password }}" """ // Output: // Url from conf param - http://example.com // Url from env var - http://example.com // Password from env var - ***** // Url from project param directly - http://example.com } } // Get project parameter values in a Kotlin script container(displayName = "Print param value kotlin", image = "amazoncorretto:17-alpine") { env["URL"] = "{{ project:bintray-repo-url }}" env["PSWRD"] = "{{ project:bintray-repo-password }}" kotlinScript { api -> println("Url from env var - " + System.getenv("URL")) println("Url from API - " + api.parameters["url"]) println("Password from env var" + System.getenv("PSWRD")) // An advanced use case is the Ref class that holds a // reference to a secret or a parameter: // Ref("project:my-param").toString() == "{{ project:my-param }} // See the example below api.secrets["service-password"] = Ref("project:bintray-repo-password") println("Password from Ref" + api.secrets["service-password"]) // Output: // Url from env var - http://example.com // Url from env API - http://example.com // Password from env var - ***** // Password from Ref - ***** } } }To see in which jobs a particular secret or parameter is used, open Secrets and Parameters in project settings and click Show usages next to the desired secret or parameter.
- Migrate from legacy secrets and parameters
Earlier, it was possible to use the
Secrets
andParams
functions for getting values of project secrets and parameters. These functions are deprecated and will be removed in the future. Therefore, if your Automation scripts use theSecrets
orParams
functions, we strongly recommend that you replace them with thejob.parameters.secret
andjob.parameters.text
blocks.Legacy script
job("Legacy way to use secrets and params") { container(displayName = "Show pwd", image = "ubuntu") { env["URL"] = Params("bintray-repo-url") env["PSWRD"] = Secrets("bintray-repo-password") shellScript { content = """ echo My password for ${'$'}URL echo is ${'$'}PSWRD """ } } }Replace with
job("New way to use secrets and params") { parameters { text("url", "{{ project:bintray-repo-url }}") secret("password", "{{ project:bintray-repo-password }}") } container(displayName = "Show pwd", image = "ubuntu") { env["URL"] = "{{ url }}" env["PSWRD"] = "{{ password }}" shellScript { content = """ echo My password for ${'$'}URL echo is ${'$'}PSWRD """ } } }
Customize jobs with Custom Run
Users can change the default parameter value before running the script. To run a job with customized parameters, they must use the Custom Run option.
The job.parameters.text
function lets you define various types of customizable parameters. The table below shows the possible options and their representation in the Custom Run window.
// Simple text parameter with optional description
text("text-param", value = "default-value", description = "This is a text param") |
// Text parameter with no default value. The job will fail
// if a user doesn't specify its value before a custom run
text("param-no-default") |
// Multiline text parameter
text("param-multiline", value = "default-value", multiline = true) |
// Text parameter that doesn't allow changing its default value
text("param-no-override", value = "non-overridable-value", allowCustomRunOverride = false) |
// Drop-down list of values
text("param-options", value = "two") {
options("one", "two", "three")
} |
// List with multiple value choice
text("param-options-multiple-choice", value = "two") {
options("one", "two", "three") {
allowMultiple = true
// optionally, you can specify how the values must be separated
valueSeparator = ","
}
} |
// Project secret
secret("token", value = "{{ project:auth-token }}", description = "Auth token") |
For example, this is how a customizable deployment script may look like:
Pass parameters between steps
Sometimes it is necessary to pass execution context (e.g., script run results) from one Automation step to another. To create a parameter or a secret from within a step, you should use the parameters
API. The API is available only inside the kotlinScript
blocks: the container that runs the step must include JRE/JDK 11 or later.
For example, you can use this feature to run a step in an image built in the previous step:
Unlike container
steps, inside a host
step, you can run a number of subsequent scripts (i.e., shellScript
and kotlinScript
blocks). But there is a limitation: parameters defined within a script will not be accessible in subsequent scripts within the same host
step. For example, the following job will fail:
Fallback values for parameters
Sometimes, a parameter value might be missing during a job run: The parameter may not exist in the current context or may resolve to an empty string. For example, when creating a job that normally runs on a merge request, you might reference the review number like {{ run:review.id }}
. However, if you run the job manually or with another trigger, the review ID value will be missing, and the job will fail. To prevent this, specify a fallback value for the parameter: {{ run:review.id ?: 'no-review'}}
. The no-review
fallback value will be used instead if the review ID is missing.
The syntax for specifying fallback values is {{ parameter-name ?: <fallback> }}
. Here fallback
is a template expression that lets you reference any sequence of fallback parameters or values like {{ param1 ?: param2 ?: 'If no params 1 and 2, this string will be the value' }}
. For example:
In kotlinScript
, you can use the native Kotlin check for nullability:
Vault parameters
In addition to its own secret storage, Space also supports external HashiCorp Vault storage. Once you configure a connection to a Vault server, you can use variables from the storage the same way you use project parameters.
- Prerequisites
You have a working Vault server with a configured AppRole. The server stores secrets required by the build script.
To configure connection to a Vault server
On the project sidebar menu, choose Settings, then Vault Connections.
Click New connection and specify a connection Name and other settings:
Vault URL: a URL of the Vault server in the
https://vaultserver:port
format.AppRole ID and AppRole Secret ID: credentials used by Space to log in to the Vault server.
Parameters namespace: (optional) an additional connection identifier. If a project has multiple Vault connections, Parameters namespace lets you specify which connection must be used to resolve a particular parameter.
Vault namespace: (optional) a Vault namespace that is used in multi-tenant Vault configurations.
Click Test connection and if connection is successfull, click Create.
To create a Vault parameter
On the project sidebar menu, choose Settings, then Secrets and Parameters.
Click Create and choose Vault parameter.
Specify
Key: a variable name. You will use this name to reference this variable in your scripts.
Path: a Vault secret path in the Key/Value v1 or v2 secrets engine format. For example,
/aws/sts/mysecret
.Field: (optional) a field name. If a secret has multiple fields, specify the field which value you want to get in an Automation job. If a secret has multiple fields, but you don't specify a field name, Automation will try to get a field named
value
.Namespace: (optional) an identifier of the Vault connection that must resolve the parameter. The Namespace must match the parameters namespace specified for the Vault connection.
Click Save.
To use Vault parameters in a job
Follow the rules:
To get a project parameter or secret, use the
vault:namespace:/secret_path/field_name
format. For example:vault:/secret/password
: get a value of the secret that has only one field namedpassword
.vault:/secret/credentials!/password
: get a value of thepassword
field of the/secret/credentials!
secret.vault:/secret/json!/store.books[0].category
: get a value corresponding to the JsonPath search query (dot notation) in the/secret/json
secret stored as a JSON object.vault:/secret/json!/['store'].['books'][0].['category']
: get a value corresponding to the JsonPath search query (bracket notation) in the/secret/json
secret stored as a JSON object.vault:test:/secret/credentials!/password
: get a value of thepassword
field of the/secret/credentials
secret using the Vault connection with thetest
parameters namespace.
You can get Vault parameters only inside the
job.parameters
,job.container.env
,job.container.shellScript.args
, andjob.container.args
blocks (same refers to all correspondingjob.host
blocks).
For example:
job("Build") { // reference `username` field of the `/secret/data/credentials!` secret // using the parameters block parameters { secret("username", "{{ vault:secret/data/credentials!/username }}") } container("ubuntu:latest") { // get Vault parameter directly env["PSWRD"] = "{{ vault:/secret/data/credentials!/password }}" // get Vault parameter from defined parameter env["USERNAME"] = "{{ username }}" shellScript { content = """ echo "Username from env var - ${'$'}USERNAME" echo "Password from env var - ${'$'}PSWRD" # It's not possible to directly reference secrets from a shell script. # The next commented lines would fail. # echo "Username from conf param - {{ username }}" """ } } }To see in which jobs a particular Vault parameter is used, open Secrets and Parameters in project settings and click Show usages next to the desired parameter.
View parameter values after a job run
Navigate to the project.
On the sidebar menu, choose Jobs.
Select a job.
To view all parameters in a job, open the Parameters tab.
To view parameters in a particular step, open the Steps tab, choose a particular step, and open its Parameters tab.
Provided parameters reference
Automation provides a number of predefined parameters which you can use in your build scripts, e.g., a job run number, a Git branch name and commit ID, a URL of your Space instance, and so on.
There are several ways to get the value of a provided parameter (some of these ways may not be available for certain variables):
Use a predefined parameter name, e.g.,
"{{ run:number }}"
. Doesn't work inkotlinScript
.Use a predefined environment variable, e.g.
$JB_SPACE_EXECUTION_NUMBER
.Inside a
kotlinScript
block:Use an API method that returns the required variable value, e.g.,
api.executionNumber()
. The corresponding API methods always returnString
values.Use the
parameters
API to get values by parameter names, e.g.,api.parameters["run:number"]
.
For example:
Predefined name | Description |
---|---|
— | The URL of your JetBrains Space instance. For example:
|
| The ID of the current project.
|
| The key of the current project.
|
| The name of the default cache repository. |
— | Temporary OAuth 2.0 access token issued to the current script. The script can use it to authorize in various Space modules. For example, you can use it to authorize the script in a Packages repository in order to publish build artifacts.
|
— | As an alternative to an access token, the script can authorize with temporary OAuth 2.0 credentials.
|
| The current Git commit ID.
|
| The current Git branch.
|
| A comma-separated list of the checked-out repositories:
|
| The trigger used to initiate job run. Possible values: |
| The ID of the user who triggered job run. Relevant only if the job was run manually by a Space user. |
| The ID of the application that triggered job run. Relevant only if the job was run by an HTTP API call from a registered application. |
| The ID of the user who triggered safe-merge run. Relevant only if the safe-merge job was run manually by a Space user. |
| The current Git branch. Relevant only if a user have chosen a particular commit during custom run. |
| The current Git commit ID. Relevant only if a user have chosen a particular commit during custom run. |
| The ID of the commit that triggered job run. Relevant only if the gitPush trigger is enabled. |
| The ID of the commit that ref was pointing to before this push. Relevant only if the gitPush trigger is enabled. |
| The branch which triggered job run. Relevant only if the gitPush trigger is enabled. |
| The repository which triggered job run. Relevant only if the gitPush trigger is enabled. |
| The list of branches/tags that were deleted. Relevant only if the gitBranchDeleted trigger is enabled. A comma is used as a separator, e.g., |
| The ID of the review that triggered job run. Relevant only if the codeReviewOpened or codeReviewClosed trigger is enabled. |
| The branch that goes through the code review. Relevant only if the codeReviewOpened or codeReviewClosed trigger is enabled. |
| The ID of the commit that goes through the code review. Relevant only if the codeReviewOpened or codeReviewClosed trigger is enabled. |
| The branch where the changes are supposed to be merged after the code review. Relevant only if the codeReviewOpened or codeReviewClosed trigger is enabled. |
| The ID of the commit that is supposed to be merged after the code review. Relevant only if the codeReviewOpened or codeReviewClosed trigger is enabled. |
| The ID of the review that triggered job run. Relevant only if the codeReviewOpened, codeReviewClosed, gitPush (within a merge request) manual (from a merge request page) or safeMerge trigger is enabled. |
| The branch that goes through the code review. Relevant only if the codeReviewOpened, codeReviewClosed, gitPush (within a merge request) manual (from a merge request) or safeMerge trigger is enabled. |
| The ID of the commit that goes through the code review. Relevant only if the codeReviewOpened, codeReviewClosed, gitPush (within a merge request) manual (from a merge request) or safeMerge trigger is enabled. |
| The branch where the changes are supposed to be merged after the code review. Relevant only if the codeReviewOpened, codeReviewClosed, gitPush (within a merge request) manual (from a merge request) or safeMerge trigger is enabled. |
| The ID of the commit that is supposed to be merged after the code review. Relevant only if the codeReviewOpened, codeReviewClosed, gitPush (within a merge request) manual (from a merge request) or safeMerge trigger is enabled. |
| In-project merge request number. Relevant only if the codeReviewOpened, codeReviewClosed, gitPush (within a merge request) manual (from a merge request) or safeMerge trigger is enabled. |
| A unique ID of the current run (build). You can use it to refer to a particular build using the Space API.
|
| A unique ID of the current job. |
| The name of the Git repository where the current |
| The current job run number (build number). For example, you can use it to generate application version number. Learn more
|
| A link to the current job execution.
|
— | The URL of the JetBrains Space instance assigned to the worker. For example,
|
— | An authorization token issued to the worker. The worker uses it to authenticate in Space.
|
— | The path to the directory where the script downloads all required data: project sources, file share, and Automation-specific data.
|
— | A host name of the worker.
|
| The system resources available on the host machine that runs the worker agent: CPU cores in mCPU, and RAM in MB.
|
| The hostname of the worker machine. |
| Target CPU architecture of the operating system (OS), OS type, and OS version on the worker. |