简体   繁体   English

bash脚本中的grep + Jenkins

[英]grep in bash script + Jenkins

I have a grep command that works in a bash script: 我有一个可以在bash脚本中使用的grep命令:

if grep 'stackoverflow' outFile.txt; then
 exit 1
fi

This works fine when run on my host. 在我的主机上运行时,这工作正常。 When I call this from a Jenkins build step however, it exits 0 everytime, not seeing 'stackoverflow'. 但是,当我从Jenkins构建步骤调用此函数时,它每次都会退出0,而不会看到“ stackoverflow”。 What is going wrong? 怎么了?

Add the following line as the first line in your "Execute Shell" command 将以下行添加为“ Execute Shell”命令中的第一行

#!/bin/sh

grep command exits with a non zero code when it does not find match and that causes jenkins to mark the job as failed. 如果找不到匹配项,则grep命令以非零代码退出,这会导致jenkins将作业标记为失败。 See Below. 见下文。

In the help section of "Execute Shell" 在“执行外壳”的帮助部分

Runs a shell script (defaults to sh, but this is configurable) for building the project. 运行用于构建项目的Shell脚本(默认为sh,但这是可配置的)。 The script will be run with the workspace as the current directory. 该脚本将以工作空间作为当前目录运行。 Type in the contents of your shell script. 输入您的shell脚本的内容。 If your shell script has no header line like #!/bin/sh —, then the shell configured system-wide will be used, but you can also use the header line to write script in another language (like #!/bin/perl) or control the options that shell uses. 如果您的shell脚本没有像#!/ bin / sh这样的标题行,那么将使用在系统范围内配置的shell,但是您也可以使用标题行以另一种语言编写脚本(例如#!/ bin / perl)。 )或控制shell使用的选项。

By default, the shell will be invoked with the "-ex" option. 默认情况下,将使用“ -ex”选项调用该外壳程序。 So all of the commands are printed before being executed, and the build is considered a failure if any of the commands exits with a non-zero exit code. 因此,所有命令都将在执行前打印出来,如果任何命令以非零退出代码退出,则构建被视为失败。 Again, add the #!/bin/... line to change this behavior. 同样,添加#!/ bin / ...行以更改此行为。

As a best practice, try not to put a long shell script in here. 作为最佳实践,请尽量不要在此处放置较长的shell脚本。 Instead, consider adding the shell script in SCM and simply call that shell script from Jenkins (via bash -ex myscript.sh or something like that), so that you can track changes in your shell script. 相反,可以考虑在SCM中添加shell脚本,然后只需通过jash从Jenkins调用该shell脚本(通过bash -ex myscript.sh或类似方法),以便您可以跟踪shell脚本中的更改。

I am a bit confused by the answers on this question! 这个问题的答案让我有些困惑! ie Sorry, but the answers here are incorrect for this question. 即对不起,但是这里的答案对于这个问题是不正确的。 The question is good/interesting as plain grep in scripts does cause scripts to exit with failure if the grep is not successful (which can be unexpected), whereas a grep inside an if will not cause exit with failure. 这个问题是好/有趣的,因为如果grep不成功(这可能是意外的),脚本中的普通grep确实会导致脚本退出并失败,而if中的grep不会导致失败而退出。

For the example shown in the question exit 1 will be done IF the grep command runs successfully(file exists) AND if the string is found in file. 对于问题1中所示的示例,如果grep命令成功运行(文件存在)并且在文件中找到了字符串,则将完成出口1。 (grep command returns 0 exit code to if). (grep命令向if返回0退出代码)。

@Gonen's comment to add 'ls -l outFile.txt' should have been followed up on to see what the real reason for failure was. @Gonen添加“ ls -l outFile.txt”的评论应该被跟踪,以了解失败的真正原因是什么。

TLDR; TLDR; if catches the exit code of commands inside the if clause: if在if子句中捕获命令的退出代码:

A grep command that 'fails'(no match or error) inside an if statement in jenkins will not cause jenkins script to stop. 在jenkins的if语句内“失败”(不匹配或错误)的grep命令不会导致jenkins脚本停止。 Whereas a grep command that fails not inside an if will cause jenkins to stop and exit with fail. 而没有在if中失败的grep命令将导致jenkins停止并失败退出。

The exit/return code handling is different for commands inside an if statement in shell. shell中if语句中的命令的退出/返回代码处理不同。 if catches the return code and no matter if command was successful or failed the if will return success to $0(after if) (and do actions in if or else). if捕获返回码,无论命令成功还是失败,if都会将成功返回到$ 0(在if之后)(并在if or else中执行操作)。

From man bash: 来自man bash:

if list; 如果列表; then list; 然后列出; [ elif list; [elif list; then list; 然后列出; ] ... [ else list; ] ... [else list; ] fi The if list is executed. ] fi执行if列表。 If its exit status is zero, the then list is executed. 如果其退出状态为零,则执行then列表。 Otherwise, each elif list is executed in turn, and if its exit status is zero, the corresponding then list is executed and the command completes. 否则,依次执行每个elif列表,如果其退出状态为零,则执行相应的then列表,命令完成。 Otherwise, the else list is executed, if present. 否则,将执行else列表(如果存在)。 The exit status is the exit status of the last command executed, or zero if no condition tested true . 退出状态是最后执行的命令的退出状态, 如果没有条件测试为true ,则为零

To illustrate, try this (same result in bash or sh): 为了说明,请尝试以下操作(bash或sh的结果相同):

$ if grep foo bar ; then echo got it; fi; echo $?
grep: bar: No such file or directory
0
$ touch bar
$ if grep foo bar ; then echo got it; fi; echo $?
0
$ echo foo >bar
$ if grep foo bar ; then echo got it; fi; echo $?
foo
got it 
0
$ if grep foo bar ; then echo gotit; grep gah mah; fi; echo $?
foo
gotit
grep: mah: No such file or directory
2

I think you have error in your script. 我认为您的脚本有错误。 You must add 'fi' at the end of 'if' block: 您必须在“ if”块的末尾添加“ fi”:

if grep 'stackoverflow' outFile.txt; then
 exit 1
fi

If the two were exactly the same it should work. 如果两者完全相同,则应该起作用。 Is your current directory or user different in the two environments? 您的当前目录或用户在两种环境中是否不同? You might not be able to read the file. 您可能无法读取该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM