For the complete StackGuardian documentation index, see llms.txt. An extended version with full page content is available at llms-full.txt.
Skip to main content

Runtime Environment Variables

StackGuardian sets a list of environment variables in every workflow step container. They tell your code which run it belongs to, where the workspace is mounted, which repository was checked out and what inputs the step received.

Use these variables instead of hardcoding IDs or paths. They hold the right values on both shared and private runners.

These are different from the environment variables you define yourself in Deployment Environment. Your own variables are passed to the same container, next to the ones on this page.

Default variables​

Every variable below is read-only from the platform's point of view. If your step changes one, the change only affects that step's process.

Run identity​

The ID variables hold resource IDs, not display names. An ID is generated from the resource's name when you create it and can differ from the name, for example when the name has spaces.

VariableDescriptionExample value
SG_ORG_IDThe organization the run belongs to./orgs/<org-id>
SG_WORKFLOW_GROUP_IDThe workflow group the workflow belongs to. For a nested group, the path lists each parent group: /wfgrps/<parent-group-id>/<workflow-group-id>./wfgrps/<workflow-group-id>
SG_WORKFLOW_IDThe workflow being run. In a stack, the path includes the stack: /wfgrps/<workflow-group-id>/stacks/<stack-id>/wfs/<workflow-id>./wfgrps/<workflow-group-id>/wfs/<workflow-id>
SG_WORKFLOW_RUN_IDThe current workflow run. In a stack, the path includes the stack, as with SG_WORKFLOW_ID./wfgrps/<workflow-group-id>/wfs/<workflow-id>/wfruns/<run-id>
SG_STACK_IDThe stack the workflow belongs to. Empty when the workflow isn't part of a stack./wfgrps/<workflow-group-id>/stacks/<stack-id>
SG_WORKFLOW_STEP_TEMPLATE_IDThe workflow step template the current step runs, with its revision./<org-id>/<template-id>:<revision>
SG_INTEGRATION_IDThe ID of the connector selected in Deployment Environment. Empty when no connector is selected./integrations/<connector-id>
SG_EXECUTOR_USERWho started the run, usually the user's email. A run started by another workflow may show a different value; use SG_TRIGGER_EVENT_INITIALIZER to get the user who started the chain.user@example.com

Workspace paths​

Each run gets a workspace that is mounted into every step container. Use these variables to find it.

VariableDescriptionExample value
SG_MOUNTED_WORKSPACE_ROOT_DIRRoot of the workspace. Everything except the artifacts directory is cleaned before and after each run./mnt/sg_workspace
SG_MOUNTED_ARTIFACTS_DIRDirectory whose contents persist between runs of the workflow, such as Terraform state. Write sg.outputs.json here to publish outputs./mnt/sg_workspace/artifacts
SG_MOUNTED_IAC_SOURCE_CODE_DIRRoot of the checked-out repository. Set only when the workflow has a VCS source. This is the repository root, not the working directory. Join it with SG_VCS_WORKING_DIRECTORY to get there./mnt/sg_workspace/user/<repo-name>

Source control​

Set when the workflow's source is a VCS repository. SG_VCS_AUTH_CREDS is always set and is empty when there's no repository.

VariableDescriptionExample value
SG_VCS_REPO_URLURL of the repository. For a private repository, the URL includes the access token, so treat it as a secret and never print it.https://github.com/<owner>/<repo-name>
SG_VCS_REPO_NAMEName of the repository. This is also the directory name under the workspace.<repo-name>
SG_VCS_REPO_REFThe branch, tag or commit that was checked out. Empty when none was set.<branch-tag-or-commit>
SG_VCS_WORKING_DIRECTORYWorking directory inside the repository, as set in the workflow's source settings. Empty when none was set.<path/in/repo>
SG_VCS_AUTH_CREDSCredentials used to clone the repository, such as a short-lived GitHub access token. Empty for public repositories. Treat this value as a secret and never print it.

Encoded inputs​

Structured inputs are passed as base64-encoded JSON. Decode them in your step, as shown in the shell example.

VariableDescription
SG_BASE64_WORKFLOW_STEP_INPUT_VARIABLESThe input values of the current workflow step, as set in the workflow's settings.
SG_BASE64_IAC_INPUT_VARIABLESThe IaC input variables from the workflow's Source and Parameters settings. Empty when the workflow has none.
SG_BASE64_POLICIESThe policies attached to the run.

Large values​

When an encoded input is too large for an environment variable, StackGuardian writes its JSON to a file in the workspace and sets the variable to file:// followed by that file's path.

echo "$SG_BASE64_POLICIES"
# file:///mnt/sg_workspace/user/.sg/<name>.json

The file holds plain JSON, not base64. Steps that read these variables should handle both forms, as the read_payload helper in the shell example does.

Trigger details​

Runs started by a trigger or by a stack get one SG_TRIGGER_<KEY> variable for each text field of the trigger. The field name is converted from camelCase to upper snake case, so eventSource becomes SG_TRIGGER_EVENT_SOURCE.

The fields depend on what started the run. SG_TRIGGER_TYPE tells you which one it was:

