Skip to content

Instantly share code, notes, and snippets.

@imoutsatsos
Created February 14, 2023 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imoutsatsos/2155ff8cbe5184a3e6f391fbc82c0e32 to your computer and use it in GitHub Desktop.
Save imoutsatsos/2155ff8cbe5184a3e6f391fbc82c0e32 to your computer and use it in GitHub Desktop.
/*
this function was suggested by ChatGPT AI
This function takes three parameters: the name of the Jenkins job to modify, the name of the existing parameter to rename, and the new name for the parameter.
It first retrieves the job object from the Jenkins instance, then looks for the ParametersDefinitionProperty for the job. If it doesn't find any parameters, or the specified parameter does not exist, it prints an error message and exits.
If the specified parameter exists, it creates a new parameter object with the new name and replaces the old parameter with the new one. Finally, it saves the job configuration to persist the changes.
You can call this function in a Jenkins pipeline or directly in the Jenkins script console.
*/
import jenkins.model.*
def renameJobParameter(String jobName, String oldParameterName, String newParameterName) {
def job = Jenkins.instance.getItem(jobName)
if (job == null) {
println("Job not found: $jobName")
return
}
def parameters = job.getProperty(ParametersDefinitionProperty.class)
if (parameters == null) {
println("No parameters found for job: $jobName")
return
}
def oldParam = parameters.getParameterDefinition(oldParameterName)
if (oldParam == null) {
println("Parameter not found: $oldParameterName")
return
}
def newParam = oldParam.clone()
newParam.setName(newParameterName)
parameters.getParameterDefinitions().remove(oldParam)
parameters.getParameterDefinitions().add(newParam)
job.save()
println("Parameter '$oldParameterName' renamed to '$newParameterName' in job '$jobName'")
}
@imoutsatsos
Copy link
Author

// Alternate BING Suggestion
// Get the Jenkins instance
def jenkins = Jenkins.instance

// Get the job name
def jobName = "my-freestyle-job"

// Get the job object
def job = jenkins.getItem(jobName)

// Check if the job exists and is a freestyle project
if (job && job instanceof hudson.model.FreeStyleProject) {
// Get the current config.xml as a string
def configXml = job.getConfigFile().asString()

// Define the old and new parameter names
def oldParam = "old-parameter-name"
def newParam = "new-parameter-name"

// Replace the old parameter name with the new one in the config
def newConfigXml = configXml.replaceAll(oldParam, newParam)

// Create a new input stream from the modified config
def newConfigStream = new ByteArrayInputStream(newConfigXml.getBytes())

// Update the job with the new config
job.updateByXml(newConfigStream)

// Save the changes
job.save()

// Print a success message
println("The parameter $oldParam has been renamed to $newParam in $jobName")
} else {
// Print an error message
println("The job $jobName does not exist or is not a freestyle project")
}

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