Search in sources :

Example 11 with Run

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;
}
Also used : Jenkins(jenkins.model.Jenkins) PromotedProjectAction(hudson.plugins.promoted_builds.PromotedProjectAction) ArrayList(java.util.ArrayList) Run(hudson.model.Run) PromotedBuildAction(hudson.plugins.promoted_builds.PromotedBuildAction) Nonnull(javax.annotation.Nonnull)

Example 12 with Run

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;
}
Also used : PrintStream(java.io.PrintStream) Action(hudson.model.Action) ArrayList(java.util.ArrayList) UpstreamCause(hudson.model.Cause.UpstreamCause) DependencyGraph(hudson.model.DependencyGraph) Run(hudson.model.Run) Dependency(hudson.model.DependencyGraph.Dependency) AbstractProject(hudson.model.AbstractProject)

Example 13 with Run

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);
    }
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Run(hudson.model.Run)

Example 14 with Run

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);
    }
}
Also used : Run(hudson.model.Run) IOException(java.io.IOException) Job(hudson.model.Job) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 15 with Run

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;
}
Also used : Binding(groovy.lang.Binding) Item(hudson.model.Item) Run(hudson.model.Run) AbstractProject(hudson.model.AbstractProject) GroovyShell(groovy.lang.GroovyShell) PrintWriter(java.io.PrintWriter)

Aggregations

Run (hudson.model.Run)26 FreeStyleProject (hudson.model.FreeStyleProject)5 Job (hudson.model.Job)5 Test (org.junit.Test)5 Item (hudson.model.Item)4 Shell (hudson.tasks.Shell)4 BlueRun (io.jenkins.blueocean.rest.model.BlueRun)4 Map (java.util.Map)4 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)4 ArtifactArchiver (hudson.tasks.ArtifactArchiver)3 ArrayList (java.util.ArrayList)3 AbstractProject (hudson.model.AbstractProject)2 ViolationsReport (hudson.plugins.violations.ViolationsReport)2 DataSetBuilder (hudson.util.DataSetBuilder)2 IOException (java.io.IOException)2 Nonnull (javax.annotation.Nonnull)2 MultiBranchProject (jenkins.branch.MultiBranchProject)2 Jenkins (jenkins.model.Jenkins)2 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)2 Ancestor (org.kohsuke.stapler.Ancestor)2