Jenkins Pipeline 流水线 - withCredentials 使用

简介: Jenkins Pipeline 流水线 - withCredentials 使用

添加凭证

Pipeline script

pipeline {
    agent any
 
    stages {  
        stage('withCredentials 使用凭证') { 
            steps {
                withCredentials([usernamePassword(credentialsId: 'DockerServer', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
                  // available as an env variable, but will be masked if you try to print it out any which way
                  // note: single quotes prevent Groovy interpolation; expansion is by Bourne Shell, which is what you want
                  bat 'echo ${USERNAME}' //Linux 下 sh 'echo $USERNAME',TODO Windows 上怎么写,
                  // also available as a Groovy variable
                  echo USERNAME
                 // or inside double quotes for string interpolation
                  echo "username is $USERNAME"
                }  
                echo 'Credentials SUCCESS'
            }
        }
        //指定 Docker 服务器节点。用于编译、上传镜像,指定K8S节点,用于升级程序
        stage('指定节点中使用凭证') { 
            agent { label 'DockerAgent' }
            steps {  
                withCredentials([usernamePassword(credentialsId: 'DockerServer', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {                 
                    sh 'docker --version' 
                    sh "echo 'docker login -u ${USERNAME} -p ${PASSWORD} image_url'"
                }  
                echo 'Credentials SUCCESS'
            }
        }
    }
}

执行结果

Started by user admin
[Pipeline] Start of Pipeline (hide)
[Pipeline] node
Running on Jenkins in D:\ProgramData\Jenkins\.jenkins\workspace\PipelineDemo
[Pipeline] {
[Pipeline] stage
[Pipeline] { (withCredentials 使用凭证)
[Pipeline] withCredentials
Masking supported pattern matches of %PASSWORD%
[Pipeline] {
[Pipeline] bat
D:\ProgramData\Jenkins\.jenkins\workspace\PipelineDemo>echo $PASSWORD 
$PASSWORD
[Pipeline] echo
root
[Pipeline] echo
username is root
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] echo
Credentials SUCCESS
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (指定节点中使用凭证)
[Pipeline] node
Running on DockerAgent in /opt/jenkins/workspace/PipelineDemo
[Pipeline] {
[Pipeline] withCredentials
Masking supported pattern matches of $PASSWORD
[Pipeline] {
[Pipeline] sh
+ docker --version
Docker version 20.10.18, build b40c2f6
[Pipeline] sh
Warning: A secret was passed to "sh" using Groovy String interpolation, which is insecure.
     Affected argument(s) used the following variable(s): [PASSWORD]
     See https://jenkins.io/redirect/groovy-string-interpolation for details.
+ echo 'docker login -u root -p **** image_url'
docker login -u root -p **** image_url
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] echo
Credentials SUCCESS
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
目录
相关文章
|
4月前
|
jenkins 持续交付 开发工具
jenkins学习笔记之一:流水线基础语法
jenkins学习笔记之一:流水线基础语法
|
4月前
|
前端开发 jenkins 持续交付
jenkins学习笔记之二十:docker in docker运行pipeline
jenkins学习笔记之二十:docker in docker运行pipeline
|
4月前
|
jenkins Devops Java
jenkins学习笔记之十:配置Gitlab提交流水线
jenkins学习笔记之十:配置Gitlab提交流水线
|
4月前
|
jenkins 持续交付 网络安全
Jenkins Pipeline SSH Publisher 环境变量、参数引用 要用双引号
Jenkins Pipeline SSH Publisher 环境变量、参数引用 要用双引号
150 0
|
4月前
|
jenkins 持续交付 Windows
Jenkins Pipeline 多分支流水线 Input length = 1
Jenkins Pipeline 多分支流水线 Input length = 1
41 0
|
4月前
|
运维 jenkins Devops
Jenkins 多分支流水线(SVN)
Jenkins 多分支流水线(SVN)
81 0
|
4月前
|
jenkins 持续交付
Jenkins Pipeline 流水线 - Parameters 参数化构建
Jenkins Pipeline 流水线 - Parameters 参数化构建
138 0
|
4月前
|
jenkins 持续交付 网络安全
Jenkins Pipeline 流水线 - 完整构建 Pipeline Script 脚本
Jenkins Pipeline 流水线 - 完整构建 Pipeline Script 脚本
88 0
|
4月前
|
Kubernetes jenkins 网络安全
Jenkins Pipeline 流水线 - 使用代理节点,Remote SSH 对 K8S 进行升级
Jenkins Pipeline 流水线 - 使用代理节点,Remote SSH 对 K8S 进行升级
57 0
|
4月前
|
弹性计算 运维 Serverless
项目管理和持续集成系统搭建问题之云效流水线支持阿里云产品的企业用户如何解决
项目管理和持续集成系统搭建问题之云效流水线支持阿里云产品的企业用户如何解决
91 1
项目管理和持续集成系统搭建问题之云效流水线支持阿里云产品的企业用户如何解决

热门文章

最新文章