在Jenkins声明性管道中的docker .withRun命令中使用空格转义参数 [英] Escaping parameters with white space in the docker .withRun command in Jenkins declarative pipeline

查看:331
本文介绍了在Jenkins声明性管道中的docker .withRun命令中使用空格转义参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前无法理解如何正确地将参数从Jenkins传递到包含空格的Jenkins Docker插件中的docker .withRun函数(特别是在声明性管道中).

Currently unable to get my head around how to correctly pass my parameters from Jenkins, to the docker .withRun function, in Jenkins Docker Plugin (specifically in declarative pipelines), which contain whitespace.

尝试了未知数量的方法来使该方法正常工作,但目前不知所措.参见下面的代码

Tried an unknown amount of methods to get this working and currently at a loss. See code below

        stage('Send Notifications')
        {
            steps
            {
            // Send a notification based on the parameters passed
                script
                {
                    docker.withRegistry(registry, registryCredentials)
                    {
                        // echo "${TITLE}"
                        docker.image("notifications:latest").withRun("--rm -e TITLE=\"${TITLE}\" -e MESSAGE=\"${MESSAGE}\" -e MESSAGE_FORMAT=\"${MESSAGE_TYPE}\" -e EMAIL=\"${EMAILS}\" --name notifications notifications")
                        // sh "docker run --rm -e TITLE=\"${TITLE}\" -e MESSAGE=\"${MESSAGE}\" -e MESSAGE_FORMAT=\"${MESSAGE_TYPE}\" -e EMAIL=\"${EMAILS}\" --name notifications notifications"

                    }
                }
            }
        }

当前,如果我仅使用shell命令方法,它将运行完美.但是,使用Docker插件方法我无法使其正常工作.

Currently if I just use the shell command method it works perfectly. However using the Docker Plugin method I just can't get it to work.

Login Succeeded
[Pipeline] {
Scripts not permitted to use method groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object (org.jenkinsci.plugins.docker.workflow.Docker$Image withRun org.codehaus.groovy.runtime.GStringImpl). Administrators can decide whether to approve or reject this signature.

任何建议都是有帮助的,只是尝试创建从其他管道接收字符串的通知.因此,我可以将消息发送到其他形式的通信(当前工作在松弛状态).

Any advice would be a help, just trying to create notifications that take in strings from other pipelines. So I can send messages to different forms of communication (currently working on slack).

在此添加了内容,并且刚刚产生了另一个错误.我要实现的目标是将参数传递到args中,这些参数将是文本行(从另一项工作向失败构建的用户发送的消息).

Added this in and has just produced another error. What I am trying to achieve is passing parameters into the args that will be lines of text (a message to the user of a failed build from another job).

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: static org.jenkinsci.plugins.docker.workflow.Docker.withRun() is applicable for argument types: (java.lang.String) values

docker.withRegistry(registry, registryCredentials)
                    {
                        def args = "--rm -e TITLE=\"This is a message\" -e MESSAGE=\"a message\" -e MESSAGE_FORMAT=\"s\" -e EMAIL=\"asdf@email.com\" --name notifications notifications"
                        echo args
                        docker.image("notifications:latest").withRun(args)
                        //sh "docker run --rm -e TITLE=\"${TITLE}\" -e MESSAGE=\"${MESSAGE}\" -e MESSAGE_FORMAT=\"${MESSAGE_TYPE}\" -e EMAIL=\"${EMAILS}\" --name notifications notifications"
                    }

推荐答案

不允许脚本使用groovy.lang.GroovyObject方法 invokeMethod java.lang.String java.lang.Object (org.jenkinsci.plugins.docker.workflow.Docker $ Image withRun org.codehaus.groovy.runtime.GStringImpl).管理员可以决定 是批准还是拒绝此签名.

Scripts not permitted to use method groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object (org.jenkinsci.plugins.docker.workflow.Docker$Image withRun org.codehaus.groovy.runtime.GStringImpl). Administrators can decide whether to approve or reject this signature.

您可以转到Jenkins的管理面板并批准 .

You could just go to Jenkins' administration panel and approve those.

关于您的其他问题. withRun

As for your other issue. The correct usage of withRun

node {
    checkout scm

    docker.withServer('tcp://swarm.example.com:2376', 'swarm-certs') {
        docker.image('mysql:5').withRun('-p 3306:3306') {
            /* do things */
        }
    }
}

请注意,withRun参数是容器开头的参数,它希望执行带有表达式的子句.对您而言,这意味着什么

Do notice that withRun arguments are parameters for the container to start with, it expects a clause with expressions to execute. Which for your case would mean

docker.image("notifications:latest").withRun() {
    sh "docker run --rm -e TITLE=\"${TITLE}\" -e MESSAGE=\"${MESSAGE}\" -e MESSAGE_FORMAT=\"${MESSAGE_TYPE}\" -e EMAIL=\"${EMAILS}\" --name notifications notifications"
}

或更简单

    docker.image('notifications:latest').inside {
        sh "docker run --rm -e TITLE=\"${TITLE}\" -e MESSAGE=\"${MESSAGE}\" -e MESSAGE_FORMAT=\"${MESSAGE_TYPE}\" -e EMAIL=\"${EMAILS}\" --name notifications notifications"
    }

要将这些参数仅用作容器参数,请执行以下操作:

to use those just as container parameters, please do:

docker.image("notifications:latest").withRun("-e TITLE=\"${TITLE}\" -e MESSAGE=\"${MESSAGE}\" -e MESSAGE_FORMAT=\"${MESSAGE_TYPE}\" -e EMAIL=\"${EMAILS}\" --name notifications notifications") {}

或者,您可以将其作为给定阶段的代理启动

Alternatively you could just start it as an agent for given stage

pipeline {
    agent none
    stages {
        agent {
            docker {
                image 'notifications:latest'
                registryUrl 'registry'
                registryCredentialsId 'registryCredentials'
                args "-e TITLE=\"This is a message\" -e MESSAGE=\"a message\" -e MESSAGE_FORMAT=\"s\" -e EMAIL=\"asdf@email.com\" --name notifications notifications"
            }
        }
        stage('Build') {
            steps {
                sh 'echo "I'm in container"'
            }
        }
    }
}

这篇关于在Jenkins声明性管道中的docker .withRun命令中使用空格转义参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