Search in sources :

Example 16 with Jenkins

use of jenkins.model.Jenkins in project gogs-webhook-plugin by jenkinsci.

the class GogsUtils method find.

/**
 * Search in Jenkins for a item with type T based on the job name
 * @param jobName job to find, for jobs inside a folder use : {@literal <folder>/<folder>/<jobName>}
 * @return the Job matching the given name, or {@code null} when not found
 */
static <T extends Item> T find(String jobName, Class<T> type) {
    Jenkins jenkins = Jenkins.getActiveInstance();
    // direct search, can be used to find folder based items <folder>/<folder>/<jobName>
    T item = jenkins.getItemByFullName(jobName, type);
    if (item == null) {
        // (to keep it backwards compatible)
        for (T allItem : jenkins.getAllItems(type)) {
            if (allItem.getName().equals(jobName)) {
                item = allItem;
                break;
            }
        }
    }
    return item;
}
Also used : Jenkins(jenkins.model.Jenkins)

Example 17 with Jenkins

use of jenkins.model.Jenkins in project workflow-job-plugin by jenkinsci.

the class WorkflowJob method poll.

@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", justification = "TODO 1.653+ switch to Jenkins.getInstanceOrNull")
@Override
public PollingResult poll(TaskListener listener) {
    if (!isBuildable()) {
        listener.getLogger().println("Build disabled");
        return PollingResult.NO_CHANGES;
    }
    // TODO 2.11+ call SCMDecisionHandler
    // TODO call SCMPollListener
    WorkflowRun lastBuild = getLastBuild();
    if (lastBuild == null) {
        listener.getLogger().println("no previous build to compare to");
        // Note that we have no equivalent of AbstractProject.NoSCM because without an initial build we do not know if this project has any SCM at all.
        return Queue.getInstance().contains(this) ? PollingResult.NO_CHANGES : PollingResult.BUILD_NOW;
    }
    WorkflowRun perhapsCompleteBuild = getLastSuccessfulBuild();
    if (perhapsCompleteBuild == null) {
        perhapsCompleteBuild = lastBuild;
    }
    if (pollingBaselines == null) {
        pollingBaselines = new ConcurrentHashMap<>();
    }
    PollingResult result = PollingResult.NO_CHANGES;
    for (WorkflowRun.SCMCheckout co : perhapsCompleteBuild.checkouts(listener)) {
        if (!co.scm.supportsPolling()) {
            listener.getLogger().println("polling not supported from " + co.workspace + " on " + co.node);
            continue;
        }
        String key = co.scm.getKey();
        SCMRevisionState pollingBaseline = pollingBaselines.get(key);
        if (pollingBaseline == null) {
            // after a restart, transient cache will be empty
            pollingBaseline = co.pollingBaseline;
        }
        if (pollingBaseline == null) {
            listener.getLogger().println("no polling baseline in " + co.workspace + " on " + co.node);
            continue;
        }
        try {
            FilePath workspace;
            Launcher launcher;
            WorkspaceList.Lease lease;
            if (co.scm.requiresWorkspaceForPolling()) {
                Jenkins j = Jenkins.getInstance();
                if (j == null) {
                    listener.error("Jenkins is shutting down");
                    continue;
                }
                Computer c = j.getComputer(co.node);
                if (c == null) {
                    listener.error("no such computer " + co.node);
                    continue;
                }
                workspace = new FilePath(c.getChannel(), co.workspace);
                launcher = workspace.createLauncher(listener).decorateByEnv(getEnvironment(c.getNode(), listener));
                lease = c.getWorkspaceList().acquire(workspace, !isConcurrentBuild());
            } else {
                workspace = null;
                launcher = null;
                lease = null;
            }
            PollingResult r;
            try {
                r = co.scm.compareRemoteRevisionWith(this, launcher, workspace, listener, pollingBaseline);
                if (r.remote != null) {
                    pollingBaselines.put(key, r.remote);
                }
            } finally {
                if (lease != null) {
                    lease.release();
                }
            }
            if (r.change.compareTo(result.change) > 0) {
                // note that if we are using >1 checkout, we can clobber baseline/remote here; anyway SCMTrigger only calls hasChanges()
                result = r;
            }
        } catch (AbortException x) {
            listener.error("polling failed in " + co.workspace + " on " + co.node + ": " + x.getMessage());
        } catch (Exception x) {
            // TODO 2.43+ use Functions.printStackTrace
            listener.error("polling failed in " + co.workspace + " on " + co.node).println(Functions.printThrowable(x).trim());
        }
    }
    return result;
}
Also used : FilePath(hudson.FilePath) WorkspaceList(hudson.slaves.WorkspaceList) ServletException(javax.servlet.ServletException) AbortException(hudson.AbortException) IOException(java.io.IOException) Jenkins(jenkins.model.Jenkins) SCMRevisionState(hudson.scm.SCMRevisionState) PollingResult(hudson.scm.PollingResult) Computer(hudson.model.Computer) Launcher(hudson.Launcher) AbortException(hudson.AbortException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 18 with Jenkins

use of jenkins.model.Jenkins in project workflow-job-plugin by jenkinsci.

the class WorkflowRun method getEnvironment.

@Override
public EnvVars getEnvironment(TaskListener listener) throws IOException, InterruptedException {
    EnvVars env = super.getEnvironment(listener);
    Jenkins instance = Jenkins.getInstance();
    if (instance != null) {
        for (NodeProperty nodeProperty : instance.getGlobalNodeProperties()) {
            nodeProperty.buildEnvVars(env, listener);
        }
    }
    // TODO EnvironmentContributingAction does not support Job yet:
    ParametersAction a = getAction(ParametersAction.class);
    if (a != null) {
        for (ParameterValue v : a) {
            v.buildEnvironment(this, env);
        }
    }
    EnvVars.resolve(env);
    return env;
}
Also used : Jenkins(jenkins.model.Jenkins) EnvVars(hudson.EnvVars) ParameterValue(hudson.model.ParameterValue) NodeProperty(hudson.slaves.NodeProperty) ParametersAction(hudson.model.ParametersAction)

Example 19 with Jenkins

use of jenkins.model.Jenkins in project gitea-plugin by jenkinsci.

the class DefaultGiteaConnection method openConnection.

private static HttpURLConnection openConnection(UriTemplate template) throws IOException {
    URL url = new URL(template.expand());
    Jenkins jenkins = Jenkins.getInstance();
    if (jenkins == null || jenkins.proxy == null) {
        return (HttpURLConnection) url.openConnection();
    }
    return (HttpURLConnection) url.openConnection(jenkins.proxy.createProxy(url.getHost()));
}
Also used : Jenkins(jenkins.model.Jenkins) HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL)

