I would also need this please - can this be upvoted somewhere?
workarround
import com.cloudbees.plugins.credentials.CredentialsProviderdef credsList = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( com.cloudbees.plugins.credentials.Credentials.class,Jenkins.instance,null,null); def creds = credsList.findResult { it.id == "<YOUR-CREDS-ID>" ? it : null } println("apiToken: ${creds.apiToken}")
where <YOUR-CREDS-ID> - creds id in Jenkins
Is there any other solution other than this workaround?
GitLab API Token is exactly a StringCredentials and should have been implemented as such. The GitLab plugin could instead accept String Credentials and an alias could be created for backward compatibility, see for example https://github.com/jenkinsci/kubernetes-plugin/blob/kubernetes-1.19.2/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java#L719-L726.