Syntax

Drone provides the ability to define environment variables scoped to individual build steps. The examples in this section showcase Docker pipelines, however, the syntax is shared across all pipeline types.

Example step with custom environment variables:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
kind: pipeline
type: docker
name: default

steps:
- name: build
  commands:
  - go build
  - go test
  environment:
    GOOS: linux
    GOARCH: amd64

Drone automatically injects environment variables containing repository and commit metadata into each pipeline step. See the environment Reference for a full list of injected variables.

Per Pipeline

Drone supports global environment variables per pipeline. Globally defined variables are automatically injected into to every pipeline step.

Note this feature is only available to Docker pipelines at this time. It is not available to Kubernetes pipelines or other pipeline types.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
kind: pipeline
type: docker
name: default

environment:
  GOOS: linux
  GOARCH: amd64

steps:
- name: build
  commands:
  - go build

- name: test
  commands:
  - go test

From Secrets

Drone provides the ability to source environment variables from secrets. In the below example we provide the username and password as environment variables to the step.

 5
 6
 7
 8
 9
10
11
12
13
14
15
steps:
- name: build
  commands:
  - docker login -u $USERNAME -p $PASSWORD
  - docker build -t hello-world .
  - docker push hello-world
  environment:
    PASSWORD:
      from_secret: password
    USERNAME:
      from_secret: username

Common Problems

Parameter expansion is subject to pre-processing before the yaml is parsed. If you do not want the system to evaluate an expression it must be escaped.

 5
 6
 7
 8
 9
10
11
steps:
- name: build
  commands:
  - echo $GOOS
  - echo $${GOARCH}
  - go build
  - go test

Also note the environment section cannot expand environment variables or evaluate shell expressions. If you need to construct variables it should be done in the commands section.

Bad:

 5
 6
 7
 8
 9
10
11
steps:
- name: build
  environment:
    GOPATH: ${HOME}/golang
  commands:
  - go build
  - go test

Good:

 5
 6
 7
 8
 9
10
steps:
- name: build
  commands:
  - export GOPATH=$HOME/golang
  - go build
  - go test