Example 20 with Jenkins

use of jenkins.model.Jenkins in project promoted-builds-plugin by jenkinsci.

the class GroovyCondition method isMet.

@Override
public PromotionBadge isMet(final PromotionProcess promotionProcess, final AbstractBuild<?, ?> build) {
    final Jenkins jenkins = Jenkins.get();
    final PluginManager pluginManager = jenkins.getPluginManager();
    final ClassLoader classLoader = pluginManager.uberClassLoader;
    final Binding binding = new Binding();
    binding.setVariable("promotionProcess", promotionProcess);
    binding.setVariable("build", build);
    binding.setVariable("jenkins", jenkins);
    Object result = null;
    try {
        result = script.evaluate(classLoader, binding);
    } catch (final RejectedAccessException e) {
        LOGGER.log(Level.WARNING, "Sandbox exception", e);
        return null;
    } catch (final UnapprovedUsageException e) {
        LOGGER.log(Level.WARNING, "Unapproved script", e);
        return null;
    } catch (final UnapprovedClasspathException e) {
        LOGGER.log(Level.WARNING, "Unapproved classpath", e);
        return null;
    } catch (final Exception e) {
        LOGGER.log(Level.WARNING, "Evaluation error", e);
        return null;
    }
    final String displayLabel = metQualificationLabel == null ? Messages.GroovyCondition_MetQualificationLabel() : metQualificationLabel;
    if (Boolean.TRUE.equals(result)) {
        return new Badge(displayLabel, Collections.<String, String>emptyMap());
    } else if (result instanceof Map && !((Map) result).isEmpty()) {
        final Map<String, String> variables = new HashMap<String, String>(((Map) result).size());
        for (final Map.Entry entry : ((Map<Object, Object>) result).entrySet()) {
            final Object key = entry.getKey();
            final Object value = entry.getValue();
            if (key == null) {
                continue;
            }
            variables.put(key.toString(), value == null ? "" : value.toString());
        }
        return new Badge(displayLabel, variables);
    }
    return null;
}
Also used : Binding(groovy.lang.Binding) RejectedAccessException(org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException) UnapprovedUsageException(org.jenkinsci.plugins.scriptsecurity.scripts.UnapprovedUsageException) UnapprovedClasspathException(org.jenkinsci.plugins.scriptsecurity.scripts.UnapprovedClasspathException) PromotionBadge(hudson.plugins.promoted_builds.PromotionBadge) UnapprovedClasspathException(org.jenkinsci.plugins.scriptsecurity.scripts.UnapprovedClasspathException) UnapprovedUsageException(org.jenkinsci.plugins.scriptsecurity.scripts.UnapprovedUsageException) RejectedAccessException(org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException) Jenkins(jenkins.model.Jenkins) PluginManager(hudson.PluginManager) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Jenkins (jenkins.model.Jenkins)73 Test (org.junit.Test)22 ConfiguredWithCode (org.jenkinsci.plugins.casc.misc.ConfiguredWithCode)13 IOException (java.io.IOException)10 File (java.io.File)9 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)9 FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)7 Statement (org.junit.runners.model.Statement)7 CheckForNull (javax.annotation.CheckForNull)6 FilePath (hudson.FilePath)5 Computer (hudson.model.Computer)5 URL (java.net.URL)5 FlowNode (org.jenkinsci.plugins.workflow.graph.FlowNode)5 Issue (org.jvnet.hudson.test.Issue)5 Item (hudson.model.Item)4 Node (hudson.model.Node)4 Date (java.util.Date)4