我有以下设置:
Jenkins环境运行docker-compose但我需要将环境变量传递给yml文件:
样本docker-compose.yml
version: '2'
services:
logon_server:
build: .
image: my_server:0.0.1
ports:
- "9200:9200"
command: ${DOCKER_CMD_EXEC}
networks:
- my_ntwrk
networks:
my_ntwrk:
当我从我的mac上的shell提示符运行以下脚本时,我可以传递不同的命令来执行,一切正常:
exec-tests.sh
#!/bin/bash
chmod 755 docker/exec-*.sh
# Setup env variables
export DOCKER_CMD_EXEC=/ci_e2e.sh
# Optional: record current versions
docker -v && docker-compose -v
echo `whoami`
echo `sudo -E -u admin printenv`
# Build, deploy and run E2E test cases
sudo -E -u admin docker-compose up --no-color --abort-on-container-exit
当我在我的jenkins工作中运行脚本时,在’Build Environment’下 – > ‘执行外壳’ – > ‘命令’,这是我正在谈论的图像:
我收到以下错误:
The DOCKER_CMD_EXEC variable is not set. Defaulting to a blank string
我尝试以root身份运行它,并作为默认用户’admin’运行jenkins.我试图让环境变量可以继承,尝试打印env,一切看起来都不错.
我可以通过取消设置DOCKER_CMD_EXEC来重新生成我的mac上的错误,但我无法弄清楚为什么会发生在jenkins中!
任何帮助,将不胜感激!
谢谢.
Ĵ
最佳答案 这是我的解决方案:
Docker脚本:
#!/bin/bash
export COMPOSE_HTTP_TIMEOUT=120
export DOCKER_CMD_EXEC=/app/docker/ci_run.sh
export DOCKER_LINKED_CMD=/app/docker/ci_run.sh
export DOCKER_NODE_ENV=staging
export DOCKER_IMAGE_TAG=${DOCKER_IMAGE_TAG:=staging}
export DOCKER_RELOAD_DB=${PARAM_RELOAD_DB:=true}
export DOCKER_HTTP_PROXY=''
# Optional: record current versions
docker -v && docker-compose -v
# Startup container(s)
cat compose.yml | envsubst | docker-compose -f - up
compose.yml文件:
version: '2'
services:
app:
build: .
image: ${DOCKER_IMAGE_TAG}
environment:
- MONGODB_HOST=mongodb
- NODE_ENV=${DOCKER_NODE_ENV}
- HTTP_PROXY=${DOCKER_HTTP_PROXY}
ports:
- "8000:8000"
command: ${DOCKER_CMD_EXEC}
volumes:
- /dev/shm:/dev/shm
networks:
- server-ntwk
networks:
server-ntwk:
bash脚本和compose文件位于同一目录中:
./docker/exec-build.sh