2016-12-02 49 views
0

我正在翻译Jenkins 2自由式作业作为Groovy的管道作业,而我在这方面的经验很少。我无法为我的生活弄清楚如何让参数在Groovy中运行。这是剧本的重要部分;如何在Groovy中使用参数运行shell脚本

stage ('Clean') { 
    try { 
     notifyBuild('STARTED') 

     dir("cloudformation") { 

      def list = sh(script: "ls -1 *.template", returnStdout: true) 

      for (i in list) { 
       sh "aws s3 cp $i s3://URLHERE —expires 1 —cache-control 1" 
      } 

     } } catch (e) { 
     // If there was an exception thrown, the build failed 
     currentBuild.result = "FAILED" 
     throw e 
    } finally { 
     // Success or failure, always send notifications 
     notifyBuild(currentBuild.result) 
    } } 

相关位是sh "aws s3 cp $i s3://URLHERE —expires 1 —cache-control 1"。试图运行这将返回以下错误;

[cloudformation] Running shell script 
+ aws s3 cp e s3://URLHERE —expires 1 —cache-control 1 

Unknown options: —expires,1,—cache-control,1 

谷歌已经很少在shell脚本中使用Groovy中的参数。显然,它试图将每个空间划分的块作为自己的位来处理;我该如何阻止这种行为?

编辑补充: 我试图sh "aws s3 cp $i s3://URLHERE '—expires 1' '—cache-control 1'"然后返回相同的错误,但与Unknown options: —expires 1,—cache-control 1所以我得到我可以通过适当地引用包含空格,但仍有潜在的问题。

回答

0

缓存控制参数需要2破折号--cache-control <value>以及expires参数。

查看S3 documentation of cp

+0

试了一下,伴随着直觉。结束了'sh'aws s3 cp $ i s3:// URLHERE“--expires 1”“--cache-control 1”'''交换引号类型以及双破折号修正,不知道它是如何搞砸的我现在正在运行'[cloudformation]正在运行的shell脚本 + aws s3 cp s3:// URLHERE --expires 1 --cache-control 1 未知选项:--cache-control 1' 因此,它是现在识别过期,但不是缓存控制?任何其他想法? – Alex

+0

是的。不要在选项组周围使用双引号。这使得每个带引号的字符串看起来像一个shell的参数。另外,整个命令行字符串中的单引号应该是双引号,以便Groovy变量插值可以工作。你的原始命令行与选项上的双破折号应该有所斩断。 – BalRog

相关问题