Configuration as Code relies on Describable and DataBound mechanism Jenkins plugin developers are probably already using.
As long as you follow best practices
using those annotations for data-binding, same attributes will be usable for configuration-as-code.
Most of the interesting plugin's configuration you want to expose to end users with JCasC is managed by your plugin's
Descriptor(s) and exposed on web UI with a global.jelly view. This is fully supported by JCasC as long as you rely on
the exact same DataBound mechanism, which isn't a common practice (yet).
In many plugins, Descriptor#configure() is implemented by lookup for attributes values from the JSONObject. To make your Descriptor
compliant with JCasC, you'll need to expose your configuration attributes as @DataBoundSetters.
Before you start, make sure the following pre-conditions are met:
You are using a recent version of plugin parent pom.
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>THE_PARENT_POM_VERSION_HERE</version>
<relativePath/>
</parent>
The Jenkins core version and the Java level of your plugin are aligned with the Configuration as Code plugin versions (also in the pom.xml).
Define @DataBoundSetters JavaBean setters for your Descriptor's properties. They should match the getters you already have for
global.jelly data-binding.
Define a new attribute in your Descriptor to own optional attributes.
For binary compatibility you'll need to maintain the legacy getters as delegates to this new sub-component.
For backward compatibility, use readResolve method to create the new nested component from legacy attributes.
Rewrite Descriptor#configure() implementation to rely on request.bindJson(this, json). You will have to reset attributes to their
default values as a Descriptor is a mutable object, i.e. data-binding won't reset values if they are not present in the JSON payload.
If you don't have one already, define a @Symbol annotation on your descriptor. This is the name an end user will be able to use to access
your Descriptor for configuration. To avoid collisions with other plugins, prefer using your plugin's artifactId as a symbolic name for your
descriptor.
assertTrue(/* check plugin has been configured as expected */);
}
}
Doing so, you will confirm JCasC is able to introspect your plugin and build the expected configuration data model, but also detect
some changes made to your plugin break this configuration model.
assertTrue(/* check plugin has been configured as expected */);
}
Within this obsolete-configuration-as-code.yml configuration file, use the legacy data model in use before the change you introduced, and enable JCasC support for deprecated methods:
configuration-as-code:
deprecated: warn
This will let JCasC consider any @Deprecated setter in your component as a valid attribute to be set, enabling backward compatibility,
while the canonical JCasC model evolves to match the changes you made.
We generate a JSON schema that users can use to validate their changes and provide IDE assistance,
you can test that your plugin's example yaml file validates correctly by implementing the below test:
SchemaGenerationTest provides a abstraction layer to test out the plugins YAML file against the generated schema.
You can test if your YAML file validates against the schema.
Step 1
Create a YAML file for the configurators corresponding to the developed plugin.
For eg: validJenkinsConfigurator.yml
jenkins:
systemMessage:"Configured by Configuration as Code plugin"