【问题标题】:Using If statement with shell commands in jenkins Declarative pipeline cloudformation在 jenkins 声明性管道 cloudformation 中使用带有 shell 命令的 If 语句
【发布时间】:2020-07-15 23:18:22
【问题描述】:

我正在将 aws cloudformation 集成到我的 jenkins 管道中。我想执行一个

$ aws cloudformation describe-stacks --stack-name dev-nics-proxyservlet-svc --region us-west-2

命令查看我是否有一个带有我正在寻找的名称的堆栈。如果命令发现栈存在,我想删除栈:

$ aws cloudformation delete-stack --stack-name dev-nics-proxyservlet-svc

但是如果堆栈不存在,我想创建堆栈:

aws cloudformation create-stack --stack-name dev-nics-proxyservlet-svc --region us-west-2 --template-body file://dev-nics-proxyservlet-cluster.yml --parameters file://dev-nics-proxyservlet-svc-param.json --capabilities "CAPABILITY_IAM" "CAPABILITY_NAMED_IAM"

如何在声明性的多分支 jenkins 管道中编写这个 shell comman?任何帮助表示赞赏。 谢谢!

【问题讨论】:

  • 这应该在bash?
  • 我这么认为?不确定是否有办法在 groovy 中做到这一点
  • 好的。您可以在 bash 中查看我的答案。

标签: shell if-statement jenkins amazon-cloudformation multibranch-pipeline


【解决方案1】:

我认为这些方面的东西应该可行:

if aws cloudformation describe-stacks --stack-name dev-nics-proxyservlet-svc --region us-west-2 &>/dev/null 
then
    aws cloudformation delete-stack --stack-name dev-nics-proxyservlet-svc
else
    aws cloudformation create-stack --stack-name dev-nics-proxyservlet-svc --region us-west-2 --template-body file://dev-nics-proxyservlet-cluster.yml --parameters file://dev-nics-proxyservlet-svc-param.json --capabilities "CAPABILITY_IAM" "CAPABILITY_NAMED_IAM"
fi

if 通过检查aws cloudformation describe-stacks退出代码来工作。如果为0,则栈存在,如果不为0,则不存在。

【讨论】:

  • 谢谢!快速提问如何将响应转发到 &>/dev/null 给我一个退出代码?
  • @giturr14 你好。它没有,它只是使输出更清洁。如果需要,您可以将其删除。顺便说一句,如果我的回答有帮助,我们将不胜感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
相关资源
最近更新 更多