使用 Auto DevOps(与 GCP Kubernetes)时,如何在 GitLab 中为 CI/CD 设置环境变量?

使用 Auto DevOps(与 GCP Kubernetes)时,如何在 GitLab 中为 CI/CD 设置环境变量?

这里有很多移动部件,所以我还不确定哪里出了问题。我这样设置变量:

在此处输入图片描述

但是在构建阶段,我遇到了这个失败(这是一个 Django 应用程序):

   raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
   django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

   !     Error while running '$ python manage.py collectstatic --noinput'.
   See traceback above for details.

   You may need to update application code to resolve this error.
   Or, you can disable collectstatic for this application:

   $ heroku config:set DISABLE_COLLECTSTATIC=1

   https://devcenter.heroku.com/articles/django-assets

如果我在 UI 的其他地方遗漏了什么线索,那么这就是输出:

在此处输入图片描述

我创建了一个公共骨架应用程序来展示和调试这个问题:https://gitlab.com/pupeno/example/-/jobs/113402553

我缺少什么来将环境变量暴露给构建过程?

我抄袭了 Auto DevOps他们的.gitlab-ci.yml模板并补充道:

variables:
  # ...
  SECRET_KEY: $SECRET_KEY

以及在$SECRET_KEY不起作用的情况下进行硬编码:

variables:
  # ...
  SECRET_KEY: "12345678901234567890123456789012345678901234567890"

另外,我将其作为变量添加到构建阶段:

build:
  stage: build
  image: docker:stable-git
  services:
  - docker:stable-dind
  script:
    - setup_docker
    - build
  only:
    - branches
  variables:
    SECRET_KEY: "12345678901234567890123456789012345678901234567890"

或导出:

build:
  stage: build
  image: docker:stable-git
  services:
  - docker:stable-dind
  script:
    - export SECRET_KEY="12345678901234567890123456789012345678901234567890"
    - setup_docker
    - build
  only:
    - branches

并且该变量仍然不可用python manage.py

我如何才能使其可用?

构建的完整日志是:

