求Jenkins Extensible插件中获取S3桶文件列表的Groovy脚本(参数化用)
Fetch S3 Bucket File Names for Jenkins Parameters via Groovy Script
Got it, let's put together a Groovy script that works with Jenkins' Extensible plugin to pull file names from an S3 bucket and turn them into parameter options. Here's what you need:
Prerequisites
- First, ensure your Jenkins instance has the AWS Credentials Plugin installed (this lets us safely access AWS credentials without hardcoding them).
- Make sure the Jenkins agent (or master, if you're running jobs there) has IAM permissions to list objects in your target S3 bucket (specifically the
s3:ListBucketaction).
Groovy Script
import com.amazonaws.services.s3.AmazonS3ClientBuilder import com.amazonaws.auth.AWSCredentialsProvider import com.amazonaws.auth.AWSStaticCredentialsProvider import com.amazonaws.auth.BasicAWSCredentials import jenkins.model.Jenkins // Configuration - update these values to match your setup def bucketName = "your-target-s3-bucket" def awsRegion = "us-east-1" // e.g., eu-west-1, ap-southeast-1 def credentialId = "your-aws-credentials-id" // ID of your AWS credentials stored in Jenkins // Fetch AWS credentials from Jenkins' credential store def credentials = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].credentials def awsCreds = credentials.find { it.id == credentialId } if (!awsCreds) { throw new Exception("AWS credentials with ID '${credentialId}' not found in Jenkins!") } // Create AWS credentials provider AWSCredentialsProvider credsProvider = new AWSStaticCredentialsProvider( new BasicAWSCredentials(awsCreds.accessKey, awsCreds.secretKey) ) // Initialize S3 client def s3Client = AmazonS3ClientBuilder.standard() .withCredentials(credsProvider) .withRegion(awsRegion) .build() // List all objects in the bucket, filter out folder prefixes (if needed) def fileNames = [] s3Client.listObjects(bucketName).objectSummaries.each { obj -> // Skip "folders" (S3 doesn't have actual folders, just objects with trailing slashes) if (!obj.key.endsWith('/')) { fileNames.add(obj.key) } } // Return the list of file names to use as parameter options return fileNames
How to Use This in the Extensible Plugin
- When setting up your Jenkins parameter, select the option for the Extensible plugin (usually labeled something like "Extensible Choice Parameter").
- In the "Choice Provider" section, pick "Groovy Script".
- Paste the script above into the script input box.
- Adjust the configuration variables (
bucketName,awsRegion,credentialId) to match your AWS setup and target bucket. - Save your parameter configuration—when you run the job, it will dynamically fetch the file names from S3 and present them as selectable options.
Notes
- If your Jenkins is running on an EC2 instance with an IAM role attached (instead of using stored credentials), you can simplify the credential part by removing the credential fetching code and using
AmazonS3ClientBuilder.defaultClient()instead (it will auto-use the instance role). - If you only want files from a specific prefix (subfolder) in the bucket, modify the
listObjectscall tos3Client.listObjects(bucketName, "your-prefix/"). - Always test the script first in a non-production environment to ensure permissions and configurations are correct.
内容的提问来源于stack exchange,提问作者RITESH SANJAY MAHAJAN




