Using the Environment

Learn about how to leverage the environment within your builds.

Vela provides the ability to define environment variables scoped to individual steps, services and secrets. Additionally, if you need global environment variables you can set it at the parent and have it injected to all containers.

Please note the environment is designed to be unique per container. Vela does inject a variety of default values from build, repo and user information.

Defaults:

Usage

The following pipeline concepts are being used in the pipeline below:

version: "1"
+ environment:
+   GLOBAL_EXAMPLE: Hello, World Globally!

services:
  - name: redis
+   environment:
+     LOCAL_EXAMPLE: Hello, World!
    image: redis:latest

stages:
  first_stage:
+   environment:
+     STAGE_EXAMPLE: "All the World's a Stage!"
    steps:
      - name: check status
        image: redis:latest
+       environment:
+         LOCAL_EXAMPLE: Hello, World!
        commands:
          # you can use bash commands in-line to set or override variables
          - export EXAMPLE="Hello World From Vela Team"
          - echo ${EXAMPLE}
          - echo ${STAGE_EXAMPLE}
          - echo ${GLOBAL_EXAMPLE}

secrets:
  - origin:
      name: private vault
      image: target/secret-vault:latest
+     environment:
+       EXAMPLE: Hello, World!
      secrets: [ vault_token ]
      parameters:
        addr: vault.example.com
        auth_method: token
        username: octocat
        items:
          - source: secret/docker
            path: docker

Global Usage

By default global injection affects all containers ran within the pipeline. However, if you only want some container types to receive the configuration you can limit which types get them by adding the environment declaration into the metadata.

version: "1"
  environment:
    GLOBAL_EXAMPLE: Hello, World Globally!

+ metadata:
+   environment: [ steps, services ]

services:
  # Global configuration is no longer available in services
  - name: redis
    environment:
      LOCAL_EXAMPLE: Hello, World!
    image: redis:latest

stages:
  first_stage:
    environment:
      STAGE_EXAMPLE: "All the World's a Stage!"
    steps:
      - name: check status
        image: redis:latest
        environment:
          LOCAL_EXAMPLE: Hello, World!
        commands:
          # you can use bash commands in-line to set or override variables
          - export EXAMPLE="Hello World From Vela Team"
          - echo ${EXAMPLE}
          - echo ${STAGE_EXAMPLE}
          - echo ${GLOBAL_EXAMPLE}

secrets:
+  # Global configuration is no longer available in secrets since "secrets"
+  # was removed as a value in the metadata.environment block.
  - origin:
      name: private vault
      image: target/secret-vault:latest
      environment:
        EXAMPLE: Hello, World!
      secrets: [ vault_token ]
      parameters:
        addr: vault.example.com
        auth_method: token
        username: octocat
        items:
          - source: secret/docker
            path: docker