我试图为Django项目运行CircleCI测试,其中manage.py
决定要从环境变量ENV_ROLE
应用的settings.py
版本(development.py
,staging.py
,或{ENV_ROLE
如果没有定义的话,ENV_ROLE
被设置为默认值development
,但是我正在更改它,以便Django在没有定义的情况下抛出一个ImproperlyConfigured
错误。在
为了使测试通过,我需要在CircleCI测试环境中定义ENV_ROLE
环境变量。在https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-step之后,我在run
步骤中添加了以下内容:
- run:
environment:
ENV_ROLE: development
但是,我仍然从CircleCI得到这个错误:
^{pr2}$以下是完整的.circleci/config.yml
:
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
Django项目修改了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
错误:
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'.")
我不明白为什么CircleCI没有“拾取”环境变量ENV_ROLE
?我的语法或对文档的理解有问题吗?在
我在使用circleCI+GAE时也遇到了类似的问题;我以为我在设置变量/配置yml因为GAE运行的构建过程没有将它们传送到应用程序中,所以从来没有真正进入应用程序。相反,我需要:
更新.circleci/配置yml在每个命令语句中使用这些命令,例如:
命令:gcloud app deploy app-登台.yaml
尝试在docker:部分指定变量。在
这将使您的容器与环境变量一起旋转。在容器可以访问我的变量之前,我必须测试在哪里插入我的几个构建的变量。在
如果您在需要ENV_角色时可以看到这些变量中的任何一个,那么您可能还需要添加there。在
^{pr2}$相关问题 更多 >
编程相关推荐