> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/open-source/use-harness-open-source/pipelines-1/variables.md).

# Variables

Variables can be set at [pipeline](#pipeline), [stage](#stage) and [step](#step) levels.

{% hint style="info" %}
The order of precedence for environment variables with the same name is step > stage > pipeline.
{% endhint %}

## Pipeline <a href="#pipeline" id="pipeline"></a>

[Pipeline](https://github.com/iKettles/harness-gitbook/tree/main/open-source/use-harness-open-source/reference/pipelines/yaml/pipeline.options.md) environment variables are available to all run steps in your pipeline.

```yaml
kind: pipeline
spec:
  options:
    envs:
      GOOS: linux
      GOARCH: amd64
  stages:
  - type: ci
    spec:
      steps:
      - name: test 1.20
        type: run
        spec:
          container: golang:1.20
          script: |
            go build
            go test

      - name: test 1.21
        type: run
        spec:
          container: golang:1.21
          script: |
            go build
            go test
```

## Stage <a href="#stage" id="stage"></a>

[Stage](https://github.com/iKettles/harness-gitbook/tree/main/open-source/use-harness-open-source/reference/pipelines/yaml/stage.type.ci.md) environment variables are available to all run steps in the stage.

```yaml
kind: pipeline
spec:
  stages:
  - type: ci
    spec:
      envs:
        GOOS: linux
        GOARCH: amd64
      steps:
      - name: test 1.20
        type: run
        spec:
          container: golang:1.20
          script: |
            go build
            go test

      - name: test 1.21
        type: run
        spec:
          container: golang:1.21
          script: |
            go build
            go test
```

## Step <a href="#step" id="step"></a>

[Run step](https://github.com/iKettles/harness-gitbook/tree/main/open-source/use-harness-open-source/reference/pipelines/yaml/step-run.md) environment variables are only available to the step.

```yaml
kind: pipeline
spec:
  stages:
  - type: ci
    spec:
      steps:
      - name: test
        type: run
        spec:
          container: golang
          envs:
            GOOS: linux
            GOARCH: amd64
          script: |
            go build
            go test
```

[Background steps](https://github.com/iKettles/harness-gitbook/tree/main/open-source/use-harness-open-source/reference/pipelines/yaml/step-background.md) also support environment variables.

```yaml
kind: pipeline
spec:
  stages:
  - type: ci
    spec:
      steps:
      - name: database
        type: background
        spec:
          container: mariadb
          envs:
            MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
            MYSQL_DATABASE: test

      - name: test
        type: run
        spec:
          container: mariadb
          script: |-
            sleep 15
            mariadb -u root -h database --execute="SELECT VERSION();"
```
