Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline bash command in Jenkins pipeline

I have the following sh command in my Jenkinsfile which does not work because it tries to execute the last "DATA" as a command. If I move last "DATA" to the beginning of the line it works but is not as beautiful as I want. Is there a way to the indention in this case?

    sh """
        sshpass -p 'password' ssh -o StrictHostKeyChecking=no appsadm@$backup_registry <<DATA
        sudo /etc/init.d/docker stop || true
        sudo yum remove -y docker-engine.x86_64
        sudo rm -fr /var/lib/docker /var/log/docker
        sudo rpm -iUvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm || true
        sudo yum update -y
        sudo yum -y install docker-io
        sudo sed -i 's#other_args=.*#other_args="--insecure-registry $official_registry:5000"#g' /etc/sysconfig/docker
        sudo /etc/init.d/docker start
        DATA
        """
like image 900
tig Avatar asked Jun 30 '17 08:06

tig


People also ask

How do I comment multiple lines in Jenkins pipeline?

Block Comments. Block comments in Jenkinsfile are used to comment out a block of code. Again, the pattern is similar to Java and C++. A block comment starts with a forward-slash followed by an asterisk (/*) and ends with an asterisk followed by a forward-slash (*/).

Where is Jenkins snippet generator?

To generate a step snippet with the Snippet Generator: Navigate to the Pipeline Syntax link (referenced above) from a configured Pipeline, or at ${YOUR_JENKINS_URL}/pipeline-syntax . Select the desired step in the Sample Step dropdown menu.


1 Answers

I know this is an old question, but I had ran into this at some point, and eventually ended up using stripIndent()

steps {
    echo 'Deploying....'
    sh """
    ssh somewhere <<EOF
    cd somewhere
    do some more stuff
    EOF
    """.stripIndent()
}

That way you can still keep your indentations and formatting

like image 148
waaffles Avatar answered Oct 16 '22 11:10

waaffles