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;
}
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;
}
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;
}
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()));
}
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;
}
Aggregations