我正在尝试编写一个脚本,该脚本可以检查URL是否已启动,然后将控制台输出发布到电子邮件中,例如“ 200 ok”站点已启动 方案2:仅在网站关闭后才会触发电子邮件
下面的代码有什么问题
def msgbody='200 OK'
if ((curl -Is https://google.com | head -1) == '$msgbody')
echo ==========Success
) else (
echo ==========Fail
)
fi (What is wrong in this code)
答案 0 :(得分:0)
您应该先查看URL类。 http://docs.groovy-lang.org/latest/html/groovy-jdk/java/net/URL.html
这将使您可以使用getText()
在特定URL下读取整个页面。如果页面关闭,将引发异常。
Jenkins应该会自动使工作失败。在Jenkins职位定义中,您可以指定何时以及如何发送邮件。
编辑这不是有效的Groovy:(curl -Is https://google.com | head -1)
。 Groovy无法像BASH一样使用|
运行shell命令或构建管道。另外,您必须在if之后而不是{}
之后的then / else块周围使用()
。最后,在Groovy中没有fi
:
if (condition) {
...then...
} else {
...
}
有关如何从Groovy脚本运行shell命令的信息,请参见Groovy executing shell commands。
不是必须检查exitValue()
的结果以确定命令是否失败。 0
是成功。
答案 1 :(得分:0)
您可以将其作为Windows批处理执行此操作(您需要安装curl并在PATH中对其进行配置):
@echo off
curl --silent --output nul --show-error --fail http://domain-or-ip/context
if %ERRORLEVEL% EQU 0 (
echo ==========Success
) else (
echo ==========Fail
exit /b %errorlevel%
)