正在将宠物列表传递到Jenkins管道,错误:工作流脚本:15:美元符号后的字符串体字符非法;

wixjitnu  于 2023-01-29  发布在  Jenkins
关注(0)|答案(1)|浏览(155)

下面是Jenkins pipeline。它针对salt master服务器上存储在.txt文件中的奴才列表运行一个状态。下面的命令在salt master cli上运行良好:

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

但是,当我通过Jenkins管道运行它时,我在美元符号后得到非法的字符串主体字符。salt master在远程服务器中,因此我无法在本地执行cmd。到目前为止,我尝试在“””“"”和“"”'''中传递cmd,也是{ print "${1}\”}。到目前为止没有任何效果。如有任何建议,不胜感激。

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()
    }
}
toe95027

toe950271#

如果你想跳过它,你需要避开你的$符号。

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

变成

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

以及

sed 's/,$/\n/'

变成

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

最后,不要使用bash脚本发送Slack通知,而应该使用Slack plugin for Jenkins,如下所示:

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

See also

相关问题