Variables
Set the environment variables of a workspace, a service, a sandbox or a pipeline job.
Last updated
On this page
Description and operation
General
A variable is a NAME=value environment variable that a resource gives to its processes. You set the variables of a workspace, a service, a sandbox or a pipeline job in its env field. The value is stored in plain text in the definition of the resource. Use a secret for a password, a key or a token.
A value that is exactly ${{ secrets.NAME }} is a reference: the variable gets the value of that secret instead. See Reference syntax.
| Part | Description |
|---|---|
| Name | The name that the process reads, for example LOG_LEVEL. |
| Value | Plain text, or a reference to a secret. |
env | The field that holds all variables of the resource, as a map of name to value. |
The variables belong to the resource, and its owner sets them. The environment document returns the variables of each service. The workspace and sandbox documents do not return env.
Where you set variables
| Resource | Field | API route | CLI command |
|---|---|---|---|
| Workspace | env | POST /v1/workspaces, PUT /v1/workspaces/{id}/env | kloudlite workspaces create --env, kloudlite workspaces set-env |
| Workspace in a session | env | POST /v1/sessions/{id}/workspaces | kloudlite sessions create-workspace --env |
| Service of an environment | env of each service | PATCH /v1/environments/{id} | kloudlite environments set-services |
| Sandbox | env, at creation only | POST /v1/sandboxes, POST /v1/sessions/{id}/sandboxes | kloudlite sandboxes create --env, kloudlite sessions create-sandbox --env |
| Pipeline job | env of the job or of a step | POST /v1/pipelines/{owner}, PUT /v1/pipelines/{owner}/{name} | kloudlite pipelines update |
Parameters
| Parameter | Required | Description |
|---|---|---|
| Name | Yes | Letters, digits and _. It does not start with a digit. |
| Value | Yes | Plain text, or exactly ${{ secrets.NAME }}. In a pipeline, plain text can hold expressions. |
A workspace and a sandbox refuse the name HOME and each name that starts with KL_, because the platform sets them.
Lifecycle
- Set. A call that sets
envreplaces the whole map. A variable that is not in the new map is removed. - Workspace. A new
envreaches the processes at the next start. A running workspace keeps the variables that it has. - Service. A new
envreaches the service at its next start. A running service keeps the variables that it has. - Restart required. A running workspace or service with old variables shows
restart_required. The message names the changed variables and never shows a value. A start for any cause gives the current variables and removes the mark. See Restart to apply new variables. - Automatic restart. Set
restartOnEnvChangetotrueto restart the resource when its variables change. The default isfalse. - Sandbox. The variables are set at creation and do not change.
- Pipeline job. A step gets the
envof the job, with theenvof the step over it. - Clone and restore. A clone or a restore of a workspace has no variables. Set them again on the new workspace.
Quota and limits
Variables do not count against the quota of the owner.
| Item | Limit |
|---|---|
| Variables of a workspace or a sandbox | 64. |
| Value in a workspace or a sandbox | 32 KiB. |
| Reserved names in a workspace or a sandbox | HOME, and each name that starts with KL_. |
Tasks
Set the variables of a workspace
Reason for the task: Give the processes of a workspace a set of variables.
Job set-up
- Access: you. Only the owner of the workspace can set its variables.
- References:
PUT /v1/workspaces/{id}/env,kloudlite workspaces set-env.
The call replaces all variables of the workspace. A variable that is not in the new list is removed.
Procedure
Send the complete list of variables.
from kloudlite import Kloudlitekl = Kloudlite()kl.workspaces.set_env(workspace="acme/api", env={"LOG_LEVEL": "debug", "PORT": "3000"})import { Kloudlite } from "./index.ts";const kl = new Kloudlite();await kl.workspaces.setEnv({ workspace: "acme/api", env: { LOG_LEVEL: "debug", PORT: "3000" } });kl := kloudlite.New(kloudlite.Options{})if _, err := kl.Workspaces.SetEnv("acme/api", map[string]string{"LOG_LEVEL": "debug", "PORT": "3000"}); err != nil { log.Fatal(err)}require_relative "kloudlite"kl = Kloudlite::Client.newkl.workspaces.set_env(workspace: "acme/api", env: { "LOG_LEVEL" => "debug", "PORT" => "3000" })var kl = new Kloudlite();kl.workspaces().setEnv("acme/api", Map.of("LOG_LEVEL", "debug", "PORT", "3000"));kloudlite workspaces set-env acme/api --env LOG_LEVEL=debug --env PORT=3000Send a restart for the workspace. See Restart to apply new variables.
Close-out
- Run
printenv LOG_LEVELin the workspace. Make sure that the output isdebug.
Set the variables of a service
Reason for the task: Give a service of an environment a set of variables.
Job set-up
- Access: you. A team environment needs team membership.
- Conditions: the service exists. See Add a service.
- References:
PATCH /v1/environments/{id},kloudlite environments set-services.
A changed env does not restart the service. The service keeps its old variables until you restart it.
Procedure
Send the service list with the new
envof the service, hereapi.env = kl.environments.get(environment="acme-dev")services = [{**sv, "env": {**sv["env"], "LOG_LEVEL": "debug"}} if sv["name"] == "api" else sv for sv in env["services"]]kl.environments.set_services(environment="acme-dev", services=services)import { type Service } from "./index.ts";const env = (await kl.environments.get({ environment: "acme-dev" })) as { services: Service[] };const services = env.services.map((sv) => (sv.name === "api" ? { ...sv, env: { ...sv.env, LOG_LEVEL: "debug" } } : sv));await kl.environments.setServices({ environment: "acme-dev", services });env, err := kl.Environments.Get("acme-dev")if err != nil { log.Fatal(err)}var services []kloudlite.Serviceraw, _ := json.Marshal(env["services"])if err := json.Unmarshal(raw, &services); err != nil { log.Fatal(err)}for i := range services { if services[i].Name == "api" { if services[i].Env == nil { services[i].Env = map[string]string{} } services[i].Env["LOG_LEVEL"] = "debug" }}if err := kl.Environments.SetServices(kloudlite.SetServices{Environment: "acme-dev", Services: services}); err != nil { log.Fatal(err)}env = kl.environments.get(environment: "acme-dev")services = env["services"].map do |sv| sv["name"] == "api" ? sv.merge("env" => sv["env"].merge("LOG_LEVEL" => "debug")) : svendkl.environments.set_services(environment: "acme-dev", services: services)// Replaces the list: describe every service, with the new variable in api.var api = new Kloudlite.Service().name("api").image("ghcr.io/acme/api:1.0").port(8080) .env("LOG_LEVEL", "debug");kl.environments().setServices(new Kloudlite.SetServices().environment("acme-dev").services(api));# Replaces the list: send every service, with the new variable in api.kloudlite environments set-services acme-dev --services '[ {"name": "api", "image": "ghcr.io/acme/api:1.0", "ports": [8080], "env": {"LOG_LEVEL": "debug"}}]'Send a restart for the service. See Restart to apply new variables.
Close-out
- Get the environment again. Make sure that
apishows the newenvand thatservice_statusshows it as ready.
Set the variables of a sandbox
Reason for the task: Give the main process of a sandbox a set of variables.
Job set-up
- Access: you. A team sandbox needs team membership.
- References:
POST /v1/sandboxes,kloudlite sandboxes create.
This task creates a standalone sandbox. A sandbox in a session takes the same env. See Run a sandbox in a session.
Procedure
Create the sandbox with the variables in
env.sb = kl.sandboxes.create(cmd=["printenv", "MODE"], env={"MODE": "test"})const sb = await kl.sandboxes.create({ cmd: ["printenv", "MODE"], env: { MODE: "test" } });sb, err := kl.Sandboxes.Create(kloudlite.CreateSandbox{Cmd: []string{"printenv", "MODE"}, Env: map[string]string{"MODE": "test"}})if err != nil { log.Fatal(err)}sb = kl.sandboxes.create(cmd: ["printenv", "MODE"], env: { "MODE" => "test" })var sb = kl.sandboxes().create(new Kloudlite.CreateSandbox().cmd(List.of("printenv", "MODE")).env(Map.of("MODE", "test")));kloudlite sandboxes create --env MODE=test -- printenv MODE
Close-out
- Read the output of the sandbox as in Read the output and the exit code. Make sure that the output is
test.
Set the variables of a pipeline job
Reason for the task: Give the steps of a pipeline job a set of variables.
Job set-up
- Access: you. For a team, an admin.
- References: Pipeline definition.
Procedure
Add
envto the job. Each step of the job gets these variables.{ "jobs": { "test": { "env": { "LOG_LEVEL": "debug" } } } }Add
envto a step to add a variable, or to replace one of the job, for that step only.{ "steps": [{ "run": "./test.sh", "env": { "LOG_LEVEL": "trace" } }] }Send the definition as the next version of the pipeline.
Close-out
- Start a run and read the log. Make sure that the job starts with the variables.
Restart to apply new variables
Reason for the task: Give a running workspace or service the variables and secret values that changed since it started.
Job set-up
- Access: you. A team resource needs team membership.
- Conditions: the workspace or the environment runs. The resource shows
restart_required. - References:
POST /v1/workspaces/{id}/restart,POST /v1/environments/{id}/services/{service}/restart,kloudlite workspaces restart,kloudlite environments restart.
The processes stop and their connections end. Files in the home of a workspace and data in the mounted folders of a service are kept.
Procedure
Read which variables changed. The message names them and never shows a value.
from kloudlite import Kloudlitekl = Kloudlite()print(kl.workspaces.get(workspace="acme/api").get("restart_required"))import { Kloudlite } from "./index.ts";const kl = new Kloudlite();console.log(((await kl.workspaces.get({ workspace: "acme/api" })) as { restart_required?: unknown }).restart_required);kl := kloudlite.New(kloudlite.Options{})ws, err := kl.Workspaces.Get("acme/api")if err != nil { log.Fatal(err)}fmt.Println(ws["restart_required"])require_relative "kloudlite"kl = Kloudlite::Client.newputs kl.workspaces.get(workspace: "acme/api")["restart_required"]var kl = new Kloudlite();System.out.println(kl.workspaces().get("acme/api"));kloudlite workspaces get acme/apiSend a restart for the workspace, or for the one service of the environment, here
apiofacme-dev.kl.workspaces.restart(workspace="acme/api")kl.environments.restart(environment="acme-dev", service="api")await kl.workspaces.restart({ workspace: "acme/api" });await kl.environments.restart({ environment: "acme-dev", service: "api" });if err := kl.Workspaces.Restart("acme/api"); err != nil { log.Fatal(err)}if err := kl.Environments.Restart("acme-dev", "api"); err != nil { log.Fatal(err)}kl.workspaces.restart(workspace: "acme/api")kl.environments.restart(environment: "acme-dev", service: "api")kl.workspaces().restart("acme/api");kl.environments().restart("acme-dev", "api");kloudlite workspaces restart acme/apikloudlite environments restart acme-dev --service api
Close-out
- Get the resource again. Make sure that it does not show
restart_required.
Troubleshooting
| Symptom | Possible cause | Corrective action |
|---|---|---|
422 with env: at most 64 variables | A workspace or a sandbox has more than 64 variables. | Remove variables that you do not use. |
422 with env: "1X" is not a variable name | The name has a character other than letters, digits and _, or starts with a digit. | Use a name such as LOG_LEVEL. |
422 with env: HOME is set by the platform | The name is HOME or starts with KL_. | Use a different name. |
422 with env: LOG is longer than 32 KiB | A value of a workspace or a sandbox is larger than 32 KiB. | Use a smaller value, or put the data in a file. |
422 with a sandbox's env is set at create | The call sets the variables of a sandbox after its creation. | Create a new sandbox with the variables. |
400 with service "api": env name "log-level" must match [A-Za-z_][A-Za-z0-9_]* | A variable name of a service is not valid. | Use a name such as LOG_LEVEL. |
A pipeline shows `env.log-level`: not a variable name | A variable name of a job is not valid. | Use a name such as LOG_LEVEL. |
| A running workspace or service does not have the new variable | A process reads env only at start. It shows restart_required until then. | Restart the resource. |
409 with workspace is not running; its next start reads the current env | The resource is stopped. | Start it. The start gives it the current variables. |
| A cloned or restored workspace has no variables | A clone and a restore do not copy the variables. | Set the variables of the new workspace. |