Jenkins – Active Choice: Harbor – Images tag

 

For a parameterized assembly with an image tag selection, you will need the Active Choices plugin

Go to “Manage Jenkins

 

Section “Manage Plugins

 

Go to the “Available” tab and select “Active Choices” in the search.

Install it.

Create a “New Item” – “Pipeline“, indicate that it will be a parameterized assembly, and add the parameter “Active Choices Reactive Parameter

 

We indicate that this is “Groovy Script” and paste the following into it:

import jenkins.model.*
import groovy.json.JsonSlurper

credentialsId = 'harbor_credentials'
harborRepo = 'https://harbor.artem.services/api/repositories/artem/site'

def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
  com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, Jenkins.instance, null, null ).find{
    it.id == credentialsId}

def addr = "${harborRepo}/tags"
def authString = "${creds.username}:${creds.password}".getBytes().encodeBase64().toString()
def conn = addr.toURL().openConnection()
conn.setRequestProperty( "Authorization", "Basic ${authString}" )
def response_json = "${conn.content.text}"
def slurper = new JsonSlurper()
def parsed = slurper.parseText(response_json)

def tags=[]

for (tag in parsed){
    tags.add(tag.name);
}

return tags

 

Where the value of variables, “credentialsId” – Jenkins Credentials ID with login and password to the Harbor;

harborRepo” – the full path to the desired repository;

 

 

The same thing, but already in Pipeline

Pipeline:

properties([
  parameters([
    [$class: 'CascadeChoiceParameter', 
      choiceType: 'PT_SINGLE_SELECT', 
      description: 'Select Image',
      filterLength: 1,
      filterable: false,
      name: 'ImageTag', 
      script: [
        $class: 'GroovyScript', 
        script: [
          classpath: [], 
          sandbox: false, 
          script: 
            '''
            import jenkins.model.*
            import groovy.json.JsonSlurper

            credentialsId = 'harbor_credentials'
            harborRepo = 'https://harbor.artem.services/api/repositories/artem/site'

            def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
              com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, Jenkins.instance, null, null ).find{
                it.id == credentialsId}

            def addr = "${harborRepo}/tags"
            def authString = "${creds.username}:${creds.password}".getBytes().encodeBase64().toString()
            def conn = addr.toURL().openConnection()
            conn.setRequestProperty( "Authorization", "Basic ${authString}" )
            def response_json = "${conn.content.text}"
            def slurper = new JsonSlurper()
            def parsed = slurper.parseText(response_json)

            def tags=[]

            for (tag in parsed){
                tags.add(tag.name);
            }

            return tags
            '''
        ]
      ]
    ]
  ])
])

Tagged: Tags

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments