Last active
December 5, 2023 13:33
-
-
Save fdesouza-ssense/b60c48de9e598e3778263c2ec3cd0476 to your computer and use it in GitHub Desktop.
Sample Groovy script for Jenkins cloud nodes configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hudson.plugins.sshslaves.SSHConnector | |
import hudson.plugins.sshslaves.verifiers.NonVerifyingKeyVerificationStrategy | |
import hudson.model.* | |
import com.amazon.jenkins.ec2fleet.FleetCloud | |
import jenkins.model.Jenkins | |
configs = [[ | |
name : "jenkins-agent-1", | |
region : "us-east-1", | |
asgName : "asg-1", | |
label : "fleet-1", | |
fsRoot : "/tmp/jenkins-home", | |
sshCredId : "my-ssh-credential-id", | |
privateIpUsed : true, | |
alwaysReconnect : true, | |
idleMinutes : 20, | |
minSize : 0, | |
maxSize : 50, | |
minSpareSize : 0, | |
numExecutors : 1, | |
restrictUsage : false, | |
maxTotalUses : "-1", | |
disableTaskResubmit : false, | |
initOnlineTimeoutSec : 180, | |
initOnlineCheckIntervalSec: 15, | |
scaleExecutorsByWeight : false, | |
cloudStatusIntervalSec : 30, | |
], [ | |
name : "jenkins-agent-2", | |
region : "us-east-1", | |
asgName : "asg-2", | |
label : "fleet-2", | |
fsRoot : "/tmp/jenkins-home", | |
sshCredId : "my-ssh-credential-id", | |
privateIpUsed : true, | |
alwaysReconnect : true, | |
idleMinutes : 20, | |
minSize : 0, | |
maxSize : 10, | |
minSpareSize : 0, | |
numExecutors : 1, | |
restrictUsage : true, | |
maxTotalUses : "1", | |
disableTaskResubmit : false, | |
initOnlineTimeoutSec : 180, | |
initOnlineCheckIntervalSec: 15, | |
scaleExecutorsByWeight : false, | |
cloudStatusIntervalSec : 10, | |
] | |
] | |
// get Jenkins instance | |
Jenkins jenkins = Jenkins.get() | |
configs.each { | |
config -> | |
FleetCloud fleetCloud = new FleetCloud( | |
config.name, // fleetCloudName | |
null, // awsCredentialsId deprecated | |
null, // credentialsId | |
config.region, // aws region | |
"", // aws endpoint | |
config.asgName, // fleet id or asg name | |
config.label, // labels | |
config.fsRoot, // fs root or jenkins home | |
new SSHConnector(22, | |
config.sshCredId, "", "", "", "", null, 0, 0, | |
new NonVerifyingKeyVerificationStrategy()), // ssh connection information | |
config.privateIpUsed, // if need to use privateIpUsed | |
config.alwaysReconnect, // if need alwaysReconnect | |
config.idleMinutes, // if need to allow downscale set > 0 in min | |
config.minSize, // minSize | |
config.maxSize, // maxSize | |
config.minSpareSize, // minSpareSize | |
config.numExecutors, // numExecutors | |
false, // addNodeOnlyIfRunning | |
config.restrictUsage, // restrictUsage allow execute only jobs with proper label | |
config.maxTotalUses, // maxTotalUses Maximum Total Uses (-1 for unlimited) | |
config.disableTaskResubmit, // disableTaskResubmit if false plugin will resubmit the job if it failed due an instance termination. | |
config.initOnlineTimeoutSec, // initOnlineTimeoutSec Maximum Init Connection Timeout in sec | |
config.initOnlineCheckIntervalSec, // initOnlineCheckIntervalSec | |
config.scaleExecutorsByWeight, // scaleExecutorsByWeight | |
config.cloudStatusIntervalSec,// cloudStatusIntervalSec Cloud Status Interval in sec | |
true // noDelayProvision | |
) | |
// add cloud configuration to Jenkins | |
jenkins.clouds.add(fleetCloud) | |
// save current Jenkins state to disk | |
jenkins.save() | |
} | |
jenkins.clouds.each { | |
println(it.name) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment