Skip to content

Instantly share code, notes, and snippets.

@Tharwat96
Last active November 9, 2020 09:50
Show Gist options
  • Save Tharwat96/19e0d2ec29458b9753708fbe4bc08b91 to your computer and use it in GitHub Desktop.
Save Tharwat96/19e0d2ec29458b9753708fbe4bc08b91 to your computer and use it in GitHub Desktop.
Jenkins Useful Groovy Scripts
import hudson.model.*
import hudson.security.*
import hudson.tasks.Mailer
def userId = args[0]
def password = args[1]
def email = args[2]
def instance = jenkins.model.Jenkins.instance
def existingUser = instance.securityRealm.allUsers.find {it.id == userId}
if (existingUser == null) {
def user = instance.securityRealm.createAccount(userId, password)
user.addProperty(new Mailer.UserProperty(email));
def strategy = (GlobalMatrixAuthorizationStrategy) instance.getAuthorizationStrategy()
strategy.add(Hudson.READ, userId)
instance.setAuthorizationStrategy(strategy)
instance.save()
}
println(hudson.util.Secret.decrypt("{XXX=}"))
for (aSlave in hudson.model.Hudson.instance.slaves) {
println('====================');
println('Name: ' + aSlave.name);
println('getLabelString: ' + aSlave.getLabelString());
println('getNumExectutors: ' + aSlave.getNumExecutors());
println('getRemoteFS: ' + aSlave.getRemoteFS());
println('getMode: ' + aSlave.getMode());
println('getRootPath: ' + aSlave.getRootPath());
println('getDescriptor: ' + aSlave.getDescriptor());
println('getComputer: ' + aSlave.getComputer());
println('\tcomputer.isAcceptingTasks: ' + aSlave.getComputer().isAcceptingTasks());
println('\tcomputer.isLaunchSupported: ' + aSlave.getComputer().isLaunchSupported());
println('\tcomputer.getConnectTime: ' + aSlave.getComputer().getConnectTime());
println('\tcomputer.getDemandStartMilliseconds: ' + aSlave.getComputer().getDemandStartMilliseconds());
println('\tcomputer.isOffline: ' + aSlave.getComputer().isOffline());
println('\tcomputer.countBusy: ' + aSlave.getComputer().countBusy());
//if (aSlave.name == 'NAME OF NODE TO DELETE') {
println('Shutting down node!!!!');
// aSlave.getComputer().setTemporarilyOffline(true,null);
aSlave.getComputer().doDoDelete();
//}
// println('\tcomputer.getLog: ' + aSlave.getComputer().getLog());
println('\tcomputer.getBuilds: ' + aSlave.getComputer().getBuilds());
}
import com.cloudbees.plugins.credentials.CredentialsProvider
import com.cloudbees.plugins.credentials.Credentials
import com.cloudbees.plugins.credentials.domains.Domain
import jenkins.model.Jenkins
def indent = { String text, int indentationCount ->
def replacement = "\t" * indentationCount
text.replaceAll("(?m)^", replacement)
}
Jenkins.get().allItems().collectMany{ CredentialsProvider.lookupStores(it).toList()}.unique().forEach { store ->
Map<Domain, List<Credentials>> domainCreds = [:]
store.domains.each { domainCreds.put(it, store.getCredentials(it))}
if (domainCreds.collectMany{ it.value}.empty) {
return
}
def shortenedClassName = store.getClass().name.substring(store.getClass().name.lastIndexOf(".") + 1)
println "Credentials for store context: ${store.contextDisplayName}, of type $shortenedClassName"
domainCreds.forEach { domain , creds ->
println indent("Domain: ${domain.name}", 1)
creds.each { cred ->
cred.properties.each { prop, val ->
println indent("$prop = \"$val\"", 2)
}
println indent("-----------------------", 2)
}
}
}
import hudson.model.*
def q = Jenkins.instance.queue
q.items.each { q.cancel(it.task) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment