Search code examples
jenkinsjenkins-pipelinesalt-project

Passing minion list to Jenkins pipeline, Error: workflowScript: 15: illegal string body character after dollar sign;


Below is the Jenkins pipeline. which runs a state against the list of minions stored in a .txt file on the salt master server. The below command runs fine on the salt master cli:

 salt --list `awk -vORS=, '{ print $1 }' /srv/salt/TMjenkins/minions.txt | sed 's/,$/\n/'` test.ping

However, when I run it through the Jenkins pipeline, I get illegal string body character after dollar sign. The salt master is in a remote server, hence I can't execute the cmd natively. so, far i have tried with passing the cmd in """ """ and ''' ''', also { print \"${1}\" }. Nothing has worked so far. Any suggestion, appreciated.

pipeline = {
      ansiColor('xterm') {
      def remote = [:]
      remote.name = 'saltmaster'
      remote.host = 'xx.xxx.xx.x'
      remote.allowAnyHosts = true
            withCredentials([usernamePassword(credentialsId: 'saltmaster', passwordVariable: 'password', usernameVariable: 'ops')]) {
            remote.user = 'xxx'
            remote.password = password 
            			 
            stage('Filetransfer') {
                  
                 sshCommand remote: remote, command: " salt -L  `awk -vORS=, '{ print \"${1}\" }' /srv/salt/TMjenkins/minions.txt | sed 's/,$/\n/'` test.ping "
                      
            }
        }			
			 sh '/home/jenkins/jenkins-data/slack_notification.sh " ${minionid}" "Deployment finished successfully" "good" ":jenkins:"'
      } 
	} 
postFailure = {
    sh '/home/jenkins/jenkins-data/slack_notification.sh " ${minionid}" "Unfortunately deployment was unsuccessful this time" "danger" ":jenkinserror:"'
}

postAlways = {
    echo 'Cleaning Workspace now'
    env.WORKSPACE = pwd()
    sh "rm ${env.WORKSPACE}/* -fr"
}

node{
     properties([
     parameters([
         
			string(name: 'Region', defaultValue: '', description: 'Region for which the process should run. ')
             ])
      ]) 
    try {
        pipeline()
    } catch (e) {
        postFailure()
        throw e
    } finally {
        postAlways()
    }
}


Solution

  • You need to escape your $ sign if you want to pass it over. So:

    awk -vORS=, '{ print $1 }' /srv/salt/TMjenkins/minions.txt
    

    becomes

    sh "awk -vORS=, '{ print \$1 }' /srv/salt/TMjenkins/minions.txt "
    

    and

    sed 's/,$/\n/'
    

    becomes

    sh "sed 's/,\$/\n/'"
    

    Finally, instead of using bash scripts to send Slack notifications, you should use Slack plugin for Jenkins, like this:

    slackSend color: "danger", text: "Failed"
    

    See also