... keeping update ...
Last active
April 11, 2024 22:48
-
-
Save marslo/40aa9ca98ab8f55616b5f06fcbde58e0 to your computer and use it in GitHub Desktop.
Get/Update Jenkins GitSCM CredneitalId and Repo URL for all pipelines
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
#!/usr/bin/env groovy | |
import org.jenkinsci.plugins.workflow.job.WorkflowJob | |
jenkins.model.Jenkins.instance.getAllItems( WorkflowJob.class ).findAll{ | |
it.definition instanceof org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition | |
}.each { | |
println it.fullName.toString().padRight(30) + | |
( it.definition?.scm?.branches?.join() ?: '' ).padRight(20) + | |
( it?.definition?.scriptPath ?: '' ).padRight(30) + | |
it.definition?.scm?.userRemoteConfigs.collect { it.credentialsId }.join().padRight(30) + | |
it.definition?.scm?.repositories?.collect{ it.getURIs() }?.flatten()?.join() | |
} | |
"DONE" | |
// vim:tabstop=2:softtabstop=2:shiftwidth=2:expandtab:filetype=Groovy |
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
#!/usr/bin/env groovy | |
import hudson.plugins.git.GitSCM | |
import hudson.plugins.git.UserRemoteConfig | |
import org.jenkinsci.plugins.workflow.job.WorkflowJob | |
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition | |
import com.cloudbees.plugins.credentials.common.StandardCredentials | |
import com.cloudbees.plugins.credentials.CredentialsProvider | |
String newCredId = 'NEW_CREDENTIAL' | |
if ( CredentialsProvider.lookupCredentials( StandardCredentials.class, jenkins.model.Jenkins.instance) | |
.any { newCredId == it.id } | |
) { | |
jenkins.model.Jenkins.instance.getAllItems( WorkflowJob.class ).findAll { | |
it.definition instanceof org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition && | |
! it.definition?.scm?.userRemoteConfigs.collect { it.credentialsId }.contains( newCredId ) | |
}.each { job -> | |
GitSCM orgScm = job.definition?.scm | |
Boolean orgLightweight = job.definition?.lightweight | |
List<UserRemoteConfig> newUserRemoteConfigs = orgScm.userRemoteConfigs.collect { | |
newUrl = 'ssh://' + it.url.split('ssh://').last().split('@').last() | |
new UserRemoteConfig( newUrl, it.name, it.refspec, newCredId ) | |
} | |
GitSCM newScm = new GitSCM( newUserRemoteConfigs, orgScm.branches, orgScm.doGenerateSubmoduleConfigurations, | |
orgScm.submoduleCfg, orgScm.browser, orgScm.gitTool, orgScm.extensions | |
) | |
CpsScmFlowDefinition flowDefinition = new CpsScmFlowDefinition( newScm, job.definition.scriptPath ) | |
job.definition = flowDefinition | |
job.definition.lightweight = orgLightweight | |
job.save() | |
println ">> " + job.fullName + " DONE !" | |
} | |
} else { | |
println "${newCredId} CANNOT be found !!" | |
} | |
"DONE" | |
// vim:tabstop=2:softtabstop=2:shiftwidth=2:expandtab:filetype=Groovy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment