[英]How to capture errors from running "gcloud app deploy" into a variable (Bash)
I am deploying my application to GCP via bash script.我正在通过 bash 脚本将我的应用程序部署到 GCP。 I want to capture any deployment errors in a variable and perform some actions when a deployment error occurs.我想捕获变量中的任何部署错误并在发生部署错误时执行一些操作。
My deployment command looks like this:我的部署命令如下所示:
gcloud app deploy --version $version --project my-app-$environment $micro.yaml --quiet $no_promote &
You can assume that this command is working with the variables I've passed.您可以假设此命令正在处理我传递的变量。 I've tried the below variations:我尝试了以下变体:
var=$(...)变量=$(...)
var=$(gcloud app deploy --version $version --project my-app-$environment $micro.yaml --quiet $no_promote &) var=$(gcloud app deploy --version $version --project my-app-$environment $micro.yaml --quiet $no_promote &)
echo ******************************回声 *********************************
echo "${var}"回声“${var}”
echo ******************************回声 *********************************
var=`...` var=`...`
var=`gcloud app deploy --version $version --project my-app-$environment $micro.yaml --quiet $no_promote &` var=`gcloud app deploy --version $version --project my-app-$environment $micro.yaml --quiet $no_promote &`
echo ******************************回声 *********************************
echo $var回显 $var
echo ******************************回声 *********************************
Other variations from GCP side that I've tried:我尝试过的 GCP 方面的其他变体:
--verbosity="warning" --verbosity="警告"
var=$(gcloud app deploy --version $version --project my-app-$environment $micro.yaml --quiet $no_promote --verbosity="warning" &) var=$(gcloud app deploy --version $version --project my-app-$environment $micro.yaml --quiet $no_promote --verbosity="warning" &)
--verbosity="error" --verbosity="错误"
var=$(gcloud app deploy --version $version --project my-app-$environment $micro.yaml --quiet $no_promote --verbosity="error" &) var=$(gcloud app deploy --version $version --project my-app-$environment $micro.yaml --quiet $no_promote --verbosity="error" &)
--log-http --log-http
var=$(gcloud app deploy --version $version --project my-app-$environment $micro.yaml --quiet $no_promote --log-http &) var=$(gcloud app deploy --version $version --project my-app-$environment $micro.yaml --quiet $no_promote --log-http &)
--user-output-enabled --user-output-enabled
var=$(gcloud app deploy --version $version --project my-app-$environment $micro.yaml --quiet $no_promote --user-output-enabled &) var=$(gcloud app deploy --version $version --project my-app-$environment $micro.yaml --quiet $no_promote --user-output-enabled &)
Everything that I've tried so far, $var is always returned empty.到目前为止,我尝试过的所有操作 $var 总是返回为空。 Even when there's an error during deployment.即使在部署期间出现错误。
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 0 files to Google Cloud Storage ═╣
╚════════════════════════════════════════════════════════════╝
File upload done.
ERROR: (gcloud.app.deploy) ABORTED: Cannot operate on apps/my-app-qa/services/search/versions/1 because an operation is already in progress for apps/my-app-qa/services/search/versions/1 by 3458c670-76bd-42ec-a971-c0ceda7ef34f.
******************************
******************************
Is there a way to detect a GCP deployment error when the deployment happens through bash script?当通过 bash 脚本进行部署时,有没有办法检测到 GCP 部署错误? Seems like I am missing something really basic but can't put my finger to it.好像我错过了一些非常基本的东西,但我无法理解它。
Update: What I've written below still holds, but I missed the main problem: you try to run the gcloud
tool in the background (via the &
at the end of its command line), which means that call returns immediately, before the $()
construct has a chance to capture anything.更新:我在下面写的仍然有效,但我错过了主要问题:您尝试在后台运行gcloud
工具(通过其命令行末尾的&
),这意味着调用立即返回,在$()
结构有机会捕获任何东西。
What you are essentially doing is to assign the command's output to your variable before the command actually has a chance to output anything.您实际上在做的是在命令实际有机会 output之前将命令的 output 分配给您的变量。 So you have basically two options:所以你基本上有两个选择:
Option 1: run the command in the foreground and continue with your script once it has finished.选项 1:在前台运行命令,并在完成后继续执行脚本。 If the command is running for a long time and the "backgrounding" was intentional, this obviously won't work.如果命令运行了很长时间并且“后台”是故意的,这显然是行不通的。
Option 2: run the command in the background, redirect its stderr to a file (see below) and then actively monitor that file for changes (ie errors).选项 2:在后台运行该命令,将其 stderr 重定向到一个文件(见下文),然后主动监视该文件的更改(即错误)。 That's certainly mire involved and the concrete way to do it depends a lot on what exactly you want your script to do.这肯定涉及到泥潭,具体的实现方式在很大程度上取决于您希望脚本执行的操作。
Original Answer: I'm not familiar with the gcloud tool in particular, but I assume it prints its error messages to stderr (as is the convention for commandline tools).原始答案:我不是特别熟悉 gcloud 工具,但我假设它会将错误消息打印到 stderr(这是命令行工具的约定)。 The $(command)
construct captures stdout, so you get everything except the errors. $(command)
构造捕获 stdout,因此您可以获得除错误之外的所有内容。
To solve this you have to redirect stderr in your gcloud call:要解决此问题,您必须在 gcloud 调用中重定向 stderr:
var=$(gcloud --options 2>&1)
The 2>&1
tells the nested shell spawned by the $()
construct to redirect ( >
) the output stream #2 (= stderr) to the same location ( &
) as stream #1 (= stdout), causing it to be captured into your variable. 2>&1
告诉$()
构造生成的嵌套 shell 将 output stream #2(= stderr)重定向( >
)到与 stream #1(= stdout)相同的位置( &
),导致它被捕获进入你的变量。
Since you likely want to capture only stderr, you also need to get rid of stdout:由于您可能只想捕获 stderr,因此还需要摆脱 stdout:
var=$(gcloud --options 2>&1 >/dev/null)
The >/dev/null
redirects the default stream (= stderr) into the void. >/dev/null
将默认的 stream (= stderr) 重定向到 void。
An alternative if you want to both display the command output and capture its errors is to pipe stderr into tee
(tool that prints the input and saves a copy to a file) and then read the captured error file into your variable:如果您想同时显示命令 output 并捕获其错误,则另一种方法是将 pipe stderr 写入tee
(打印输入并将副本保存到文件的工具),然后将捕获的错误文件读入您的变量:
gcloud --options 2| tee /tmp/my-gcloud-errors
var=$(cat /tmp/my-gcloud-errors)
(answer may contain errors, since I'm typing this on my phone with a cat on my lap. Will fix later) (答案可能包含错误,因为我在手机上输入此内容时腿上有一只猫。稍后会修复)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.