docker - Gitlab CI 将环境变量传递给 docker build
问题描述
我想使用https://gitlab.com/ric_harvey/nginx-php-fpm作为带有 docker executor 的基本 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
解决方案
至于你不能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 注册表,因此拥有自己的构建/测试/部署映像是一个好习惯。
推荐阅读
- bash - 在执行之前打印格式化的 bash 注释值
- c# - .NET Core 程序集的子依赖项无法在运行时加载
- python - 如果删除对象,停止 python 运行某些代码部分
- javascript - AJAX 调用后打开一个新的选项卡/窗口
- python - 如何通过多个一维数组附加一个空数组
- aws-lambda - Amazon API 网关忽略 set-cookie
- javascript - react-add-to-calendar 全天事件
- ios - 将 pod 添加到单个自定义框架
- android - 安装apk后无法在真实的android设备中访问我的存储
- python - 如何在 Python 中将 PDF 转换为灰度