[英]How to programmatically invoke Git with credentials in Jenkins Groovy script?
My task is similar to the one, described here:我的任务与此处描述的任务相似:
Dynamically Fill Jenkins Choice Parameter With Git Branches In a Specified Repo 使用指定存储库中的 Git 分支动态填充 Jenkins 选择参数
Basically, I want to fetch tags from a remote repositories within Jenkins Groovy script.基本上,我想从 Jenkins Groovy 脚本中的远程存储库中获取标签。
But the problem is, that my repositories require credentials for accessing them - SSH keys, which are stored in credentials storage of Jenkins.但问题是,我的存储库需要凭据才能访问它们 - SSH 密钥,这些密钥存储在 Jenkins 的凭据存储中。
Using the Credentials plugin you can inject your credentials as environment variables to your job.使用 Credentials 插件,您可以将您的凭证作为环境变量注入到您的工作中。 In Jenkins job DSL plugin it's called credentials binding.
在 Jenkins 工作 DSL 插件中,它被称为凭据绑定。 You can have the Credentials stored in 2 separate variables.
您可以将凭据存储在 2 个单独的变量中。
depending on what type of credentials, here is a groovy script to get username and password for credentials storage that can be used with https://plugins.jenkins.io/extensible-choice-parameter/ :根据凭据的类型,这里有一个 groovy 脚本,用于获取凭据存储的用户名和密码,可与https://plugins.jenkins.io/extensible-choice-parameter/一起使用:
def jenkinsCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.Credentials.class,
Jenkins.instance,
null,
null
);
def username
def password
for (creds in jenkinsCredentials) {
if(creds.id == "user-pass-id-in-jenkins-cred-storage"){
username = creds.username
password = creds.password
}
}
then to connect to git and get tags, something like :然后连接到 git 并获取标签,例如:
def command = [ "/bin/bash", "-c", "git ls-remote --tags https://$username:$password@yourgit.domain/path/repo.git | awk '{print \$2}' | sort -r -V | sed 's@refs/tags/@@'" ]
def process = command.execute();
process.waitFor()
def result = process.text.tokenize("\n")
I was looking to try this with an ssh key with -key parameter and SSH var, but it did not work, but I stumbled upon: https://plugins.jenkins.io/git-parameter/我想用带有 -key 参数和 SSH var 的 ssh 密钥来尝试这个,但它没有用,但我偶然发现: https : //plugins.jenkins.io/git-parameter/
Check it out, it probably gives you what you need without scripting.检查一下,它可能会在没有脚本的情况下为您提供所需的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.