• Jenkins Pipeline 流水线 withCredentials 使用


    添加凭证

    image
    image
    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
    
  • 相关阅读:
    loj6033.「雅礼集训 2017 Day2」棋盘游戏
    loj6032. 「雅礼集训 2017 Day2」水箱
    BZOJ 5217 [Lydsy2017省队十连测] 航海舰队
    P4173 残缺的字符串
    P3723 [AH2017/HNOI2017]礼物
    P3321 [SDOI2015]序列统计
    P4841 [集训队作业2013]城市规划
    MySQL基础
    MySQL查询
    HTTP响应码
  • 原文地址:https://www.cnblogs.com/vipsoft/p/16850005.html
Copyright © 2020-2023  润新知