Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ivan-pinatti/7f7af9efe655d26a41f3292174adb2ea to your computer and use it in GitHub Desktop.
Save ivan-pinatti/7f7af9efe655d26a41f3292174adb2ea to your computer and use it in GitHub Desktop.
Jenkins - Set Digitalocean slaves plugin parameters via groovy script - #jenkins #groovy #jenkins-digitalocean #jenkins-slaves #jenkins-slave
#!groovy
// imports
import com.dubture.jenkins.digitalocean.DigitalOceanCloud
import com.dubture.jenkins.digitalocean.SlaveTemplate
import jenkins.model.Jenkins
// parameters
def slaveTemplateParameters = [
idleTerminationInMinutes: '10',
imageId: 'ubuntu-17-10-x64',
initScript: '',
installMonitoring: false,
instanceCap: '2',
labellessJobsAllowed: false,
labelString: 'digitalocean.toronto.ubuntu.17.10',
name: 'digitalocean.toronto.ubuntu.17.10',
numExecutors: '1',
regionId: 'tor1',
sizeId: '512mb',
sshPort: 22,
tags: '',
userData: '',
username: 'root',
workspacePath: '/home/jenkins'
]
def cloudParameters = [
authToken: '01234567890123456789012345678901234567890123456789',
connectionRetryWait: '30',
instanceCap: '2',
name: 'mycompany',
sshKeyId: '01234567',
timeoutMinutes: '10',
usePrivateNetworking: false,
privateKey: '''-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgGuoiHtwl8T2cKfclsWLOcv8S6p74iOAQX1kwCLvLy7ioDFlNzsI
U235N1StnZYZIwGla+3Uo3jMSUuWkMH85+d3KoRFPS+6RJCiAvMI0hr8FByes22v
DAVDnhkZ2SFOeh1SPxWDygPo2fW5sgqL2eYLO1CplDdqYhHLAL1FDV5tAgMBAAEC
gYBWRZoJgXK9zdb9TZIs/6LzSlzAY8IWPOM+PwyRcibXZZiFvNyDm+pviHTEkNRl
wgMBgLR6xBmz5dEel6utKKQVEPtD1m6N+z6hwUw+Nis35DCvmBX+hQSK+atGgjYH
ZKz0oqWUSuzHG+CxxcrePDTYJ4fdSyLPsQqaWoCZseDDwQJBANLey9r+juBEQe2N
MJoZTU1q/AoS5kY7OWQ1aF495I9fz87u9vx8BJh8djvmABwidUWREnd4vwwEIS3M
JtFGn+kCQQCCsvBvOXgVAlcR54/6ro6R42/0F3bZw0ZFVXvgRRjCZW6m4FyHq4AL
+gfAV0HERkMdlO1zBpBwkSURekDc9NvlAkAA3zj6k9jlZoLbR50u1fHy4wFdzUw0
eCQ5nNrsoNbkHOJQGb7dtmmSc9lNUBsqAp53hi0MX2xy0UWN2e1DKkaZAkBi9stH
7OQYRGVZkVVcI8Cghu7GjN3ZlhsndMsPzkIpMFTQ1yI5OIsEhpZH9co+rFU1mQcT
Ce1kzwKacU+b/2xhAkEAovqzUMFB9YEbc8C9AzTej5F2ttyuKBDJJ+kvQeJP+PnW
4ovFI4Ee5UmTWI6k/Md9BM+MvEMWs3nPoF4MULHqNg==
-----END RSA PRIVATE KEY-----'''
]
// https://github.com/jenkinsci/digitalocean-plugin/blob/digitalocean-plugin-0.17/src/main/java/com/dubture/jenkins/digitalocean/SlaveTemplate.java
SlaveTemplate slaveTemplate = new SlaveTemplate(
slaveTemplateParameters.name,
slaveTemplateParameters.imageId,
slaveTemplateParameters.sizeId,
slaveTemplateParameters.regionId,
slaveTemplateParameters.username,
slaveTemplateParameters.workspacePath,
slaveTemplateParameters.sshPort,
slaveTemplateParameters.idleTerminationInMinutes,
slaveTemplateParameters.numExecutors,
slaveTemplateParameters.labelString,
slaveTemplateParameters.labellessJobsAllowed,
slaveTemplateParameters.instanceCap,
slaveTemplateParameters.installMonitoring,
slaveTemplateParameters.tags,
slaveTemplateParameters.userData,
slaveTemplateParameters.initScript
)
// https://github.com/jenkinsci/digitalocean-plugin/blob/digitalocean-plugin-0.17/src/main/java/com/dubture/jenkins/digitalocean/DigitalOceanCloud.java
DigitalOceanCloud digitalOceanCloud = new DigitalOceanCloud(
cloudParameters.name,
cloudParameters.authToken,
cloudParameters.privateKey,
cloudParameters.sshKeyId,
cloudParameters.instanceCap,
cloudParameters.usePrivateNetworking,
cloudParameters.timeoutMinutes,
cloudParameters.connectionRetryWait,
[slaveTemplate]
)
// get Jenkins instance
Jenkins jenkins = Jenkins.getInstance()
// add cloud configuration to Jenkins
jenkins.clouds.add(digitalOceanCloud)
// save current Jenkins state to disk
jenkins.save()
@toondaey
Copy link

toondaey commented Oct 10, 2018

Hi @ivan-pinatti,
I'm kind of late to the party, but could you please walk me through how to use this script.
Thanks.

@ivan-pinatti
Copy link
Author

Hi @toondaey,

You have to change the values between the lines 8-51 to your own and then you use it in the Script Console to configure the Digital Ocean cloud on your Jenkins.

Let me know if I can assist you further.

Cheers!

https://gist.github.com/ivan-pinatti/7f7af9efe655d26a41f3292174adb2ea#file-jenkins-set-digitalocean-plugin-parameters-groovy-L8-L51

Hi @ivan-pinatti,
I'm kind of late to the party, but could you please walk me through how to use this script.
Thanks.

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