【问题标题】:How to run AWS CLI commands inside jenkinsfile in a loop如何在 jenkinsfile 中循环运行 AWS CLI 命令
【发布时间】:2021-12-08 05:16:48
【问题描述】:

我正在尝试创建一个 jenkins 作业,该作业在触发时将使用特定标签更新所有 Elasticache Redis 复制组。

主要的工作流程是我找到一个区域中的所有 Redis 副本组,例如 us-east-1

def findAllRedisReplicationGroups(label, region) {
  query = "ReplicationGroups[*].{arn: ARN}"

  output = sh(
    script: "aws --region ${region} elasticache describe-replication-groups --query '${query}' --output text",
    label: label,
    returnStdout: true
  )
  return output
}

这里的输出将是一个字符串示例

String a = """
          arn:aws:elasticache:us-west-2:AccountID:replicationgroup:application-2-test
          arn:aws:elasticache:us-west-2:AccountID:replicationgroup:application-1-test
          """

然后我将字符串拆分为一个列表,其中每个 arn 都是一个元素。 然后使用for循环遍历所有Redis副本组并获取它们的标签,如果标签像Environment: test那么Redis副本组的arn将被添加到arn列表中

def findCorrectEnvReplicationGroups(label, region, environment, redis_arns){
  def arn_list = redis_arns.split();
  def correct_env_arn_list = [];
  for( def arn : arn_list) {
    def redisTags = getRedisTags(label, region, arn)

    def jsonSlurper = new groovy.json.JsonSlurper()
    def object = jsonSlurper.parseText(redisTags)

    EnvironmentFromTag = object.TagList.find { it.Key == "Environment"}

    if (EnvironmentFromTag.Value == environment) {
      correct_env_arn_list.add(arn)
    }
    break
  }
  return correct_env_arn_list
}

def getRedisTags(label, region, arn) {
  output = sh(
    script: "aws --region ${region} elasticache list-tags-for-resource --resource-name ${arn} --output json",
    label: label,
    returnStdout: true
  )

  return output
}

我完成了 1 个循环。通过打印出每个循环的 arn 进行测试,但在尝试再次运行 getRedisTags 方法中的脚本时崩溃。

输出应该是标签匹配的arns列表

有没有人遇到过这样的错误或有任何 groovy 的经验,也许可以提供帮助 我想知道为什么 jenkinsfile 在尝试循环运行 aws cli 命令时崩溃

非常感谢

【问题讨论】:

    标签: groovy jenkins-pipeline aws-cli jenkins-groovy amazon-elasticache


    【解决方案1】:

    仍然不完全确定为什么它不起作用,但使用 groovy-s 内置的迭代器我让它工作了

    1. 首先我得到所有 Redis 复制组的列表
    2. 然后将其插入到findEnvironmentReplicationGroups 中,我使用.findAll 将arns 一个一个地输入到方法getRedisTags 中,该方法每次都返回给我一张地图(例如=[Environment: "test"]
    3. 然后我将输出[Environment: "test"] 存储到变量tags 中,并检查它是否与给定方法的环境匹配
    4. 如果匹配,则将其添加到correctArns,并最终返回
    def findAllRedisReplicationGroups(region) {
      def query = "ReplicationGroups[*].ARN"
    
      def output = sh(
        script: "aws --region ${region} elasticache describe-replication-groups --query '${query}' --output json",
        label: "Find all Redis Replication groups in the region",
        returnStdout: true
      )
      readJSON(text: output)
    }
    
    def findEnvironmentReplicationGroups(region, environment, listOfRedisArns){
      def correctArns = listOfRedisArns.findAll { arn ->
        def tags = getRedisTags(region, arn).collectEntries { [it.Key, it.Value] }
        tags.Environment == environment
      }
      correctArns
    }
    
    def getRedisTags(region, arn) {
      def query = "TagList[?Key==`Environment`]"
      def output = sh(
        script: "aws --region ${region} elasticache list-tags-for-resource --resource-name ${arn} --query '${query}' --output json",
        label: "Get tags for a Redis replication group",
        returnStdout: true
      )
      readJSON(text: output)
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 2021-12-17
      • 1970-01-01
      • 2021-04-20
      • 2022-08-18
      • 2020-04-26
      • 1970-01-01
      • 2017-09-21
      • 2018-01-25
      相关资源
      最近更新 更多