如何在Jenkins管道中的AWS CLI中转义引号? [英] How to escape quotes in a AWS CLI in a Jenkins pipeline?

查看:134
本文介绍了如何在Jenkins管道中的AWS CLI中转义引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个AWS CLI命令来修改我的ALB侦听器之一的DefaultActions块.我在终端上执行此操作.

I'm trying to run an AWS CLI command to modify the DefaultActions block of one of my ALB listeners. I do this on my terminal.

$ aws elbv2 modify-listener --listener-arn arn:aws:elasticloadbalancing:us-east-1:1234567890:listener/app/my-alb/123456789 --default-actions '[{"Type": "redirect", "RedirectConfig": {"Protocol": "HTTPS", "Port": "443", "Host": "#{host}", "Query": "#{query}", "Path": "/#{path}", "StatusCode": "HTTP_301"}}]'

如何在Jenkins管道中对此进行编码,以便保留--default-actions字符串中的单引号和双引号?我现在这样做

How would I code this in a Jenkins pipeline so the single-quotes and double-quotes in the --default-actions string are preserved? I do this now

def defaultActions = '[{"Type": "redirect", "RedirectConfig": {"Protocol": "HTTPS", "Port": "443", "Host": "#{host}", "Query": "#{query}", "Path": "/#{path}", "StatusCode": "HTTP_301"}}]'
sh """
    aws elbv2 modify-listener --listener-arn arn:aws:elasticloadbalancing:us-east-1:1234567890:listener/app/my-alb/123456789 --default-actions \\'$defaultActions\\'
"""

但它被解释为删除双引号的地方.

But it is interpreted as this, where the double-quotes are removed.

aws elbv2 modify-listener --listener-arn arn:aws:elasticloadbalancing:us-east-1:803597461034:listener/app/sb-zift-admin-lb/375b68a2c9e7550c/fbb5e9154eb73ab5 --default-Actions '[{Type: redirect, RedirectConfig: {Protocol: HTTPS, Port: 443, Host: #{host}, Query: #{query}, Path: /#{path}, StatusCode: HTTP_301}}]'

顺便说一句,由于我了解即使API可用,但该选项在Cloudformation中仍然不可用,所以我在AWS CLI中执行此操作.

On a side note, I'm doing this in AWS CLI since I understand that even though the API is available, the option is not available in Cloudformation yet.

推荐答案

您还需要在defaultActions字符串中转义双引号. sh管道步骤在将参数传递到外壳之前执行参数扩展.在您的情况下,像\\"这样的双斜杠转义"应该可以解决问题:

You need to escape double quotes in defaultActions string as well. The sh pipeline step performs parameter expansion before passing it to the shell. In your case escaping " with double slash like \\" should do the trick:

def defaultActions = '[{\\"Type\\": \\"redirect\\", \\"RedirectConfig\\": {\\"Protocol\\": \\"HTTPS\\", \\"Port\\": \\"443\\", \\"Host\\": \\"#{host}\\", \\"Query\\": \\"#{query}\\", \\"Path\\": \\"/#{path}\\", \\"StatusCode\\": \\"HTTP_301\\"}}]'
sh """
    aws elbv2 modify-listener --listener-arn arn:aws:elasticloadbalancing:us-east-1:1234567890:listener/app/my-alb/123456789 --default-actions \\'${defaultActions}\\'
"""

我用echo命令而不是aws运行了一个示例,它产生了预期的结果:

I have run an example with echo command instead aws and it produced expected result:

+ echo elbv2 modify-listener --listener-arn arn:aws:elasticloadbalancing:us-east-1:1234567890:listener/app/my-alb/123456789 --default-actions '[{"Type": "redirect", "RedirectConfig": {"Protocol": "HTTPS", "Port": "443", "Host": "#{host}", "Query": "#{query}", "Path": "/#{path}", "StatusCode": "HTTP_301"}}]'
elbv2 modify-listener --listener-arn arn:aws:elasticloadbalancing:us-east-1:1234567890:listener/app/my-alb/123456789 --default-actions '[{"Type": "redirect", "RedirectConfig": {"Protocol": "HTTPS", "Port": "443", "Host": "#{host}", "Query": "#{query}", "Path": "/#{path}", "StatusCode": "HTTP_301"}}]'

这篇关于如何在Jenkins管道中的AWS CLI中转义引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