0

I've been trying to get a solution for a docker image tag drop down parameter in a Jenkinsfile for a deploy pipeline. So basically, pipeline as code, with choose your own image tag to deploy this project. In my case I'm trying to use the Declarative style. But I'm not against scripted if that is my only choice. I just want the job described in code and not freestyle. I feel like I'm close, but I have not seem to have had success yet. Here are some of my examples of my attempts.

I don't think it matters, but in case it does. This is the latest Jenkins, I just pulled in the latest patch today. This is also running in kubernetes and runs on the agents that get spun up from the kubernetes plugin.

properties([
  parameters([
    [$class: 'ChoiceParameter',
      choiceType: 'PT_SINGLE_SELECT',
      description: 'Image Tag To Deploy',
      filterable: true,
      name: 'TAG',
      script: [
        $class: 'GroovyScript',
        script: [
          classpath: [],
          sandbox: false,
          script:
            """
            def sout = new StringBuffer(), serr = new StringBuffer()
            def ecrLogin = 'aws ecr get-login-password --region us-gov-west-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-gov-west-1.amazonaws.com'.execute()
            def proc = 'aws ecr list-images --repository-name elasticsearch --region us-gov-west-1 --output json | jq -r \'.imageIds[].imageTag\' | grep -v null | sort -V'.execute()

            proc.consumeProcessOutput(sout, serr)
            proc.waitForOrKill(10000)

            return proc.text.readLines()
            """
        ]
      ]
    ]
  ])
])

pipeline {
  agent any
  stages() {
    steps {
      script {
        sh('echo "${params.TAG}"')
      }
    }
  }
}

I think most of this is probably right. But the final part of outputting the returned tags. Without repeating the entire code block, here are some other attempts in just the last part.

results = "${proc.in.text}".eachLine { line ->
  tags << line
}
return tags

and

def tags = stdout.tokenize().join('\n')
def results = []
tags.each { tag ->
  results.add("${tag}")
}

return results

even tried

def getTags() {
  def sout = new StringBuffer(), serr = new StringBuffer()
  def ecrLogin = 'aws ecr get-login-password --region us-gov-west-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-gov-west-1.amazonaws.com'.execute()
  def proc = 'aws ecr list-images --repository-name elasticsearch --region us-gov-west-1 --output json | jq -r \'.imageIds[].imageTag\' | grep -v null | sort -V'.execute()

  proc.consumeProcessOutput(sout, serr)
  proc.waitForOrKill(10000)

  def tags = sout.tokenize().join('\n')
  def results = []
  tags.each { tag ->
    results.add("${tag}")
  }

  return results
}

properties([
  parameters([
    [$class: 'ChoiceParameterDefinition',
      name: 'TAG',
      description: 'Image Tag To Deploy',
      choices: getTags()
    ]
  ])
])

I've looked into this plugin. But I couldn't seem to get it to work. Also, we are using gov cloud which may have been part of the issue. Any thoughts on what I might be doing wrong, or what I'm missing? Any alternative approaches you might suggest? Other plugins which might save me from scripting this out myself?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.