简体   繁体   中英

How to use ci.yml to declare env variables to be used in python?

-in gitlab-ci.yml

variables:
  SECRET_KEY: secret_key
  DB_NAME: somedb_name
  DB_USER: postgres
  DB_HOST: mdillon-postgis
  DB_PORT: 5432
  DB_PASSWORD: ''

-in django

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': os.getenv('DB_NAME'),
        'USER': os.getenv('DB_USER'),
        'PASSWORD': os.getenv('DB_PASSWORD'),
        'HOST': os.getenv('DB_HOST'),
        'PORT': os.getenv('DB_PORT'),
    }
}

Django doesn't seem to pick up the variables declared on CI when running pipeline. How do I properly declare the env variables on testing?

Anyone having the same confusion. Here are the steps:

  1. Go to Settings > CI/CD > Variables. Add the variables and values:
  DB_NAME: your_db_name
  DB_USER: postgres
  DB_HOST: mdillon-postgis (or whatever service you are using)
  DB_PORT: 5432
  DB_PASSWORD: <leave it blank>
  1. In gitlab-ci.yml
  POSTGRES_DB: $DB_NAME
  POSTGRES_USER: $DB_USER
  POSTGRES_PASSWORD: $DB_PASSWORD
  DATABASE_URL: postgres://postgres:@mdillon__postgis:5432/$DB_NAME
  1. In django
DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': os.getenv('DB_NAME'),
        'USER': os.getenv('DB_USER'),
        'PASSWORD': os.getenv('DB_PASSWORD'),
        'HOST': os.getenv('DB_HOST'),
        'PORT': os.getenv('DB_PORT'),
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM