jenkins pipeline避免使用明文密码

安装插件

Credentials Binding

创建凭证

在这里插入图片描述在这里插入图片描述忽略已经存在的凭证
在这里插入图片描述在这里插入图片描述这里以创建一个gitllab的账号密码为例

在这里插入图片描述
注意这个ID可以不填保存后可以随机生成,此ID唯一
在这里插入图片描述添加后会到凭据页面即可看到

测试

创建一个流水线项目
在这里插入图片描述

node ('haimaxy-jnlp'){
    stage ("Clone code") {
     withCredentials([usernameColonPassword(credentialsId: 'gitlab', variable: 'USERPASS')]){
    sh """
      git clone http://$USERPASS@"git地址" /ccreate-parent
     """
    }
    }
    stage ("Compile ccreate-parent") {
        sh """
         ls -l /ccreate-parent
        """
    }
}

在这里插入图片描述这里的用户名和密码变成了"*"

pipeline调用不同凭证的方法

使用SSH私钥文件

withCredentials([file(credentialsId: 'secret', variable: 'FILE')]) {
    sh 'use $FILE'
}

使用以冒号分隔的帐号密码

withCredentials([usernameColonPassword(credentialsId: 'gitlab', variable: 'USERPASS')]) {
	sh '''
	  git clone https://$USERPASS@********
	'''
}

使用字符串类型密钥

withCredentials([string(credentialsId: “CRET-ID”, variable: "varName")]){
    sh "echo $varName"
}

获取用户名密码

withCredentials([usernamePassword(credentialsId: “CRET-ID”, usernameVariable: "username", passwordVariable: "password")]){
    sh "echo $username $password"
}

参考博客:https://k4nz.com/Software_Engineering/Continuous_Delivery/Jenkins_Pipeline/Plugins_and_Programming/Credentials.html

猜你喜欢

转载自blog.csdn.net/qq_33235529/article/details/105161805