JetBrains Space Help

Automation Environment Variables

In your automation script, you have access to a number of Automation environment variables. Use these variables to get data about the script run context: for example, current Git branch name and Git revision number, Space URL, and so on.

If you want to get environment variables in a shell script, note that you need to escape the $ symbol in a Kotlin way, for example, ${'$'}JB_SPACE_API_URL. Also, you can get some of the Automation environment variables using the the API commands (only inside the kotlinScript block). For example:

job("Example") { container(displayName = "Show key using shell", image = "ubuntu:latest") { shellScript { content = """ echo Project key: ${'$'}JB_SPACE_PROJECT_KEY """ } } container(displayName = "Show key using api", image = "openjdk:11.0.3-jdk") { kotlinScript { api -> // get env var from system println("Project key: " + System.getenv("JB_SPACE_PROJECT_KEY")) // get env var using API println("Project key: " + api.projectKey()) } } }

When does Automation resolve environment variables

The work of an Automation script consists of two phases:

  1. Script compilation: Automation checks the .space.kts file for errors and compiles the necessary script parts. This phase is performed in a separate container that cannot be anyhow accessed from within the script. Automation environment variables are not resolved on this phase.

  2. Script run: Automation runs the content of kotlinScript, shellScript, and docker.beforeBuildScript in a container. On this phase, Automation environment variables are resolved inside the specified container. This means that you can access Automation environment variables only inside the kotlinScript, shellScript, and beforeBuildScript items.

General

VariableAPI methodDescription
JB_SPACE_API_URLspaceUrl(): StringThe URL of your JetBrains Space instance. For example: mycompany.jetbrains.space

Authentication

VariableAPI methodDescription
JB_SPACE_CLIENT_ID, JB_SPACE_CLIENT_SECRETspaceClientId(): String, spaceClientSecret(): StringTemporary OAuth 2.0 credentials issued to the current script. The script uses them to authenticate in various Space modules. For example, you may need them to authenticate in a Packages repository in order to publish script artifacts.

Project settings

VariableAPI methodDescription
JB_SPACE_GIT_REVISIONgitRevision(): StringThe current Git revision number
JB_SPACE_GIT_BRANCHgitBranch(): StringThe current Git branch name
JB_SPACE_PROJECT_IDprojectId(): StringThe ID of the current project
JB_SPACE_PROJECT_KEYprojectKey(): StringThe key of the current project
JB_SPACE_GIT_REPOSITORY_NAMEgitRepositoryName(): StringThe name of the current Git repository (the one that contains the currently running .space.kts file)

Automation

VariableAPI methodDescription
JB_SPACE_EXECUTION_NUMBERexecutionNumber(): StringThe current build number. For example, you can use it to generate application version number. Learn more

External workers

External workers let you run your jobs on your own host machines. Learn more

VariableAPI methodDescription
SPACE_WORKER_SERVERURLThe URL of the JetBrains Space instance assigned to the worker. For example: mycompany.jetbrains.space
SPACE_WORKER_TOKENAn authorization token issued to the external worker when you register the worker in Space. The worker uses it to authenticate in Space.
SPACE_WORKER_DATADIRThe path to the directory where the script downloads all required data: project sources, file share, and Automation-specific data.
SPACE_WORKER_CPU, SPACE_WORKER_MEMThe system resources available on the host machine that runs the worker agent: CPU cores in mCPU, and RAM in MB. These values are used to help Automation select the suitable worker for a job. Learn more

Example. Use environment variables to generate a product version

For example, you want to generate a Docker container tag based on the current branch name and the job run number like version-0.123-mybranch:

job("Build and push image") { docker { beforeBuildScript { // Create an env variable BRANCH, // use env var to get full branch name, // leave only the branch name without the 'refs/heads/' path content = """ export BRANCH=${'$'}(echo ${'$'}JB_SPACE_GIT_BRANCH | cut -d'/' -f 3) """ } build { context = "docker" file = "./docker/Dockerfile" } push("mycompany.registry.jetbrains.space/p/pkey/mydocker/myimage") { // Use the BRANCH and JB_SPACE_EXECUTION_NUMBER env vars tag = "version-0.\$JB_SPACE_EXECUTION_NUMBER-\$BRANCH" } } }
Last modified: 27 August 2021