首页 > 解决方案 > Gitlab CI 将环境变量传递给 docker build

问题描述

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

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

一切似乎都为时已晚,我需要将 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

标签: dockergitlabgitlab-ci

解决方案


至于你不能entrypoint为你的构建器图像超载: https ://docs.gitlab.com/runner/executors/docker.html#the-image-keyword

Docker 执行程序不会覆盖 Docker 映像的 ENTRYPOINT。

我建议您创建自己的图像,基于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

Dockerfiles/Dockerfile

FROM richarvey/nginx-php-fpm:1.1.1

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


推荐阅读