Skip to content

Instantly share code, notes, and snippets.

View jonesbusy's full-sized avatar
😸
Working from home

Valentin Delaye jonesbusy

😸
Working from home
  • @jenkinsci contributor
  • Lausanne, Switzerland
  • 14:59 (UTC +02:00)
View GitHub Profile
@jonesbusy
jonesbusy / jenkins_retrieve_string_token_credential.groovy
Last active March 14, 2022 09:51
jenkins_retrieve_credential.groovy
// Token/String
println(com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl.class, Jenkins.instance, null, null).findAll {it.id == id}[0].secret.value)
// SSH key
println(com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey.class, Jenkins.instance, null, null).findAll {it.id == id}[0].getPrivateKey())
// Username password
println(com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials.class, Jenkins.instance, null, null).findAll {it.id == id}[0].username)
println(com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials.class, Jenkins.instance, null, null).findAll {it.id == id}[0].password)
@jonesbusy
jonesbusy / nexus_ldap_system.groovy
Created April 7, 2021 19:16
nexus_ldap_system.groovy
import org.sonatype.nexus.capability.CapabilityReference
import org.sonatype.nexus.capability.CapabilityType
import org.sonatype.nexus.internal.capability.DefaultCapabilityReference
import org.sonatype.nexus.internal.capability.DefaultCapabilityRegistry
import org.sonatype.nexus.security.realm.RealmManager
// Remove proxy
core.removeHTTPProxy()
core.removeHTTPSProxy()
@jonesbusy
jonesbusy / nexus_cleanup_policies.groovy
Created April 7, 2021 19:14
nexus_cleanup_policies.groovy
import org.sonatype.nexus.cleanup.storage.CleanupPolicyStorage
import org.sonatype.nexus.cleanup.storage.CleanupPolicyComponent
def isCleanupPolicyExists(String name) {
def cleanupPolicyStorage = container.lookup(CleanupPolicyStorage.class.getName())
return cleanupPolicyStorage.exists(name)
}
def deleteCleanupPolicy(String name) {
def cleanupPolicyStorage = container.lookup(CleanupPolicyStorage.class.getName())
ZSH_THEME="flazz"
export EDITOR=vim
# Prompt config
CARETCOLOR=magenta
PROMPT='%{${fg[cyan]}%}%n@%m%{${fg_bold[magenta]}%}:%{$reset_color%}%3~%{$fg_bold[cyan]%}$(__git_ps1 " (%s)") %{${fg_bold[$CARETCOLOR]}%}$%{${reset_color}%} '
RPROMPT='%{$fg[white]%}[%?][%l][%*]%{$reset_color%}'
GIT_PS1_SHOWUPSTREAM=verbose
@jonesbusy
jonesbusy / check_vulnerable_plugins.groovy
Created November 25, 2020 17:35
Check if a Jenkins plugin or its dependencies are vulnerable
def static isPluginVulnerable(name) {
def plugins = [:]
// Push all plugins from all update site
jenkins.model.Jenkins.instance.updateCenter.sites.each {plugins += it.data.plugins}
def plugin = plugins[name]
// Ignore
@jonesbusy
jonesbusy / display_jenkins_plugins.groovy
Created November 25, 2020 17:32
Display jenkins plugin and their version
println(jenkins.model.Jenkins.instance.pluginManager.activePlugins.sort().collect { plugin -> "${plugin.shortName}:${plugin.version}" }.sort().join("\n"))
@jonesbusy
jonesbusy / jenkins_public_key.groovy
Created November 25, 2020 17:21
Jenkins dump instance public key
import jenkins.security.CryptoConfidentialKey
import java.security.KeyPair
import java.security.spec.X509EncodedKeySpec
import java.security.spec.PKCS8EncodedKeySpec
import org.bouncycastle.util.io.pem.PemObject
import org.bouncycastle.util.io.pem.PemWriter
import org.jenkinsci.main.modules.instance_identity.InstanceIdentity
import org.jenkinsci.main.modules.instance_identity.pem.PEMHelper
import org.apache.commons.io.FileUtils
@jonesbusy
jonesbusy / Jenkins_Align_all_branches_revisions.groovy
Created November 21, 2020 13:51
Align last seen revision for all workflow jobs. Useful when restoring build history and job are recreated from scratch
// Retrieve all workflows
def workflows = jenkins.model.Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob)
// Ensure to take only workflow having a parent project
workflows.findAll{ isProject(it.getParent()) }.each { workflow ->
def project = workflow.getParent()
def latestBuild = workflow.getLastBuild()
// Ensure we have a latest build
@jonesbusy
jonesbusy / Jenkins_Align_All_Build_Numbers.groovy
Created November 21, 2020 13:49
Ensure to align all next build number. Useful when restoring build history when job are recreated
jenkins.model.Jenkins.instance.allItems.findAll { (it instanceof hudson.model.Job) }.each { item ->
def nextBuildNumber = item.getNextBuildNumber()
def build = item.getLastBuild()
if (build != null) {
def lastBuildNumber = build.getNumber()
if (nextBuildNumber <= lastBuildNumber) {
println("Detected previous build number '${nextBuildNumber}' is smaller than last build '${lastBuildNumber}' for job '${item.displayName}'.")
nextBuildNumber = lastBuildNumber + 1
println("Performed update to '${nextBuildNumber}'")
item.updateNextBuildNumber(nextBuildNumber)
@jonesbusy
jonesbusy / Jenkins_rescan_multibranch_pipelines.groovy
Last active November 21, 2020 13:49
Force the Rescan of all multibranch pipelines and organisation folders
def scannableItems = jenkins.model.Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) +
jenkins.model.Jenkins.instance.getAllItems(jenkins.branch.OrganizationFolder)
scannableItems.collect { jenkins.model.Jenkins.getInstance().getQueue().schedule(it, 0, null, null)}.each { waitingItem ->
def future
def result
try {
future = waitingItem.getFuture()
result = future.get(timeout, TimeUnit.SECONDS)
println("${result} : ${result.getResult()}. Took ${result.getEstimatedDuration() / 1000.0} seconds")