2016-12-02 221 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