Skip to content

Instantly share code, notes, and snippets.

@jonesbusy
Created November 21, 2020 13:51
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 jonesbusy/fc08279258dfb002f040959bc3a9f12f to your computer and use it in GitHub Desktop.
Save jonesbusy/fc08279258dfb002f040959bc3a9f12f to your computer and use it in GitHub Desktop.
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
if (latestBuild != null) {
def actions = latestBuild.getActions(jenkins.scm.api.SCMRevisionAction.class)
if (actions) {
def revisionData = actions.first().revision
def factory = project.getProjectFactory()
def factoryRevision = factory.getRevision(workflow)
if (factoryRevision != null && revisionData.head == factoryRevision.head) {
println("Revision are the same between last build and project for '${workflow.fullDisplayName}'. Nothing to change")
}
// Need update
else {
factory.setRevisionHash(workflow, revisionData)
factory.setLastSeenRevisionHash(workflow, revisionData)
println("Successfully updated last revision to '${revisionData}' for '${workflow.fullDisplayName}'.")
}
}
else {
println("Unable to find any SCM action for '${workflow.fullDisplayName}'. Cannot determine last seen revision.")
}
}
else {
println("Unable to find latest build for '${workflow.fullDisplayName}'. Cannot determine last seen revision.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment