Jenkins声明性管道中docker.withRun命令中带空格的转义参数

Jenkins声明性管道中docker.withRun命令中带空格的转义参数,jenkins,groovy,jenkins-pipeline,Jenkins,Groovy,Jenkins Pipeline,目前,我无法了解如何将参数从Jenkins正确传递给docker.withRun函数,该函数位于Jenkins docker插件中(特别是在声明性管道中),其中包含空格 尝试了未知数量的方法使其正常工作,但目前处于亏损状态。请参阅下面的代码 stage('Send Notifications') { steps { // Send a notification based on the par

目前,我无法了解如何将参数从Jenkins正确传递给docker.withRun函数,该函数位于Jenkins docker插件中(特别是在声明性管道中),其中包含空格

尝试了未知数量的方法使其正常工作,但目前处于亏损状态。请参阅下面的代码

        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插件方法,我就是无法让它工作

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.
任何建议都会有所帮助,只要尝试创建接收其他管道字符串的通知即可。因此,我可以向不同形式的通信发送消息(目前正在slack上工作)

编辑:将此添加到中,刚刚产生另一个错误。我试图实现的是将参数传递到参数中,这些参数将是文本行(从另一个作业发送给失败构建的用户的消息)

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

你可以去詹金斯家。

编辑:

至于你的另一个问题。在运行时正确使用

node {
    checkout scm

    docker.withServer('tcp://swarm.example.com:2376', 'swarm-certs') {
        docker.image('mysql:5').withRun('-p 3306:3306') {
            /* do things */
        }
    }
}
请注意,
withRun
参数是容器的参数,它需要执行一个包含表达式的子句。这对你来说意味着什么

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"
    }
编辑2:

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

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

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

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"'
            }
        }
    }
}

批准了脚本,并且刚刚产生了一个错误,如果您在我的编辑部分看到,我只是传递了一条包含空格的消息,因此如果没有引号,该消息将被视为附加参数。如果你有任何更好的解决方案,请让我知道。跟进你可能是我对withRun的误解,因为docker容器的寿命很短。它的唯一目标是接收这4个参数并发送一条消息并关闭。容器中实际上没有运行任何命令。那么在这种情况下,运行shell脚本是否就是建议的解决方案?在我看来,最后两个似乎是在容器中运行shell脚本。我想知道withRun所需的最小值是多少,可能是您需要执行一个子句
。withRun(args){c->sh“echo test”}
,就像那里的任何东西一样。这不是你能找到的最好的情况。找到一种声明性管道方法来做到这一点,一定要让我知道这是否适合你
docker.image("notifications:latest").withRun("-e TITLE=\"${TITLE}\" -e MESSAGE=\"${MESSAGE}\" -e MESSAGE_FORMAT=\"${MESSAGE_TYPE}\" -e EMAIL=\"${EMAILS}\" --name notifications notifications") {}
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"'
            }
        }
    }
}