如何在Jenkins管道中的AWS CLI中转义引号?

时间:2018-10-16 20:41:28

标签: groovy jenkins-pipeline

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

$ 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字符串中的单引号和双引号?我现在这样做

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\\'
"""

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

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中这样做。

1 个答案:

答案 0 :(得分:4)

您还需要在namespace CloneAControl { public partial class Form1 : Form { private int yValue = 50; public Form1() { InitializeComponent(); } private void button3_Click(object sender, EventArgs e) { Panel ctrl = panel1.Clone(); //Control ctrl = ControlFactory.CloneCtrl(this.panel1); this.Controls.Add(ctrl); //ctrl.Text = "created by clone"; ctrl.SetBounds(ctrl.Bounds.X, ctrl.Bounds.Y + yValue, ctrl.Bounds.Width, ctrl.Bounds.Height); yValue = yValue + 50; ctrl.BackColor = Color.Red; ctrl.Show(); } } public static class ControlExtensions { public static T Clone<T>(this T controlToClone) where T : Control { PropertyInfo[] controlProperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); T instance = Activator.CreateInstance<T>(); foreach (PropertyInfo propInfo in controlProperties) { if (propInfo.CanWrite) { if (propInfo.Name != "WindowTarget") propInfo.SetValue(instance, propInfo.GetValue(controlToClone, null), null); } } return instance; } } } 字符串中转义双引号。 defaultActions流水线步骤在将参数传递到外壳之前执行参数扩展。在您的情况下,使用sh这样的双斜杠转义"可以解决问题:

\\"

我用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