如何扩展Jenkins Credential Plugin并添加自定义选项?
Hey there! I totally get how frustrating it can be when you want to extend a Jenkins plugin but can’t find clear docs to start with. Let’s break this down step by step for adding a custom credential type to the Credential Plugin.
First, you’ll need to understand the foundational components of the Credential Plugin. Start by looking at the core interfaces and abstract classes in Jenkins’ credential module:
- The
Credentialsinterface is the base for all credential types. BaseStandardCredentialsis an abstract class that handles common fields like scope, ID, and description—you’ll probably want to inherit from this to save time.CredentialsDescriptoris what defines how your credential appears in the Jenkins UI (display name, form validation, etc.).
Take a peek at existing implementations in the Credential Plugin source code (like UsernamePasswordCredentialsImpl or SecretTextCredentialsImpl)—they’re great blueprints for your custom type.
Use the Jenkins Plugin Archetype to bootstrap your project. Run this command in your terminal:
mvn archetype:generate -Dfilter=io.jenkins.archetypes:
Select the plugin archetype when prompted. Then, add the Credential Plugin dependency to your pom.xml (use the latest stable version):
<dependency> <groupId>org.jenkins-ci.plugins</groupId> <artifactId>credentials</artifactId> <version>最新稳定版本号</version> <scope>provided</scope> </dependency>
Create a class that either implements Credentials or inherits from BaseStandardCredentials. For example, if you’re building an API key credential:
import com.cloudbees.plugins.credentials.CredentialsScope; import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials; import edu.umd.cs.findbugs.annotations.NonNull; import org.kohsuke.stapler.DataBoundConstructor; public class ApiKeyCredentials extends BaseStandardCredentials { private final String apiKey; @DataBoundConstructor public ApiKeyCredentials(CredentialsScope scope, String id, String description, String apiKey) { super(scope, id, description); this.apiKey = apiKey; } @NonNull public String getApiKey() { return apiKey; } }
The @DataBoundConstructor annotation tells Jenkins how to map form inputs to your class constructor.
Every credential needs a descriptor to integrate with Jenkins’ UI. Create a class that extends CredentialsDescriptor and mark it with @Extension so Jenkins picks it up:
import com.cloudbees.plugins.credentials.CredentialsDescriptor; import hudson.Extension; import org.jenkinsci.Symbol; import hudson.util.FormValidation; import org.kohsuke.stapler.QueryParameter; @Extension @Symbol("apiKeyCredential") public class ApiKeyCredentialsDescriptor extends CredentialsDescriptor { @Override public String getDisplayName() { return "API Key Credential"; // This is the name users will see in the "Add Credential" menu } // Optional: Add form validation for your fields public FormValidation doCheckApiKey(@QueryParameter String value) { if (value == null || value.trim().isEmpty()) { return FormValidation.error("API Key cannot be empty"); } return FormValidation.ok(); } }
Jenkins uses Jelly templates to render forms and summaries. Create two files in src/main/resources/[your-package-path]/ApiKeyCredentials:
config.jelly(renders the input form):
<?jelly escape-by-default='true'?> <j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form"> <f:entry title="${%API Key}" field="apiKey"> <f:textbox /> </f:entry> </j:jelly>
summary.jelly(shows a truncated view in the credential list):
<?jelly escape-by-default='true'?> <j:jelly xmlns:j="jelly:core"> ${instance.description} (API Key: ${instance.apiKey.substring(0, 4)}...) </j:jelly>
Run a local Jenkins instance with your plugin using:
mvn hpi:run
Once Jenkins starts, go to Manage Jenkins > Manage Credentials, click Add Credential, and you should see your custom "API Key Credential" option. Test creating a credential to make sure it saves and displays correctly.
- If your credential type doesn’t show up, double-check that your descriptor has the
@Extensionannotation and that your package path is correct. - Check the Jenkins log (
target/jenkins.log) for class loading errors or exceptions. - If you hit a roadblock, reverse-engineer existing credential types in the Credential Plugin—they’ll answer most structural questions.
内容的提问来源于stack exchange,提问作者Sai Lakshmi

