diff --git a/README.adoc b/README.adoc index 9d4aae7d1d..5a26ceee7d 100644 --- a/README.adoc +++ b/README.adoc @@ -279,7 +279,7 @@ Refer to the link:https://git-scm.com/book/en/v2/Git-Internals-The-Refspec[git r The git plugin supports username / password credentials and private key credentials provided by the https://plugins.jenkins.io/credentials[Jenkins credentials plugin]. -It does not support other credential types like secret text, secret file, or certificates. +It does not support other credential types like secret file or certificates. Select credentials from the job definition drop down menu or enter their identifiers in Pipeline job definitions. When the remote repository is accessed with the **HTTP or HTTPS protocols**, the plugin requires a **username / password credential**. diff --git a/src/main/java/hudson/plugins/git/GitSCM.java b/src/main/java/hudson/plugins/git/GitSCM.java index d101a3456f..c28dc99cca 100644 --- a/src/main/java/hudson/plugins/git/GitSCM.java +++ b/src/main/java/hudson/plugins/git/GitSCM.java @@ -1,10 +1,8 @@ package hudson.plugins.git; -import com.cloudbees.plugins.credentials.CredentialsMatcher; -import com.cloudbees.plugins.credentials.CredentialsMatchers; import com.cloudbees.plugins.credentials.CredentialsProvider; +import com.cloudbees.plugins.credentials.common.StandardCredentials; import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials; -import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder; import com.google.common.collect.Iterables; @@ -64,6 +62,7 @@ import org.eclipse.jgit.transport.RemoteConfig; import org.eclipse.jgit.transport.URIish; import org.jenkinsci.plugins.gitclient.*; +import org.jenkinsci.plugins.plaincredentials.StringCredentials; import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted; import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; @@ -924,7 +923,7 @@ private GitClient createClient(TaskListener listener, EnvVars environment, @NonN listener.getLogger().println("No credentials specified"); } else { String url = getParameterString(uc.getUrl(), environment); - StandardUsernameCredentials credentials = lookupScanCredentials(build, url, ucCredentialsId); + var credentials = lookupScanCredentials(build, url, ucCredentialsId); if (credentials != null) { c.addCredentials(url, credentials); if(!isHideCredentials()) { @@ -943,19 +942,35 @@ private GitClient createClient(TaskListener listener, EnvVars environment, @NonN return c; } - private static StandardUsernameCredentials lookupScanCredentials(@NonNull Run build, - @CheckForNull String url, - @CheckForNull String ucCredentialsId) { + private static StandardCredentials lookupScanCredentials(@NonNull Run build, + @CheckForNull String url, + @CheckForNull String ucCredentialsId) { if (Util.fixEmpty(ucCredentialsId) == null) { return null; - } else { - StandardUsernameCredentials c = CredentialsProvider.findCredentialById( - ucCredentialsId, - StandardUsernameCredentials.class, - build, - URIRequirementBuilder.fromUri(url).build()); - return c != null && GitClient.CREDENTIALS_MATCHER.matches(c) ? c : null; } + + StandardCredentials c; + c = CredentialsProvider.findCredentialById( + ucCredentialsId, + StandardUsernameCredentials.class, + build, + URIRequirementBuilder.fromUri(url).build()); + + if (c != null && GitClient.CREDENTIALS_MATCHER.matches(c)) { + return c; + } + + c = CredentialsProvider.findCredentialById( + ucCredentialsId, + StringCredentials.class, + build, + URIRequirementBuilder.fromUri(url).build()); + + if (c != null && GitClient.CREDENTIALS_MATCHER.matches(c)) { + return c; + } + + return null; } @NonNull