firasKoubaa
firasKoubaa

Reputation: 6867

Jenkinsfile : how to modify a string parameter passed in pipeline jenkins

I'm using Jenkins pipeline.

Under my jenkinsfile , i'm calling an ansible playbook within a shell command :

It looks like this :

stage('Run Playbook') {
  steps {
    script{
      sh " ansible-playbook myplaybook.yml \
          -e myparam=\"${MY_PARAM}\" "
    }
  }
}

As you can see : in the job my param is MY_PARAM , it's a string parameter , and it may contain some spaces.

My purpose is to replace all spaces woth comma (-) , and pass it to the playbook ?

so i was suggested to inject :

.replace('', '-')

but with jenkinsfile syntax , i wasn t able to do it correctly

Suggestions ?

Upvotes: 0

Views: 5267

Answers (1)

Adam Smith
Adam Smith

Reputation: 54223

everything inside the ${...} is a regular Groovy expression, so you can do whatever sorts of translations you'd like.

stage('Run Playbook') {
  steps {
    script {
      sh "ansible -playbook myplaybook.yml -e myparam=\"${MY_PARAM.replace(' ', '-')}\""
    }
  }
}

Alternatively you can do this replacement in an environment block.

stage('Run Playbook') {
  environment {
    ANSIBLE_MY_PARAM="${MY_PARAM.replace(' ', '-')}"
  }
  steps {
    script {
      sh "ansible -playbook myplaybook.yml -e myparam=\"${ANSIBLE_MY_PARAM}\""
    }
  }
}

but that seems overkill in this limited case.

Upvotes: 1

Related Questions