簡體   English   中英

Jenkins Pipeline sh命令處理輸出並引發錯誤

[英]Jenkins Pipeline sh command working with output and raising errors

我將Jenkins Pipeline用作業務流程系統,以進行較為復雜的構建和部署管道。 我是Groovy的新手,盡管遇到了麻煩,並且找到了解決方法,但我覺得必須有相當簡單的方法才能完成以下工作。

假設我有一些Ruby文件:

def some_method(x, y)
  fail StandardError if x == "cat"
  puts "i love #{x} and #{y}"
end

some_method ARGV[0], ARGV[1]

我想運行管道外殼命令sh並使其表現出以下行為(我正在使用偽rspec來傳達期望):

describe "When I run a Jenkins Pipeline shell command" do
  subject { sh "ruby config/deploy/some_script.rb ${x} ${y}" }
  let(:x) { "foo" }
  let(:y) { "bar" }
  context "when the shell command has a 0 return code" do
    it "is the stdout from the shell command execution" do
      expect(subject).to eq "i love foo and bar"
    end
  end
  context "when the shell has a non-zero return code" do
    let(:x) { "cat" }
    it "raises a Jenkins Pipeline error" do
      expect(subject).to invoke groovy_jenkins_pipeline_command_error
    end
  end
end 

實際發生的情況是,返回代碼始終由sh命令返回,而我無法直接訪問stdout。 我可以將stdout傳遞到/ dev / null,但是隨后我得到了一個含糊的返回值,其中包含返回代碼,我想要的輸出以及其他我並不真正關心的stdout。

理想情況下,我什至不必在Ruby代碼中調用puts,而是讓代碼返回結果並將其傳遞到groovy運行時。

在Jenkins Pipeline sh命令內調用AWS eb cli客戶端時,我也遇到這個問題。

感謝幫助。

一種方法是告訴外殼獲取標准輸出:

myRubyScript="ruby config/deploy/some_script.rb ${x} ${y}"
def returncode=sh script: "$myRubyScript", returnStdout: true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM