将变量从jenkinsfile传递到docker-compose作为环境变量

时间:2019-08-07 13:26:42

标签: docker-compose jenkins-pipeline

我正在尝试从Jenkinsfile将环境变量设置为docker-compose。下一条命令-启动docker-compose

我的docker-compose文件现在看起来像这样:

services:
  myService:
    image: someImage:latest-release
    environment:
     - SOME_ENV=${FOLDER_PATH}

在某些情况下,我尝试过-我只留下了var的名称,没有值。为了以后设置值。

我尝试了很多选择。尝试导出并在开始时使用env var启动docker-compose,例如:SOME_ENV=VALUE docker-compose... 最后一个-使用.env文件。然后从Jenkinsfile启动脚本:

sh '''#!/bin/bash
set -exuo pipefail
FOLDER_PATH="$(realpath ./my/path/to/folder/)"
echo SOME_ENV=$FOLDER_PATH >> Docker/.env
"$(docker-compose -f Docker/ci.docker-compose.yml up -d --build)"

但是我想要这样的东西,至少要尽可能接近: 在jenkinsfile中:

def dcl = "docker-compose -f Docker/ci.docker-compose.yml"
def VIDEO_PATH = sh(returnStdout: true, script: "realpath ./automation/reports/selenoid/video/")
sh "SOME_ENV=$VIDEO_PATH $dcl up -d --build"

But as I see - because of few shells I cannot do it...

I need find some way how is properly set var from Jenkinsfile as env var to docker-compose

1 个答案:

答案 0 :(得分:0)

因此,我花了将近一天的时间来解决它。 Args,处于撰写环境,结合了带有arg的单独容器,其中包含了env ...只有一件事对我有所帮助-在运行时在compose(而非.env)中创建env_file并在其中设置我的env变量。可能是.env文件也可以在Windows情况下提供帮助,不确定,在我的情况下(linux)不是这样