在声明性管道中使用 withEnv

问题描述 投票:0回答:3

我正在尝试在声明性管道中运行 docker 命令,在我的从机上安装 docker env 我正在尝试使用 docker commons 插件“https://plugins.jenkins.io/docker-commons/”,但没有成功。

进一步的研究我在下面的链接中提到了如何使用这个插件。

https://automatingguy.com/2017/11/06/jenkins-pipelines-simple-delivery-flow/

我已经在管理 jenkins -> 全局工具配置中配置了 docker,但没有找到如何在我的 jenkins 声明性管道中使用以下部分,我认为下面的结构/语法适用于脚本化 jenkins 管道

def dockerTool = tool name: 'docker', type: 
'org.jenkinsci.plugins.docker.commons.tools.DockerTool'
withEnv(["DOCKER=${dockerTool}/bin"]) {
   stages{}
}

有人可以帮忙吗,我如何在 jenkins 的声明式管道中使用 docker 通用工具。 注意:由于与其他项目的标准化,我无法切换到脚本化管道

jenkins jenkins-pipeline jenkins-plugins jenkins-groovy
3个回答
0
投票

这是工作示例

pipeline{
    agent any
    stages{
        stage('test') {
            steps{
                script{
                    test_env="this is test env"
                    withEnv(["myEnv=${test_env}"]){
                        echo "${env.myEnv}"
                    }
                }
            }
        }
    }
}

0
投票

我有这种感觉,你不需要使用

withEnv
docker commons
。你见过这个吗? https://www.jenkins.io/doc/book/pipeline/docker/ 有很多关于如何将 docker 与 Jenkinsfile 结合使用的好例子。

我尝试回答你的问题(如果我答对了),如果你问的是脚本化

withEnv
的声明式等价物,那么你可能正在寻找
environment {}
?像这样的东西:

pipeline {
    agent any

    environment {
        DOCKER = "${dockerTool}/bin"
    }

    stages {
        stage('One') {
            steps {
              // steps here
            }
        }
    }
} 

0
投票

这是一个从 Docker Commons v1.17 开始工作的声明式管道解决方案

注意:工具名称,

dockerTool
是关键字,docker-19.03.11是我在 Jenkins > 管理 Jenkins > 全局工具配置页面中给出的安装名称。

pipeline {
    agent any
    tools {
        dockerTool 'docker-19.03.11'
    }
    stages {
        stage('build') {
            steps {
                sh'''
                    echo 'FROM mongo:3.2' > Dockerfile
                    echo 'CMD ["/bin/echo", "HELLO WORLD...."]' >> Dockerfile
                '''
                script {
                    docker.withRegistry('http://192.168.99.100:5000/v2/') {
                        def image = docker.build('test/helloworld2:$BUILD_NUMBER')
                    }
                }
            }
        }
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.