# Build config variables

# Overview

Build config variables are defined by the build container config in your .sun-ci.yml file.

Environment variables are defined using the environment keyword.

jobs:
  build:
    image: node
    environment:
      NODE_ENV: production
    script:
    - echo $NODE_ENV  # production

# Service container environment variables

You can also define environment variables for service containers. A lot of docker images use environment variables to customize the application running inside the container.

Environment variables in service containers are defined the same way as the build container.

For example, to customize the MySQL service for a Laravel application:

jobs:
  test:
    services:
    - image: mysql:8
      name: mysql-test
      environment:
        MYSQL_DATABASE: my_app
        MYSQL_USER: my_app
        MYSQL_PASSWORD: secret
    image: php
      environment:
        DB_HOST: mysql
        DB_PORT: 3306
        DB_DATABASE: my_app
        DB_USERNAME: my_app
        DB_PASSWORD: secret

# Syntax

There are two syntaxes available for defining your environment variables.

# Array of variable defines

jobs:
  build:
    image: node
    environment:
    - NODE_ENV=production

# Use YAML mapping

jobs:
  build:
    image: node
    environment:
      NODE_ENV: production

This syntax is particularly useful when you want to use YAML anchor to merge environment variables.

# Global variables

You can define variables available to all jobs using the default config. In the following example, $NODE_ENV is available in all jobs in the build.

default:
  environment:
    NODE_ENV: production

jobs:
  install:
    stage: install
    image: node
    script:
    - echo $NODE_ENV  # production

Global variables can be overriden by job-defined environment variables.

default:
  environment:
    NODE_ENV: production

jobs:
  install:
    stage: install
    image: node
    environment:
      NODE_ENV: development
    script:
    - echo $NODE_ENV  # development
    - npm install

  build:
    stage: build
    image: node
    script:
    - echo $NODE_ENV  # production
    - npm run build

In the above example, $NODE_ENV is set to development in the job install. Job build still has NODE_ENV=production.