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

时间:2018-11-30 11:02:29

标签: docker gitlab gitlab-ci

我想将https://gitlab.com/ric_harvey/nginx-php-fpm用作docker executor的基础Gitlab CI映像。但是此映像具有许多配置,例如。 WEBROOT。我需要将此WEBROOT设置为我自己的值。是否可以在Gitlab CI中运行它?

我已经尝试了(无法正常工作):

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

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

1 个答案:

答案 0 :(得分:1)

就您不能为构建器映像超载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

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