pipeline读取jenkins凭据

  • 原创
  • |
  • 浏览:1349
  • |
  • 更新:

pipeline读取jenkins凭据

groovy读取jenkins凭据

工具/原料

  • jenkins

方法/步骤

  1. 1

    在jenkins中新建任务

    pipeline读取jenkins凭据
    pipeline读取jenkins凭据
  2. 2

    配置任务

    pipeline读取jenkins凭据
  3. 2
    本页面未经授权抓取自百度经验
  4. 3

    将以下内容黏贴到流水线脚本框

    import com.cloudbees.plugins.credentials.CredentialsNameProvider

    import com.cloudbees.plugins.credentials.Credentials

    import com.cloudbees.plugins.credentials.CredentialsProvider

    import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials

    import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey

    import org.jenkinsci.plugins.plaincredentials.StringCredentials

    import org.jenkinsci.plugins.plaincredentials.FileCredentials

     

    def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(

            com.cloudbees.plugins.credentials.Credentials.class,

            Jenkins.instance,

            null,

            null

        );

    for (credential in creds) {

        

            if(credential instanceof StandardUsernamePasswordCredentials){

                println("""

                    Username Password

                    ID: ${credential.id}

                    Description: ${credential.description}

                    Username: ${credential.username}

                    Password: ${credential.password?.getPlainText()}\n\n

                """)

            }else if(credential instanceof SSHUserPrivateKey){

                println("""

                    SSH User Private Key

                    ID: ${credential.id}

                    Description: ${credential.description}

                    Key:

                    ${credential.privateKeySource?.getPrivateKey()}

                    Passphrase:

                    ${credential.passphrase?.getPlainText()}\n\n

                """)

            }else if(credential instanceof StringCredentials){

                println("""

                    String

                    ID: ${credential.id}

                    Description: ${credential.description}

                    Secret: ${credential.secret?.getPlainText()}\n\n

                """)

            }else if(credential instanceof FileCredentials){

                println("""

                    File

                    ID: ${credential.id}

                    Description: ${credential.description}

                    Content:

                    ${credential.content?.text}\n\n

                """)

            }else{

                println("""

                    ${credential.class.name}

                    ID: ${credential.id}

                    Description: ${credential.description}\n\n

                """)

            }

    }

    pipeline读取jenkins凭据
  5. 4

    构建任务后会多次出现如下错误,点进去。然后选择“Approve”

    pipeline读取jenkins凭据
    pipeline读取jenkins凭据
  6. 5

    最后构建成功

    pipeline读取jenkins凭据
    END
经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。
作者声明:本篇经验系本人依照真实经历原创,未经许可,谢绝转载。
展开阅读全部