Escaping parameters with white space in the docker .withRun command in Jenkins declarative pipeline

倖福魔咒の 提交于 2021-02-11 14:33:09

问题


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"

                    }
                }
            }
        }

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).

EDIT: 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"
                    }

回答1:


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.

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

EDIT:

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 */
        }
    }
}

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

Or even simpler

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

EDIT2:

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") {}

EDIT3:

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


来源:https://stackoverflow.com/questions/56022929/escaping-parameters-with-white-space-in-the-docker-withrun-command-in-jenkins-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!