SG_TRIGGER_TYPEOther variables you can expect
github_webhookSG_TRIGGER_GH_EVENT_TYPE, SG_TRIGGER_EVENT_ACTION, SG_TRIGGER_REPO_HTTP_URL
gitlab_webhookSG_TRIGGER_GL_EVENT_TYPE, SG_TRIGGER_REPO_HTTP_URL
bitbucket_webhookSG_TRIGGER_BB_EVENT_TYPE, SG_TRIGGER_REPO_HTTP_URL, SG_TRIGGER_EVENT_INITIALIZER
workflow_chain, started by another workflowSG_TRIGGER_EVENT_SOURCE, the run that started it, such as /orgs/<org-id>/wfgrps/<workflow-group-id>/wfs/<workflow-id>/wfruns/<run-id>, and SG_TRIGGER_EVENT_INITIALIZER, the user who started that run
workflow_chain, started by a stackSG_TRIGGER_EVENT_SOURCE and SG_TRIGGER_STACK_ID, both /orgs/<org-id>/wfgrps/<workflow-group-id>/stacks/<stack-id>, and SG_TRIGGER_STACK_RUN_ID, such as /stackruns/<stack-run-id>

Runs started by hand have no trigger variables, and runs on a schedule may have none. Check that a variable is set before you use it:

if [[ "${SG_TRIGGER_TYPE:-}" == "github_webhook" ]]; then
echo "Started by a GitHub ${SG_TRIGGER_GH_EVENT_TYPE} event"
fi

Connector credentials​

When a cloud connector is selected in Deployment Environment, StackGuardian sets the variables that the provider's SDKs and Terraform providers read, so most tools authenticate with no extra setup.

ConnectorVariables
AWS OIDCAWS_WEB_IDENTITY_TOKEN_FILE
AWS RBACAWS_SHARED_CREDENTIALS_FILE and AWS_CONFIG_FILE, pointing to files with temporary credentials for the connector's IAM role. The credentials are in the default profile unless the connector sets a profile name.
Azure service principal with federated credentials, Azure managed identityARM_USE_OIDC=true, ARM_OIDC_TOKEN_FILE_PATH
GCP workload identity federation, GCP service accountGOOGLE_APPLICATION_CREDENTIALS, the path to a credentials file written from the connector

Naming your own variables​

  • Don't start your own variable names with SG_. That prefix is reserved for the variables on this page.
  • Don't reuse the names in Connector credentials when a connector is selected, because the connector sets them.
  • To build a value from a runtime variable, reference it in the value of your own variable, as in the Terraform example.

Deprecated variables​

Older workflow steps may use the names below. StackGuardian still sets them, but they will be removed. Use the replacement in new steps.

DeprecatedUse instead
ORG_ID, WORKFLOW_GROUP_ID, WORKFLOW_ID, WORKFLOW_RUN_ID, STACK_IDSG_ORG_ID, SG_WORKFLOW_GROUP_ID, SG_WORKFLOW_ID, SG_WORKFLOW_RUN_ID, SG_STACK_ID
MOUNTED_WORKSPACE_ROOT_DIR, LOCAL_WORKSPACE_ROOT_DIRSG_MOUNTED_WORKSPACE_ROOT_DIR
MOUNTED_WORKSPACE_USER_DIR$SG_MOUNTED_WORKSPACE_ROOT_DIR/user
MOUNTED_ARTIFACTS_DIR, LOCAL_ARTIFACTS_DIRSG_MOUNTED_ARTIFACTS_DIR
MOUNTED_IAC_SOURCE_CODE_DIR, LOCAL_IAC_SOURCE_CODE_DIR$SG_MOUNTED_IAC_SOURCE_CODE_DIR/$SG_VCS_WORKING_DIRECTORY. The old names include the working directory; SG_MOUNTED_IAC_SOURCE_CODE_DIR is the repository root.
VCS_AUTH_CREDSSG_VCS_AUTH_CREDS
BASE64_POLICIESSG_BASE64_POLICIES
BASE64_WORKFLOW_STEP_INPUT_VARIABLESSG_BASE64_WORKFLOW_STEP_INPUT_VARIABLES
BASE64_IAC_INPUT_VARIABLESSG_BASE64_IAC_INPUT_VARIABLES
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN set for an AWS RBAC connectorThe credentials files in AWS_SHARED_CREDENTIALS_FILE and AWS_CONFIG_FILE

Example​

Terraform and OpenTofu read input variables from TF_VAR_<name> environment variables. To pass a runtime variable into your configuration, add an environment variable in Settings → Deployment Environment and set its value to the runtime variable. You can use it on its own or inside a longer string.

KeyValue
TF_VAR_sg_workflow_id${SG_WORKFLOW_ID}
TF_VAR_created_by$SG_EXECUTOR_USER
TF_VAR_custom_prefix_from_orgcustom_${SG_ORG_ID}_prefix

Runtime environment variables mapped to Terraform variables in Deployment Environment

Then declare the matching variables in your configuration:

variable "sg_workflow_id" {
type = string
}

variable "created_by" {
type = string
}

locals {
tags = {
ManagedBy = "StackGuardian"
WorkflowId = var.sg_workflow_id
CreatedBy = var.created_by
}
}