Created
November 21, 2020 13:51
-
-
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
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
// 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