从 jenkins groovy 脚本中的 bash 脚本中捕获退出代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48174857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:44:44  来源:igfitidea点击:

Capture exit code from bash scripts in jenkins groovy scripts

bashjenkinsjenkins-pipelineexitjenkins-groovy

提问by Foobar-naut

Executing a bash script copy_file.shfrom Jenkins Groovy script and trying to shoot mail depending upon the exit code generated form the bash script.

copy_file.sh从 Jenkins Groovy 脚本执行 bash 脚本并尝试根据从 bash 脚本生成的退出代码发送邮件。

copy_file.sh:

copy_file.sh

#!/bin/bash

$dir_1=/some/path
$dir_2=/some/other/path

if [ ! -d $dir ]; then
  echo "Directory $dir does not exist"
  exit 1
else
  cp $dir_2/file.txt $dir_1
  if [ $? -eq 0 ]; then
      echo "File copied successfully"
  else
      echo "File copy failed"
      exit 1
  fi
fi

Portion of the groovy script:

的部分groovy script

stage("Copy file")  {
    def rc = sh(script: "copy_file.sh", returnStatus: true)
    echo "Return value of copy_file.sh: ${rc}"
    if (rc != 0) 
    { 
        mail body: 'Failed!',       
        subject: 'File copy failed',        
        to: "[email protected]"       
        System.exit(0)
    } 
    else 
    {
        mail body: 'Passed!',   
        subject: 'File copy successful',
        to: "[email protected]"
    }
}

Now, irrespective of the exit 1s in bash script, groovy script is always getting return code 0in rcand shooting Passed!mails!

现在,无论的exit 1在bash脚本S,Groovy脚本总是得到返回代码0rc,投篮命中率Passed!邮件!

Any suggestions why I can not receive the exit code from bash script in this Groovy script?

有什么建议为什么我无法在此 Groovy 脚本中从 bash 脚本接收退出代码?

DO I NEED TO USE RETURN CODE INSTEAD OF EXIT CODE?

我是否需要使用返回代码而不是退出代码?

回答by natansun

Your groovy code is OK.

你的常规代码没问题。

I created a new pipeline job to check your problem, but altered it a bit.

我创建了一个新的管道作业来检查您的问题,但对其进行了一些更改。

Instead of running your shell script copy_file.shI created ~/exit_with_1.shscript, that only exits with an exit code of 1.

copy_file.sh我创建的~/exit_with_1.sh脚本没有运行你的 shell 脚本,它只以退出代码 1 退出。

The job has 2 steps:

作业有 2 个步骤:

  1. Creation of ~/exit_with_1.shscript

  2. Run the script and check the exit code that stored in rc.

  1. ~/exit_with_1.sh脚本的创建

  2. 运行脚本并检查存储在rc.

I got 1as an exit code in this example. If you think something wrong with your groovy <-> bashconfiguration, consider replace your copy_file.shcontent with only exit 1then try to print the result (before posting the emails).

1在这个例子中,我得到了一个退出代码。如果您认为您的groovy <-> bash配置有问题,请考虑替换您的copy_file.sh内容,exit 1然后尝试打印结果(在发布电子邮件之前)。

The jenkins job I created:

我创建的詹金斯工作:

node('master') {
    stage("Create script with exit code 1"){
            // script path
            SCRIPT_PATH = "~/exit_with_1.sh"

            // create the script
            sh "echo '# This script exits with 1' > ${SCRIPT_PATH}"
            sh "echo 'exit 1'                    >> ${SCRIPT_PATH}"

            // print it, just in case
            sh "cat ${SCRIPT_PATH}"

            // grant run permissions
            sh "chmod +x ${SCRIPT_PATH}"
    }
    stage("Copy file")  {
        // script path
        SCRIPT_PATH = "~/exit_with_1.sh"

       // invoke script, and save exit code in "rc"
        echo 'Running the exit script...'
        rc = sh(script: "${SCRIPT_PATH}", returnStatus: true)

        // check exit code
        sh "echo \"exit code is : ${rc}\""

        if (rc != 0) 
        { 
            sh "echo 'exit code is NOT zero'"
        } 
        else 
        {
            sh "echo 'exit code is zero'"
        }
    }
    post {
        always {
            // remove script
            sh "rm ${SCRIPT_PATH}"
        }
    }
}