use of hudson.model.Run in project promoted-builds-plugin by jenkinsci.
the class PromotedBuildParameterDefinition method getRuns.
/**
* Gets a list of promoted builds for the project.
* @param base Base item for the relative addressing
* @return List of {@link AbstractBuild}s, which have been promoted.
* May return an empty list if {@link Jenkins} instance is not ready
* @since 2.22
*/
@Nonnull
public List<Run<?, ?>> getRuns(@CheckForNull Item base) {
final List<Run<?, ?>> runs = new ArrayList<Run<?, ?>>();
final Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
return runs;
}
// JENKINS-25011: also look for jobs in folders.
final AbstractProject<?, ?> job = ItemPathResolver.getByPath(projectName, base, AbstractProject.class);
if (job == null) {
return runs;
}
PromotedProjectAction promotedProjectAction = job.getAction(PromotedProjectAction.class);
if (promotedProjectAction == null) {
return runs;
}
for (Run<?, ?> run : job.getBuilds()) {
List<PromotedBuildAction> actions = run.getActions(PromotedBuildAction.class);
for (PromotedBuildAction buildAction : actions) {
if (buildAction.contains(promotionProcessName)) {
runs.add(run);
break;
}
}
}
return runs;
}
use of hudson.model.Run in project hudson-2.x by hudson.
the class BuildTrigger method execute.
/**
* Convenience method to trigger downstream builds.
*
* @param build
* The current build. Its downstreams will be triggered.
* @param listener
* Receives the progress report.
*/
public static boolean execute(AbstractBuild build, BuildListener listener) {
PrintStream logger = listener.getLogger();
// Check all downstream Project of the project, not just those defined by BuildTrigger
final DependencyGraph graph = Hudson.getInstance().getDependencyGraph();
List<Dependency> downstreamProjects = new ArrayList<Dependency>(graph.getDownstreamDependencies(build.getProject()));
// Sort topologically
Collections.sort(downstreamProjects, new Comparator<Dependency>() {
public int compare(Dependency lhs, Dependency rhs) {
// Swapping lhs/rhs to get reverse sort:
return graph.compare(rhs.getDownstreamProject(), lhs.getDownstreamProject());
}
});
for (Dependency dep : downstreamProjects) {
AbstractProject p = dep.getDownstreamProject();
if (p.isDisabled()) {
logger.println(Messages.BuildTrigger_Disabled(p.getName()));
continue;
}
List<Action> buildActions = new ArrayList<Action>();
if (dep.shouldTriggerBuild(build, listener, buildActions)) {
// this is not completely accurate, as a new build might be triggered
// between these calls
String name = p.getName() + " #" + p.getNextBuildNumber();
if (p.scheduleBuild(p.getQuietPeriod(), new UpstreamCause((Run) build), buildActions.toArray(new Action[buildActions.size()]))) {
logger.println(Messages.BuildTrigger_Triggering(name));
} else {
logger.println(Messages.BuildTrigger_InQueue(name));
}
}
}
return true;
}
use of hudson.model.Run in project hudson-2.x by hudson.
the class LogRotator method perform.
public void perform(Job<?, ?> job) throws IOException, InterruptedException {
LOGGER.log(FINE, "Running the log rotation for " + job.getFullDisplayName());
// keep the last successful build regardless of the status
Run lsb = job.getLastSuccessfulBuild();
Run lstb = job.getLastStableBuild();
List<? extends Run<?, ?>> builds = job.getBuilds();
Calendar cal = null;
//Delete builds
if (-1 != numToKeep || -1 != daysToKeep) {
if (-1 != daysToKeep) {
cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, -daysToKeep);
}
if (-1 != numToKeep) {
builds = builds.subList(Math.min(builds.size(), numToKeep), builds.size());
}
//Delete builds based on configured values. See http://issues.hudson-ci.org/browse/HUDSON-3650
deleteBuilds(builds, lsb, lstb, cal);
}
cal = null;
builds = job.getBuilds();
//Delete build artifacts
if (-1 != artifactNumToKeep || -1 != artifactDaysToKeep) {
if (-1 != artifactDaysToKeep) {
cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, -artifactDaysToKeep);
}
if (-1 != artifactNumToKeep) {
builds = builds.subList(Math.min(builds.size(), artifactNumToKeep), builds.size());
}
//Delete build artifacts based on configured values. See http://issues.hudson-ci.org/browse/HUDSON-3650
deleteBuildArtifacts(builds, lsb, lstb, cal);
}
}
use of hudson.model.Run in project hudson-2.x by hudson.
the class CommandDuringBuild method getCurrentlyBuilding.
/**
* This method makes sense only when called from within the build kicked by Hudson.
* We use the environment variables that Hudson sets to determine the build that is being run.
*/
protected Run getCurrentlyBuilding() throws CmdLineException {
try {
CLICommand c = CLICommand.getCurrent();
if (c == null)
throw new IllegalStateException("Not executing a CLI command");
String[] envs = c.channel.call(new GetCharacteristicEnvironmentVariables());
if (envs[0] == null || envs[1] == null)
throw new CmdLineException("This CLI command works only when invoked from inside a build");
Job j = Hudson.getInstance().getItemByFullName(envs[0], Job.class);
if (j == null)
throw new CmdLineException("No such job: " + envs[0]);
try {
Run r = j.getBuildByNumber(Integer.parseInt(envs[1]));
if (r == null)
throw new CmdLineException("No such build #" + envs[1] + " in " + envs[0]);
return r;
} catch (NumberFormatException e) {
throw new CmdLineException("Invalid build number: " + envs[1]);
}
} catch (IOException e) {
throw new CmdLineException("Failed to identify the build being executed", e);
} catch (InterruptedException e) {
throw new CmdLineException("Failed to identify the build being executed", e);
}
}
use of hudson.model.Run in project hudson-2.x by hudson.
the class GroovyCommand method run.
protected int run() throws Exception {
// this allows the caller to manipulate the JVM state, so require the admin privilege.
Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
Binding binding = new Binding();
binding.setProperty("out", new PrintWriter(stdout, true));
String j = getClientEnvironmentVariable("JOB_NAME");
if (j != null) {
Item job = Hudson.getInstance().getItemByFullName(j);
binding.setProperty("currentJob", job);
String b = getClientEnvironmentVariable("BUILD_NUMBER");
if (b != null && job instanceof AbstractProject) {
Run r = ((AbstractProject) job).getBuildByNumber(Integer.parseInt(b));
binding.setProperty("currentBuild", r);
}
}
GroovyShell groovy = new GroovyShell(binding);
groovy.run(loadScript(), "RemoteClass", remaining.toArray(new String[remaining.size()]));
return 0;
}
Aggregations