docker - Gitlab CI 将环境变量传递给 docker build

标签 docker gitlab gitlab-ci

我想使用https://gitlab.com/ric_harvey/nginx-php-fpm作为带有 docker 执行器的基础 Gitlab CI 镜像。但是这个图像有很多配置,例如。网络根目录。我需要将此 WEBROOT 设置为我自己的值。在 Gitlab CI 中运行可以吗?

我已经尝试过(行不通):

  • 变量:gitlab-ci.yml 中的定义
  • 在 gitlab-ci.yml 的 before_script 中设置变量 WEBROOT
  • 在 Gitlab UI -> 项目 -> CI/CD -> 环境变量中添加环境变量 WEBROOT

一切似乎都太晚了,我需要的是将 docker 的启动命令编辑为:

docker run -e "WEBROOT=xxx" ...

.

image: richarvey/nginx-php-fpm:1.1.1

variables:
  WEBROOT: "/build/domotron/cloud/www" <- this wont work

before_script:
   ## Install ssh-agent if not already installed, it is required by Docker.
   - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'

   ## Run ssh-agent (inside the build environment)
   - eval $(ssh-agent -s)

   ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
   ## We're using tr to fix line endings which makes ed25519 keys work
   ## without extra base64 encoding.
   ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
   - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null

   ## Create the SSH directory and give it the right permissions
   - mkdir -p ~/.ssh
   - chmod 700 ~/.ssh

   ## Setup git
   - git config --global user.email "email"
   - git config --global user.name "User"

   ## Use ssh-keyscan to scan the keys of your private server.
   - ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
   - chmod 644 ~/.ssh/known_hosts
stages:
  - test

Codeception:
  stage: test
  services:
    - name: selenium/standalone-chrome
      alias: chrome
  script:
    - curl -sS https://getcomposer.org/installer | php
    - php composer.phar install --no-interaction
    - php vendor/bin/codecept run

最佳答案

就您无法重载构建器镜像的入口点而言: https://docs.gitlab.com/runner/executors/docker.html#the-image-keyword

The Docker executor doesn’t overwrite the ENTRYPOINT of a Docker image.

我建议您基于richarvey/nginx-php-fpm:1.1.1创建自己的镜像,并使用它来构建。

您可以在管道中添加一个步骤,在其中准备所需的工具,例如您自己的构建器:

gitlab-ci.yaml

stages:
  - prepare
  - build
  - ...
prepare-build-dockers:
  stage: prepare
  image: docker:stable
  script:
    - export WEBROOT
    - build -t my-builder Dockerfiles

Dockerfile/Dockerfile

FROM richarvey/nginx-php-fpm:1.1.1

顺便说一句,gitlab 现在支持自定义 docker 注册表,因此拥有自己的镜像用于构建/测试/部署是一个很好的做法。

关于docker - Gitlab CI 将环境变量传递给 docker build,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53556252/

相关文章:

docker - 在 Windows 上的 docker 命令中找不到输入文件

ruby - 使用 SAML 连接 GitLab 帐户

git - 通过 web 界面在 Gitlab repo 中创建一个目录

docker - 配置 docker-compose 覆盖以忽略/隐藏一些容器

linux - Docker 安装在用户家

git 忽略 .env 文件不起作用

gitlab - 将 gitlab 多项目管道中的一些继承变量(但不是全部)列入白名单

reactjs - GitLab CI Pipeline Job 给出错误 JavaScript heap out of memory

ruby-on-rails - 来自 gitlab-ci 的状态不再显示在 gitlab 合并请求中

Docker:来自守护进程的错误响应:删除 myvol:卷正在使用中