Multistep shell commands on Jenkins pipeline

Viewed 4595

I have a jenkins job that has a shell step with following commands. It runs great!

sudo yum install python36
virtualenv -p python3 test
source test/bin/activate
<some other command>

Now I want to make this into a pipeline. How do I write the same in groovy? I tried using syntax like this but it fails:

stage('Test') {
        steps {
            sh 'sudo yum install python36'
            sh 'virtualenv -p python3 test'
        }
    }
2 Answers

In order to to execute multiple shell commands you need to wrap them in a pair of three single quotes ''':

stage('Test') {
        steps {
            sh '''
               sudo yum install python36
               virtualenv -p python3 test
               '''
        }
}

And if your shell commands container GStrings like ${some_str} use double quotes:

stage('Test') {
        steps {
            sh """
               sudo yum install ${some_package}
               virtualenv -p python3 test
               """
        }
}
Related