1

在我的流程中,我經常獲得臨時AWS信用,在我的Jenkins文件中,我需要更新特定的Jenkins Aws信譽。 我如何更新它? 我需要的原因是Jenkins docker方法withRegistry需要憑證ID,每當我得到新的AWS密鑰以便能夠使用憑證時,我必須更新此憑證。使用groovy腳本更新jenkins文件中的aws憑證

回答

1

經過多次嘗試和錯誤,我發現這一點:

import jenkins.model.* 
import com.cloudbees.plugins.credentials.* 
import com.cloudbees.plugins.credentials.impl.* 
import com.cloudbees.plugins.credentials.domains.* 
import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey 
import com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl 
import org.jenkinsci.plugins.plaincredentials.StringCredentials 
    def changePassword = { id,accessKey, secKey -> 
def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
    com.cloudbees.jenkins.plugins.awscredentials.AWSCredentialsImpl.class, 
    Jenkins.instance 
) 
def c = creds.findResult { it.id == id ? it : null } 
if (c) { 
    println "found credential ${c.id} for accessKey ${c.accessKey}" 

    def credentials_store = Jenkins.instance.getExtensionList(
     \'com.cloudbees.plugins.credentials.SystemCredentialsProvider\' 
     )[0].getStore() 

    def result = credentials_store.updateCredentials(
     com.cloudbees.plugins.credentials.domains.Domain.global(), 
     c, 
     new AWSCredentialsImpl(c.scope, id, accessKey, secKey,c.description) 
     ) 

    if (result) { 
     println "password changed for ${accessKey}" 
    } else { 
     println "failed to change password for ${accessKey}" 
    } 
} else { 
    println "could not find credential for ${accessKey}" 
} 
}