skip to Main Content

When I run the script below it works fine but I have to enter the PublicDNS in the script. This script has single quotes.

          sh '''
    ssh -o StrictHostKeyChecking=no 
        -i my-key.pem 
        [email protected] 
        "sudo yum update -y && 
        sudo yum install docker -y && 
        sudo systemctl start docker && 
        sudo docker pull image/test:be && 
        sudo docker run -d -p 8080:8080 image/test:be"
'''

However, when I try to add a variable for DNS in the second script and as a result change the script to double quotations then error shown below is thrown

sh """
    ssh -o StrictHostKeyChecking=no 
        -i my-key.pem 
        ec2-user@$DNS 
        "sudo yum update -y && 
        sudo yum install docker -y && 
        sudo systemctl start docker && 
        sudo docker pull image/test:be && 
        sudo docker run -d -p 8080:8080 image/test:be"
"""

Error:

/var/jenkins_home/workspace/script.sh: 3: sudo yum update -y && sudo yum install docker -y && sudo systemctl start docker && sudo docker pull image/test:be && sudo docker run -d -p 8080:8080 image/test:be: not found

Any idea what I am doing wrong? Thanks

2

Answers


  1. Use a here-document:

    sh <<EOF
    ssh -o StrictHostKeyChecking=no 
        -i my-key.pem 
        ec2-user@$DNS 
        "sudo yum update -y && 
        sudo yum install docker -y && 
        sudo systemctl start docker && 
        sudo docker pull image/test:be && 
        sudo docker run -d -p 8080:8080 image/test:be"
    EOF
    
    Login or Signup to reply.
  2. 100% Working

    Simply just use heredoc instead:

    sh <<EOF
        ssh -o StrictHostKeyChecking=no 
            -i my-key.pem 
            ec2-user@$DNS 
            "sudo yum update -y && 
            sudo yum install docker -y && 
            sudo systemctl start docker && 
            sudo docker pull image/test:be && 
            sudo docker run -d -p 8080:8080 image/test:be"
    EOF
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search