Skip to content

Instantly share code, notes, and snippets.

@xremoteuser
Last active April 19, 2024 07:39
Show Gist options
  • Save xremoteuser/4efaf6a3926ec28fc4db9d3ea7216734 to your computer and use it in GitHub Desktop.
Save xremoteuser/4efaf6a3926ec28fc4db9d3ea7216734 to your computer and use it in GitHub Desktop.
Jenkins groovy Active Choices plugin show a list of files in Minio bucket
properties([
parameters([
[$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: 'Select an available file:',
filterLength: 1,
filterable: false,
name: 'FILE',
script: [
$class: 'GroovyScript',
fallbackScript: [
classpath: [],
sandbox: false,
script: ""
],
script: [
classpath: [],
sandbox: false,
script: '''
//List files in Minio bucket
import io.minio.MinioClient
import io.minio.ListObjectsArgs
import io.minio.Result
import io.minio.messages.Item
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
def s3_endpoint = 'https://s3.minio.local'
def s3_bucketName = 'backups'
def s3_access_key_raw = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.common.StandardCredentials.class, Jenkins.instance, null, null
).find {
it.id == "s3-access-key" } //secret text credential ID here
def s3_secret_key_raw = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.common.StandardCredentials.class, Jenkins.instance, null, null
).find {
it.id == "s3-secret-key" } //agother secret text credential ID here
def s3_access_key = s3_access_key_raw ? s3_access_key_raw.getSecret().getPlainText() : null
def s3_secret_key = s3_secret_key_raw ? s3_secret_key_raw.getSecret().getPlainText() : null
def objectNames = []
try {
def minioClient = MinioClient.builder()
.endpoint(s3_endpoint)
.credentials(s3_access_key.toString(), s3_secret_key.toString())
.build()
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket(s3_bucketName).build() )
results.each { Result<Item> result ->
Item item = result.get()
objectNames.add(item.objectName()) }
} catch (Exception e) {
println("Error occurred: ${e.message}") }
return objectNames
'''
]
]
]
])
])
pipeline {
agent any
stages {
stage('Debug') {
steps {
script {
echo "Selected file is: ${params.FILE}"
}
}
}
}
}
@xremoteuser
Copy link
Author

xremoteuser commented Apr 19, 2024

Requires:
Minio Plugin 1.3.3-rc93.9e92f846d4cf
Active Choices Plug-in 2.8.3
Credentials Plugin 1337.v60b_d7b_c7b_c9f
Credentials Binding Plugin 657.v2b_19db_7d6e6d
Plain Credentials Plugin 179.vc5cb_98f6db_38

Note that for Active Choices scripts to work, you need to manually approve them in the Configure tab. Otherwise, the Active Choices interactive fields will not show any results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment