Skip to content

pass environment variables to deployed application

This exports all variables of a deploy job to the Kubernetes Deployment and thus all its Pods.

Here is an example .gitlab-ci.yml. Please note the variables key of the staging and production jobs.

stages:
  - build
  - test
  - staging
  - production

variables:
  IMG: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_SLUG}
  # Application deployment domain
  KUBE_DOMAIN: example.org
  CI_PORT: 8000

before_script:
  - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
  - export HELLO_UID=$(id -u)
  - export HELLO_GID=$(id -g)

build:
  stage: build
  tags:
    - kvm
  script:
    - docker build -t ${IMG} .
    - docker push ${IMG}

unittests:
  stage: test
  tags:
    - kvm
  script:
    - docker-compose run --rm app coverage run --source='.' manage.py test
    - docker-compose run --rm app coverage report

test-fixtures:
  stage: test
  tags:
    - kvm
  script:
    - docker-compose run --rm app ./manage.py migrate
    - docker-compose run --rm app ./manage.py loaddata hello/fixtures/admin.json

staging:
  # don't run before_script for this one
  before_script:
    - echo skip before_script
  variables:
    HELLO_GREETING: Bonjour
  image: registry.gitlab.com/felixhummel/kubernetes-deploy:2-pass-env-vars
  stage: staging
  script:
    - command deploy
  environment:
    name: staging
    url: http://$CI_PROJECT_PATH_SLUG-staging.$KUBE_DOMAIN
  tags:
    - docker-privileged
  only:
    - master

production:
  # don't run before_script for this one
  before_script:
    - echo skip before_script
  variables:
    HELLO_GREETING: Prodjour
  image: registry.gitlab.com/felixhummel/kubernetes-deploy:2-pass-env-vars
  stage: production
  script:
    - command deploy
  environment:
    name: production
    url: http://$CI_PROJECT_PATH_SLUG.$KUBE_DOMAIN
  tags:
    - docker-privileged
  when: manual
  only:
    - master

Merge request reports