在Pipeline中将参数从文件传递到shell

时间:2017-12-03 20:21:12

标签: shell jenkins groovy jenkins-pipeline

基本上我正在尝试从文件中将属性传递给shell。

我有一个文件“docker_info”,其中包含工作区中的以下内容

IMAGE_NAME='Docker-image'

IMAGE_VERIONS='Docker-1.3.5'

这是我的管道脚本:

node
{

load "${WORKSPACE}/docker_info"

sh " echo ${IMAGE_NAME}" // here getting expected output: Docker-image

stage('Executing SHELL SCRIPING TO CHECK DOCKER IMAGE')

        sh '''

         echo "$DOCKER_IMAGE"

            if [ -z "${IMAGE_NAME}" ]     //(also tried "$IMAGE_NAME")
            then
            echo "Docker image not found."
            exit 0
            fi

             echo "${IMAGE_NAME}:started pushing image"

         '''   
}

输出:

[Test_BUILD_PIPELINE] Running shell script

+ echo Docker-image

Docker-image     

**Entering stage Executing SHELL SCRIPTING TO CHECK DOCKER IMAGE**

Proceeding

[Pipeline] sh

[Test_BUILD_PIPELINE] Running shell script

+  echo ''
+ '[' -z '' ']'

+ echo 'Docker image not found. Skip pushing Docker image'

Docker image not found. Skip pushing Docker image

+ exit 0

请注意,进入舞台后我将看不到预期值(Docker-image)而是显示:echo''

我试过几种方法但是没有用。

2 个答案:

答案 0 :(得分:1)

sh'''

。 /路径/到/与/ docker_info

如果[-z" $ IMAGE_NAME" ]

然后

echo"未找到Docker镜像"

退出0

网络连接

echo" $ IMAGE_NAME:开始推送图片"

'''

答案 1 :(得分:0)

尝试

    sh """

     echo '$DOCKER_IMAGE'

        if [ -z '$IMAGE_NAME' ]  
        then
        echo 'Docker image not found.'
        exit 0
        fi

         echo '$IMAGE_NAME:started pushing image' 

     """