configure global libraries

This commit is contained in:
Nicolas De Loof 2017-12-19 17:14:52 +01:00
parent 5561836f25
commit 46b32a36ab
9 changed files with 82 additions and 2 deletions

View File

@ -11,8 +11,10 @@ Here is a list of plugin we have successfuly tested to support configuration-as-
- [x] active directory plugin ([details](demos/credentials/README.md))
- [x] artifactory plugin ([details](demos/artifactory/README.md))
- [x] credentials plugin ([details](demos/credentials/README.md))
- [x] docker plugin ([details](demos/docker/README.md))
- [x] git plugin ([details](demos/git/README.md))
- [x] ldap plugin ([details](demos/ldap/README.md))
- [x] mailer plugin with some limitations ([details](demos/mailer/README.md))
- [x] tfs plugin with some limitations ([details](demos/tfs/README.md))
- [x] workflow-cps-global-lib _aka_ "global libraries" ([details](demos/workflow-cps-global-lib/README.md))
- [ ] more to come soon...

View File

@ -0,0 +1,16 @@
# configure global libraries plugin
## sample configuration
```yaml
globalLibraries:
libraries:
- name: "awesome-lib"
retriever:
modernSCM:
scm:
git:
remote: "https://github.com/jenkins-infra/pipeline-library.git"
```

View File

@ -131,6 +131,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps-global-lib</artifactId>
<version>2.9</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -45,6 +45,7 @@ public abstract class BaseConfigurator<T> extends Configurator<T> {
// FIXME move this all into cleaner logic to discover property type
Type type = setter.getGenericParameterTypes()[0];
Attribute attribute = detectActualType(name, type);
if (attribute == null) continue;
attributes.add(attribute);
@ -136,7 +137,8 @@ public abstract class BaseConfigurator<T> extends Configurator<T> {
Attribute attribute;
if (!c.isPrimitive() && !c.isEnum() && Modifier.isAbstract(c.getModifiers())) {
if (!Describable.class.isAssignableFrom(c)) {
throw new IllegalStateException("Configuration-as-Code can't manage abstract attributes which are not Describable.");
// Not a Describable, so probably not an attribute expected to be selected as sub-component
return null;
}
attribute = new DescribableAttribute<T>(name, c);
} else {

View File

@ -38,7 +38,7 @@ public abstract class Configurator<T> implements ExtensionPoint {
public static Configurator lookupRootElement(String name) {
for (RootElementConfigurator c : RootElementConfigurator.all()) {
if (c.getName().equals(name)) {
if (c.getName().equalsIgnoreCase(name)) {
return (Configurator) c;
}
}

View File

@ -141,6 +141,7 @@ public class DataBoundConfigurator extends BaseConfigurator<Object> {
final Parameter p = parameters[i];
final Attribute a = detectActualType(names[i], p.getParameterizedType());
if (a == null) continue;
attributes.add(a);
}
}

View File

@ -53,14 +53,18 @@ public class JenkinsConfigurator extends BaseConfigurator<Jenkins> implements Ro
@Override
public void setValue(Jenkins jenkins, Object value) throws Exception {
List<TopLevelItem> jobs = (List<TopLevelItem>) value;
// FIXME not pleasant we have to re-implement jenkins.createProject logic here
for (TopLevelItem item : jobs) {
final String name = item.getName();
if (jenkins.getItem(name) == null) {
item.onCreatedFromScratch();
item.save();
jenkins.add(item, name);
} else {
// FIXME re-configure ? remove/replace ?
}
}
Jenkins.getInstance().rebuildDependencyGraphAsync();
}
}.multiple(true));

View File

@ -0,0 +1,40 @@
package org.jenkinsci.plugins.casc;
import com.nirima.jenkins.plugins.docker.DockerCloud;
import com.nirima.jenkins.plugins.docker.DockerTemplate;
import io.jenkins.docker.connector.DockerComputerAttachConnector;
import jenkins.plugins.git.GitSCMSource;
import jenkins.scm.api.SCMSource;
import org.jenkinsci.plugins.workflow.libs.GlobalLibraries;
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration;
import org.jenkinsci.plugins.workflow.libs.LibraryRetriever;
import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
public class GlobalLibrariesTest {
@Rule
public JenkinsRule j = new JenkinsRule();
@Test
public void configure_global_library() throws Exception {
ConfigurationAsCode.configure(getClass().getResourceAsStream("GlobalLibrariesTest.yml"));
assertEquals(1, GlobalLibraries.get().getLibraries().size());
final LibraryConfiguration library = GlobalLibraries.get().getLibraries().get(0);
assertEquals("awesome-lib", library.getName());
final SCMSourceRetriever retriever = (SCMSourceRetriever) library.getRetriever();
final GitSCMSource scm = (GitSCMSource) retriever.getScm();
assertEquals("https://github.com/jenkins-infra/pipeline-library.git", scm.getRemote());
}
}

View File

@ -0,0 +1,8 @@
globalLibraries:
libraries:
- name: "awesome-lib"
retriever:
modernSCM:
scm:
git:
remote: "https://github.com/jenkins-infra/pipeline-library.git"