我正在尝试为Django项目运行CircleCI测试,其中manage.py
确定要应用的settings.py
版本(development.py
,staging.py
或{{ 1}})来自环境变量production.py
。以前,ENV_ROLE
默认设置为ENV_ROLE
,如果没有定义,但我正在更改它,以便Django在未定义的情况下抛出development
错误。 / p>
为了使测试通过,我需要在CircleCI测试环境中定义ImproperlyConfigured
环境变量。在https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-step之后,我在ENV_ROLE
步骤中添加了以下内容:
run
但是,我仍然从CircleCI获得此错误:
- run:
environment:
ENV_ROLE: development
以下是完整的#!/bin/bash -eo pipefail
cd lucy-web
source venv/bin/activate
python manage.py compilescss --verbosity 0
python manage.py collectstatic --clear --no-input --verbosity 0
flake8
python manage.py test
Traceback (most recent call last):
File "/root/lucy/lucy_web/lucy-web/env.py", line 5, in <module>
ENV_ROLE = os.environ['ENV_ROLE']
File "/usr/local/lib/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'ENV_ROLE'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 4, in <module>
from env import ENV_ROLE
File "/root/lucy/lucy_web/lucy-web/env.py", line 8, in <module>
"No 'ENV_ROLE' environment variable is defined. "
django.core.exceptions.ImproperlyConfigured: No 'ENV_ROLE' environment variable is defined. Please define it as 'development', 'staging', or 'production'.
Exited with code 1
:
.circleci/config.yml
Django项目有一个修改过的version: 2
jobs:
build:
working_directory: ~/lucy/lucy_web/
docker:
- image: python:3.6.5
environment:
DATABASE_URL: ...
- image: jannkleen/docker-postgres-gis-hstore
environment:
POSTGRES_USER: ...
POSTGRES_DB: ...
- image: redis:4.0.9-alpine
steps:
- checkout
- restore_cache:
key: deps1-{{ .Branch }}-{{ checksum "lucy-web/requirements.txt" }}
- run:
name: Install Python deps in a venv
command: |
cd lucy-web
python3 -m venv venv
. venv/bin/activate
pip3 install -r requirements.txt
- save_cache:
key: deps1-{{ .Branch }}-{{ checksum "lucy-web/requirements.txt" }}
paths:
- "venv"
- run:
environment:
ENV_ROLE: development
command: |
cd lucy-web
source venv/bin/activate
python manage.py compilescss --verbosity 0
python manage.py collectstatic --clear --no-input --verbosity 0
flake8
python manage.py test
- store_artifacts:
path: test-reports/
destination: tr1
- store_test_results:
path: test-reports/
app_test:
working_directory: ~/lucy/lucy_app
docker:
- image: node:8
steps:
- checkout
- run:
command: |
cd lucy-app
yarn install
yarn lint
yarn jest
workflows:
version: 2
build_and_test:
jobs:
- build
- app_test
:
manage.py
其中#!/usr/bin/env python
import os
import sys
from dotenv import load_dotenv, find_dotenv
if __name__ == "__main__":
# Set environment variables from .env file
load_dotenv(find_dotenv())
# Determine which settings to apply (development, staging, or production)
from env import ENV_ROLE
os.environ.setdefault("DJANGO_SETTINGS_MODULE", f"lucy.settings.{ENV_ROLE}")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django # noqa: F401
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
检查env.py
环境变量是否已定义,否则会引发ENV_ROLE
错误:
ImproperlyConfigured
我不明白为什么CircleCI没有“拿起”import os
from django.core.exceptions import ImproperlyConfigured
try:
ENV_ROLE = os.environ['ENV_ROLE']
except KeyError:
raise ImproperlyConfigured(
"No 'ENV_ROLE' environment variable is defined. "
"Please define it as 'development', 'staging', or 'production'.")
环境变量?我的语法或对文档的理解是否有问题?
答案 0 :(得分:0)
我使用circleCI + GAE遇到了类似的问题;我以为我在.circleCI / config.yml中设置的变量实际上并没有真正应用到应用程序中,因为GAE运行的构建过程没有将它们转移进来。相反,我需要:
更新.circleci / config.yml以在每个命令语句中使用它们,例如:
命令:gcloud应用程序部署app-staging.yaml
答案 1 :(得分:0)
尝试在docker:部分中指定变量。
docker:
environment:
ENV_ROLE: development
这应该使用环境var启动您的容器。在容器可以访问它们之前,我必须测试一些版本的VAR。
如果您需要ENV_ROLE时可以看到这些变体中的任何一个,那么您可能也希望在其中添加您的变体。
environment:
DATABASE_URL: ...
environment:
POSTGRES_USER: ...
POSTGRES_DB: ...