[0KRunning with gitlab-runner 11.4.0-rc1 (1ff344e1)
[0;m[0K  on docker-auto-scale fa6cab46
[0;m[0KUsing Docker executor with image docker:stable-git ...
[0;m[0KStarting service docker:stable-dind ...
[0;m[0KPulling docker image docker:stable-dind ...
[0;m[0KUsing docker image sha256:acfec978837639b4230111b35a775a67ccbc2b08b442c1ae2cca4e95c3e6d08a for docker:stable-dind ...
[0;m[0KWaiting for services to be up and running...
[0;m[0KPulling docker image docker:stable-git ...
[0;m[0KUsing docker image sha256:a8a2d0da40bc37344c35ab723d4081a5ef6122d466bf0a0409f742ffc09c43b9 for docker:stable-git ...
[0;msection_start:1540485937:prepare_script
[0KRunning on runner-fa6cab46-project-8991099-concurrent-0 via runner-fa6cab46-srm-1540485878-b0f2f392...
section_end:1540485939:prepare_script
[0Ksection_start:1540485939:get_sources
[0K[32;1mCloning repository...[0;m
Cloning into '/builds/eligibleai/phoenix'...
[32;1mChecking out 4753fd6d as master...[0;m
[32;1mSkipping Git submodules setup[0;m
section_end:1540485940:get_sources
[0Ksection_start:1540485940:restore_cache
[0Ksection_end:1540485942:restore_cache
[0Ksection_start:1540485942:download_artifacts
[0Ksection_end:1540485943:download_artifacts
[0Ksection_start:1540485943:build_script
[0K[32;1m$ # Auto DevOps variables and functions # collapsed multi-line command[0;m
[32;1m$ setup_docker[0;m
[32;1m$ build[0;m
Logging to GitLab Container Registry with CI credentials...
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

Building Heroku-based application using gliderlabs/herokuish docker image...
Unable to find image 'gliderlabs/herokuish:latest' locally
latest: Pulling from gliderlabs/herokuish
8ee29e426c26: Pulling fs layer
6e83b260b73b: Pulling fs layer
e26b65fd1143: Pulling fs layer
40dca07f8222: Pulling fs layer
b420ae9e10b3: Pulling fs layer
da73fdbc63c8: Pulling fs layer
06627042f798: Pulling fs layer
91591ee9e53a: Pulling fs layer
16bb228b9f74: Pulling fs layer
5ac34e15ac7b: Pulling fs layer
7ff3cba3e710: Pulling fs layer
8be7609fa05c: Pulling fs layer
28b563a2d870: Pulling fs layer
7d2ba817ef0b: Pulling fs layer
40dca07f8222: Waiting
b420ae9e10b3: Waiting
da73fdbc63c8: Waiting
06627042f798: Waiting
91591ee9e53a: Waiting
16bb228b9f74: Waiting
5ac34e15ac7b: Waiting
7ff3cba3e710: Waiting
8be7609fa05c: Waiting
28b563a2d870: Waiting
7d2ba817ef0b: Waiting
6e83b260b73b: Verifying Checksum
6e83b260b73b: Download complete
e26b65fd1143: Verifying Checksum
e26b65fd1143: Download complete
40dca07f8222: Verifying Checksum
40dca07f8222: Download complete
8ee29e426c26: Verifying Checksum
8ee29e426c26: Download complete
b420ae9e10b3: Verifying Checksum
b420ae9e10b3: Download complete
da73fdbc63c8: Verifying Checksum
da73fdbc63c8: Download complete
91591ee9e53a: Verifying Checksum
91591ee9e53a: Download complete
5ac34e15ac7b: Verifying Checksum
5ac34e15ac7b: Download complete
7ff3cba3e710: Verifying Checksum
7ff3cba3e710: Download complete
16bb228b9f74: Verifying Checksum
16bb228b9f74: Download complete
28b563a2d870: Verifying Checksum
28b563a2d870: Download complete
8be7609fa05c: Verifying Checksum
8be7609fa05c: Download complete
7d2ba817ef0b: Verifying Checksum
7d2ba817ef0b: Download complete
06627042f798: Verifying Checksum
06627042f798: Download complete
8ee29e426c26: Pull complete
6e83b260b73b: Pull complete
e26b65fd1143: Pull complete
40dca07f8222: Pull complete
b420ae9e10b3: Pull complete
da73fdbc63c8: Pull complete
06627042f798: Pull complete
91591ee9e53a: Pull complete
16bb228b9f74: Pull complete
5ac34e15ac7b: Pull complete
7ff3cba3e710: Pull complete
8be7609fa05c: Pull complete
28b563a2d870: Pull complete
7d2ba817ef0b: Pull complete
Digest: sha256:d1581d20408be7a7366b6ea74dc5cddc866f4ad4c573a04251272471c0aa2e56
Status: Downloaded newer image for gliderlabs/herokuish:latest
[1G       [1G-----> Python app detected
[1G-----> Installing python-3.6.6
[1G-----> Installing pip
[1G-----> Installing SQLite3
[1G-----> Installing requirements with pip
[1G       Collecting Django==2.1.2 (from -r /tmp/build/requirements.txt (line 1))
[1G       Downloading https://files.pythonhosted.org/packages/32/ab/22530cc1b2114e6067eece94a333d6c749fa1c56a009f0721e51c181ea53/Django-2.1.2-py3-none-any.whl (7.3MB)
[1G       Collecting django-dotenv==1.4.2 (from -r /tmp/build/requirements.txt (line 2))
[1G       Downloading https://files.pythonhosted.org/packages/e4/af/d9175f2b40a9bcd700db2861610d6ed48c2795ffba46c1d6abf25f7c1dea/django_dotenv-1.4.2-py2.py3-none-any.whl
[1G       Collecting psycopg2==2.7.5 (from -r /tmp/build/requirements.txt (line 3))
[1G       Downloading https://files.pythonhosted.org/packages/5e/d0/9e2b3ed43001ebed45caf56d5bb9d44ed3ebd68e12b87845bfa7bcd46250/psycopg2-2.7.5-cp36-cp36m-manylinux1_x86_64.whl (2.7MB)
[1G       Collecting pytz (from Django==2.1.2->-r /tmp/build/requirements.txt (line 1))
[1G       Downloading https://files.pythonhosted.org/packages/52/8b/876c5745f617630be90cfb8fafe363c6d7204b176dc707d1805d1e9a0a35/pytz-2018.6-py2.py3-none-any.whl (507kB)
[1G       Installing collected packages: pytz, Django, django-dotenv, psycopg2
[1G       Successfully installed Django-2.1.2 django-dotenv-1.4.2 psycopg2-2.7.5 pytz-2018.6
[1G       
[1G-----> $ python manage.py collectstatic --noinput
[1G       Environment variables:
[1G       CPLUS_INCLUDE_PATH: /app/.heroku/vendor/include:/app/.heroku/python/include:
[1G       HOSTNAME: 09751c55b3ac
[1G       EXPORT_PATH: /tmp/buildpacks/04_buildpack-python/bin/../export
[1G       CURL_CONNECT_TIMEOUT: 30
[1G       PROFILE_PATH: /tmp/build/.profile.d/python.sh
[1G       HEROKUISH_VERSION: 0.4.4
[1G       BUILDPACK_LOG_FILE: /dev/null
[1G       LIBRARY_PATH: /app/.heroku/vendor/lib:/app/.heroku/python/lib:
[1G       CACHE_DIR: /tmp/cache
[1G       LATEST_37: python-3.7.0
[1G       PYTHONUNBUFFERED: 1
[1G       LATEST_36: python-3.6.6
[1G       DEFAULT_PYTHON_STACK: cedar-14
[1G       LD_LIBRARY_PATH: /app/.heroku/vendor/lib:/app/.heroku/python/lib:
[1G       BIN_DIR: /tmp/buildpacks/04_buildpack-python/bin
[1G       PATH: /app/.heroku/python/bin:/app/.heroku/vendor/bin::/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/tmp/buildpacks/04_buildpack-python/vendor/:/tmp/buildpacks/04_buildpack-python/vendor/pip-pop
[1G       ENV_DIR: /tmp/env
[1G       SELF: /bin/herokuish
[1G       RECOMMENDED_PYTHON_VERSION: python-3.6.6
[1G       C_INCLUDE_PATH: /app/.heroku/vendor/include:/app/.heroku/python/include:
[1G       APP_DIR: /app
[1G       PWD: /tmp/build
[1G       BPLOG_PREFIX: buildpack.python
[1G       LANG: en_US.UTF-8
[1G       SELF_EXECUTABLE: /bin/herokuish
[1G       CACHED_PYTHON_STACK: heroku-16
[1G       STACK: heroku-16
[1G       REQUEST_ID: build-16345
[1G       HOME: /app
[1G       SHLVL: 3
[1G       LATEST_2: python-2.7.15
[1G       SQLITE3_VERSION: 3.11.0-1ubuntu1
[1G       PIP_UPDATE: 9.0.2
[1G       PYTHONPATH: .
[1G       BUILD_DIR: /tmp/build
[1G       VENDOR_URL: https://lang-python.s3.amazonaws.com/heroku-16
[1G       WARNINGS_LOG: /tmp/tmp.Iy1gH0Xs0X
[1G       DEFAULT_PYTHON_VERSION: python-3.6.6
[1G       CURL_TIMEOUT: 180
[1G       DEBIAN_FRONTEND: noninteractive
[1G       PKG_CONFIG_PATH: /app/.heroku/vendor/lib/pkgconfig:/app/.heroku/vendor/lib/pkg-config:/app/.heroku/python/lib/pkg-config:
[1G       _: /app/.heroku/python/bin/python
[1G       Traceback (most recent call last):
[1G       File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 204, in fetch_command
[1G       app_name = commands[subcommand]
[1G       KeyError: 'collectstatic'
[1G       During handling of the above exception, another exception occurred:
[1G       Traceback (most recent call last):
[1G       File "manage.py", line 23, in <module>
[1G       execute_from_command_line(sys.argv)
[1G       File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
[1G       utility.execute()
[1G       File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
[1G       self.fetch_command(subcommand).run_from_argv(self.argv)
[1G       File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 211, in fetch_command
[1G       settings.INSTALLED_APPS
[1G       File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 57, in __getattr__
[1G       self._setup(name)
[1G       File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 44, in _setup
[1G       self._wrapped = Settings(settings_module)
[1G       File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 126, in __init__
[1G       raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
[1G       django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
[1G       
[1G       !     Error while running '$ python manage.py collectstatic --noinput'.
[1G       See traceback above for details.
[1G       
[1G       You may need to update application code to resolve this error.
[1G       Or, you can disable collectstatic for this application:
[1G       
[1G       $ heroku config:set DISABLE_COLLECTSTATIC=1
[1G       
[1G       https://devcenter.heroku.com/articles/django-assets
section_end:1540486016:build_script
[0Ksection_start:1540486016:after_script
[0Ksection_end:1540486017:after_script
[0Ksection_start:1540486017:upload_artifacts_on_failure
[0Ksection_end:1540486018:upload_artifacts_on_failure
[0K[31;1mERROR: Job failed: exit code 1
[0;m

答案1

我在 Gitlab Auto DevOps 上遇到了非常类似的问题。

通过在文件中添加以下内容,我能够解决这个问题settings.py

STATIC_URL = '/static/'
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

信用:

答案2

在我的例子中,我对密钥进行了硬编码。我更改了代码,以便可以从文件中读取它。我忘记像其他变量一样.env添加该变量。即使我在 Gitlab CI/CD 设置中添加了该变量,但它在 Web 容器中不可用。请看这里:docker-compose.ymlSECRET_KEYhttps://stackoverflow.com/questions/57835476/how-to-set-environmental-variables-properly-gitlab-ci-cd-and-docker

答案3

我遇到了同样的问题。我显然没有足够的声誉来发表评论,所以我只会回答...

我认为这个问题源于 dind 没有传递其运行的环境。docker 文件随后尝试构建所有内容,但 django 缺少所需的变量。我还没有找到一种很好的方法来实现这一点,而无需对设置中的值进行硬编码。(Aaron Blythe 的回答完全不相关)

答案4

这是一个较老的问题,我知道该功能在提出时尚不可用,但由于这个问题仍未得到解答,其他人在寻找问题解决方案时仍可能会遇到该帖子:

GitLab 12.3 版本引入了通过单独的变量将 CI/CD 变量转发到构建环境的功能AUTO_DEVOPS_BUILD_IMAGE_FORWARDED_CI_VARIABLES

因此,如果您希望 Heroku 在构建过程中不运行 collectstatic(本文中提到的第一个错误),您可以在存储库的 CI/CD 变量部分中设置DISABLE_COLLECTSTATIC为,并将值添加为附加变量。请参阅1AUTO_DEVOPS_BUILD_IMAGE_FORWARDED_CI_VARIABLESDISABLE_COLLECTSTATIC最新 GitLab 文档的这一部分更多细节。

另一方面,如果你想让某些变量可供Django 项目一旦它在容器中运行,你就可以在它们前面加上前缀,K8S_SECRET_以便将它们传递给容器。参见此表包含 CI/CD 变量

相关内容