Java Code Examples for jenkins.model.Jenkins#getActiveInstance()

The following examples show how to use jenkins.model.Jenkins#getActiveInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BuildWorker.java    From anchore-container-scanner-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Print versions info and configuration
 */
private void printConfig() {
  console.logInfo("Jenkins version: " + Jenkins.VERSION);
  List<PluginWrapper> plugins;
  if (Jenkins.getActiveInstance() != null && Jenkins.getActiveInstance().getPluginManager() != null
      && (plugins = Jenkins.getActiveInstance().getPluginManager().getPlugins()) != null) {
    for (PluginWrapper plugin : plugins) {
      if (plugin.getShortName()
          .equals("anchore-container-scanner")) { // artifact ID of the plugin, TODO is there a better way to get this
        console.logInfo(plugin.getDisplayName() + " version: " + plugin.getVersion());
        break;
      }
    }
  }
  config.print(console);
}
 
Example 2
Source File: GogsUtils.java    From gogs-webhook-plugin with MIT License 6 votes vote down vote up
/**
 * 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) {
		// not found in a direct search, search in all items since the item might be in a folder but given without folder structure
		// (to keep it backwards compatible)
		for (T allItem : jenkins.getAllItems(type)) {
			 if (allItem.getName().equals(jobName)) {
			 	item = allItem;
			 	break;
			 }
		}
	}
	return item;
}
 
Example 3
Source File: GitHubStatusNotificationStep.java    From pipeline-githubnotify-step-plugin with MIT License 5 votes vote down vote up
/**
 * Uses proxy if configured on pluginManager/advanced page
 *
 * @param host GitHub's hostname to build proxy to
 *
 * @return proxy to use it in connector. Should not be null as it can lead to unexpected behaviour
 */
@Nonnull
private static Proxy getProxy(@Nonnull String host) {
    Jenkins jenkins = Jenkins.getActiveInstance();

    if (jenkins.proxy == null) {
        return Proxy.NO_PROXY;
    } else {
        return jenkins.proxy.createProxy(host);
    }
}
 
Example 4
Source File: S3Profile.java    From jobcacher-plugin with MIT License 4 votes vote down vote up
private ProxyConfiguration getProxy() {
    return Jenkins.getActiveInstance().proxy;
}
 
Example 5
Source File: UserNameNginxProxyAuthTest.java    From yet-another-docker-plugin with MIT License 4 votes vote down vote up
@Override
        public Boolean call() throws Throwable {
            final String dockerLabel = "docker-label";
            final Jenkins jenkins = Jenkins.getActiveInstance();

            // prepare jenkins global (url, cred)
            JenkinsLocationConfiguration.get().setUrl(String.format("http://%s:%d", dockerUri.getHost(), jenkinsPort));

            final UsernamePasswordCredentialsImpl credentials = new UsernamePasswordCredentialsImpl(
                    CredentialsScope.GLOBAL,
                    null,
                    "description",
                    "docker",
                    "docker"
            );

            SystemCredentialsProvider.getInstance().getCredentials().add(credentials);

            // prepare Docker Cloud
            final DockerConnector dockerConnector = new DockerConnector(
                    String.format("tcp://%s:%d", dockerUri.getHost(), CONTAINER_PORT)
            );
            dockerConnector.setConnectTimeout(50 * 1000);
            dockerConnector.setConnectorType(JERSEY);
            dockerConnector.setCredentialsId(credentials.getId());
            dockerConnector.testConnection();
//            final Version version = dockerConnector.getClient().versionCmd().exec();
//            LOG.info("Version {}", version);

            final DockerComputerJNLPLauncher launcher = new DockerComputerJNLPLauncher();
            final DockerPullImage pullImage = new DockerPullImage();
            pullImage.setPullStrategy(PULL_ALWAYS);

            final DockerRemoveContainer removeContainer = new DockerRemoveContainer();
            removeContainer.setRemoveVolumes(true);
            removeContainer.setForce(true);

            final DockerContainerLifecycle containerLifecycle = new DockerContainerLifecycle();
            containerLifecycle.setImage(slaveImage);
            containerLifecycle.setPullImage(pullImage);
            containerLifecycle.setRemoveContainer(removeContainer);

            final DockerSlaveTemplate slaveTemplate = new DockerSlaveTemplate();
            slaveTemplate.setLabelString(dockerLabel);
            slaveTemplate.setLauncher(launcher);
            slaveTemplate.setMode(Node.Mode.EXCLUSIVE);
            slaveTemplate.setRetentionStrategy(new DockerOnceRetentionStrategy(10));
            slaveTemplate.setDockerContainerLifecycle(containerLifecycle);

            final List<DockerSlaveTemplate> templates = new ArrayList<>();
            templates.add(slaveTemplate);

            final DockerCloud dockerCloud = new DockerCloud(
                    "docker",
                    templates,
                    3,
                    dockerConnector
            );

            jenkins.clouds.add(dockerCloud);
            jenkins.save(); // either xmls a half broken

            return true;
        }
 
Example 6
Source File: CloudNanny.java    From ec2-spot-jenkins-plugin with Apache License 2.0 2 votes vote down vote up
/**
 * We return {@link List} instead of original {@link jenkins.model.Jenkins.CloudList}
 * to simplify testing as jenkins list requires actual {@link Jenkins} instance.
 *
 * @return basic java list
 */
@VisibleForTesting
private static List<Cloud> getClouds() {
    return Jenkins.getActiveInstance().clouds;
}