【问题标题】:Passing minion list to Jenkins pipeline, Error: workflowScript: 15: illegal string body character after dollar sign;将 minion 列表传递给 Jenkins 管道,错误:workflowScript:15:美元符号后的非法字符串主体字符;
【发布时间】:2020-05-20 06:28:32
【问题描述】:

下面是 Jenkins 流水线。它针对存储在 salt 主服务器上的 .txt 文件中的 minions 列表运行状态。以下命令在 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()
    }
}

【问题讨论】:

  • 你需要转义$

标签: jenkins jenkins-pipeline salt-stack salt


【解决方案1】:

如果您想传递 $ 符号,您需要将其转义。所以:

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/'"

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

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

See also

【讨论】:

  • 谢谢,Martac。在当前管道中,根据 cmd test.ping 的所有 minions 的返回,通知作为成功或失败发出。如何根据结果分别为每个 minion 发送通知?注意:我想从 .txt 文件中传递所有的奴才,而不是从 cmd 传递它自己的。
  • 这应该是一个单独的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-22
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
相关资源
最近更新 更多