为什么我的环境变量在 github 操作上等于 None?
Why does my environment variable are equal to None on github actions?
我正在尝试为我的 Django 应用构建一个具有 GitHub 操作的 CI。我在设置 -> 机密 -> 操作中的 github 上定义了我的环境变量。
这是我的 ci.yaml 文件:
# name of our workflow
name: Django CI/CD Workflow
# triggers for our workflow
on: push
jobs:
health-check-job: # health check job for testing and code formatting check
runs-on: ubuntu-latest # os for running the job
services:
postgres: # we need a postgres docker image to be booted a side car service to run the tests that needs a db
image: postgres
env: # the environment variable must match with app/settings.py if block of DATBASES variable otherwise test will fail due to connectivity issue.
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: github-actions
ports:
- 5432:5432 # exposing 5432 port for application to use
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Checkout code # checking our the code at current commit that triggers the workflow
uses: actions/checkout@v2
- name: Cache dependency # caching dependency will make our build faster.
uses: actions/cache@v2 # for more info checkout pip section documentation at https://github.com/actions/cache
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Setup python environment # setting python environment to 3.x
uses: actions/setup-python@v2
with:
python-version: '3.x' # if you want multiple python version run just use matrix strategy in job config. See the documentation of GitHub Actions
- name: Check Python version # checking the python version to see if 3.x is installed.
run: python --version
- name: Install requirements # install application requirements
run: pip install -r pur_beurre/requirements.txt
- name: Run Migrations # run migrations to create table in side car db container
run: python pur_beurre/manage.py migrate
- name: Run Test # running tests
run: python pur_beurre/manage.py test
当它出现 ro 运行 迁移操作时,我有这个错误:
File "/home/runner/work/P10/P10/pur_beurre/pur_beurre/settings.py", line 29, in <module>
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ")
AttributeError: 'NoneType' object has no attribute 'split'
但我确实定义了DJANGO_ALLOWED_HOSTS。它等于本地主机。我也尝试将其定义为“localhost”,但它也不起作用。
你很接近 - 你所需要的只是将秘密传递给 env
变量到你的构建步骤,就像这样:
- name: Run Migrations # run migrations to create table in side car db container
run: python pur_beurre/manage.py migrate
env:
DJANGO_ALLOWED_HOSTS: ${{ secrets.DJANGO_ALLOWED_HOSTS }}
秘密默认情况下不是环境变量的一部分,因此如果您有更多秘密,则必须为每个秘密指定单独的一行。
您需要在脚本步骤级别set the environment variable(也可以在工作或工作流程级)。
对于这个 DJANGO_ALLOWED_HOSTS
env 变量,它可能看起来像这样:
- name: Run Migrations # run migrations to create table in side car db container
run: python pur_beurre/manage.py migrate
env:
DJANGO_ALLOWED_HOSTS: your_host_value # you can also use a secret variable
我正在尝试为我的 Django 应用构建一个具有 GitHub 操作的 CI。我在设置 -> 机密 -> 操作中的 github 上定义了我的环境变量。
这是我的 ci.yaml 文件:
# name of our workflow
name: Django CI/CD Workflow
# triggers for our workflow
on: push
jobs:
health-check-job: # health check job for testing and code formatting check
runs-on: ubuntu-latest # os for running the job
services:
postgres: # we need a postgres docker image to be booted a side car service to run the tests that needs a db
image: postgres
env: # the environment variable must match with app/settings.py if block of DATBASES variable otherwise test will fail due to connectivity issue.
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: github-actions
ports:
- 5432:5432 # exposing 5432 port for application to use
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Checkout code # checking our the code at current commit that triggers the workflow
uses: actions/checkout@v2
- name: Cache dependency # caching dependency will make our build faster.
uses: actions/cache@v2 # for more info checkout pip section documentation at https://github.com/actions/cache
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Setup python environment # setting python environment to 3.x
uses: actions/setup-python@v2
with:
python-version: '3.x' # if you want multiple python version run just use matrix strategy in job config. See the documentation of GitHub Actions
- name: Check Python version # checking the python version to see if 3.x is installed.
run: python --version
- name: Install requirements # install application requirements
run: pip install -r pur_beurre/requirements.txt
- name: Run Migrations # run migrations to create table in side car db container
run: python pur_beurre/manage.py migrate
- name: Run Test # running tests
run: python pur_beurre/manage.py test
当它出现 ro 运行 迁移操作时,我有这个错误:
File "/home/runner/work/P10/P10/pur_beurre/pur_beurre/settings.py", line 29, in <module>
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ")
AttributeError: 'NoneType' object has no attribute 'split'
但我确实定义了DJANGO_ALLOWED_HOSTS。它等于本地主机。我也尝试将其定义为“localhost”,但它也不起作用。
你很接近 - 你所需要的只是将秘密传递给 env
变量到你的构建步骤,就像这样:
- name: Run Migrations # run migrations to create table in side car db container
run: python pur_beurre/manage.py migrate
env:
DJANGO_ALLOWED_HOSTS: ${{ secrets.DJANGO_ALLOWED_HOSTS }}
秘密默认情况下不是环境变量的一部分,因此如果您有更多秘密,则必须为每个秘密指定单独的一行。
您需要在脚本步骤级别set the environment variable(也可以在工作或工作流程级)。
对于这个 DJANGO_ALLOWED_HOSTS
env 变量,它可能看起来像这样:
- name: Run Migrations # run migrations to create table in side car db container
run: python pur_beurre/manage.py migrate
env:
DJANGO_ALLOWED_HOSTS: your_host_value # you can also use a secret variable