Home > Software engineering >  Conditional step in Jenkinsfile
Conditional step in Jenkinsfile

Time:11-29

I have the following situation:

.
.
.
stage('My stage')
{
  steps
  {
    sh 'my_command1.sh'
    sh 'my_command2.sh'
    sh 'my_command3.sh'
  }
}
.
.
.

It is possible for example to execute the step "my_command2.sh" only if there is a particular condition? I know that it is possible to define conditional statement with "when" token, but the condition has an impact to all the stage. I want to limit this behavior only for a single step.

Thanks.

CodePudding user response:

You can use script Then your stage will be like:

stage('My Stage') {
    steps {
       script{
           sh 'my_command1.sh'    
           if(condition){
               sh 'my_command2.sh'           
           }
           sh 'my_command3.sh'   
           }
        }
    }

You can also check declarative step from official documentation

  • Related