Syntax

Updated 2 years ago by Admin

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   kind: pipeline
2 type: docker
3 name: default
4
5 steps:
6 - name: build
7 commands:
8 - go build
9 - go test
10 environment:
11 GOOS: linux
12 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   kind: pipeline
2 type: docker
3 name: default
4
5 environment:
6 GOOS: linux
7 GOARCH: amd64
8
9 steps:
10 - name: build
11 commands:
12 - go build
13
14 - name: test
15 commands:
16 - 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  steps:
6 - name: build
7 commands:
8 - docker login -u $USERNAME -p $PASSWORD
9 - docker build -t hello-world .
10 - docker push hello-world
11 environment:
12 PASSWORD:
13 from_secret: password
14 USERNAME:
15 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  steps:
6 - name: build
7 commands:
8 - echo $GOOS
9 - echo $${GOARCH}
10 - go build
11 - 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  steps:
6 - name: build
7 environment:
8 GOPATH: ${HOME}/golang
9 commands:
10 - go build
11 - go test

Good:

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


How did we do?