簡體   English   中英

如何在 jenkins DSL 的 groovy 腳本中使用存儲在 Jenkins 中的憑據

[英]How to use the credentials stored in Jenkins in a groovy script in jenkins DSL

我正在使用 jenkins 和 Jobdsl 來創建 jenkins 工作。 我正在嘗試通過在活動選擇參數中添加一個 groovy 腳本來構建參數化作業。 該腳本使用存儲在 jenkins 憑據中的憑據,我正在嘗試使用代碼在我的腳本中獲取它

import jenkins.model.*
import jenkins.*
import jenkins.model.* 
import hudson.*
import hudson.model.*

credentialsId = '1672622gjj'
def jenkinsCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
        com.cloudbees.plugins.credentials.Credentials.class,
        Jenkins.instance,
        null,
        null
).find{it.id == credentialsId};

def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
  com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, Jenkins.instance, null, null ).find{
    it.id == credentialsId}
    println(creds.username)
    println(creds.password)


This code gives me the credential name and password but the result of the branches is blank. I am using `creds.password` as the authorization token.

What I am doing wrong?

你可以用同樣的方式替換那里的字符串,你已經用你的腳本名稱( +字符串)做了,但這一點都不常規。

所以這應該有效:

... "curl ... 'Authorization: token ${creds.password}' ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM