首页 > 其他分享 >Jenkins Pipeline 流水线 - withCredentials 使用

Jenkins Pipeline 流水线 - withCredentials 使用

时间:2022-11-02 09:58:40浏览次数:55  
标签:Pipeline withCredentials echo sh Jenkins PASSWORD stage

添加凭证

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 $PASSWORD' //windows 下 sh 'echo $PASSWORD' 报错,在当前服务器上使用凭证进行一些操作

                  // 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

标签:Pipeline,withCredentials,echo,sh,Jenkins,PASSWORD,stage
From: https://www.cnblogs.com/vipsoft/p/16850005.html

相关文章

  • Jenkins: Environment
     Jenkins环境变量就是通过env关键字暴露出来的全局变量,可以在Jenkins文件的任何位置使用 查看Jenkins系统内置环境变量${JENKINS_URL}/env-vars.htmlthrou......
  • Linux系统安装jenkins
    搭建Jenkins,准备搞一个定时任务来自动部署服务。做个记录。jdk版本:jdk-11.0.17Jenkins版本:2.3751.应用下载jdk下载:https://www.oracle.com/java/technologies/do......
  • 11Jmeter之优化jenkins上html报告格式
    问题:当在jenkins上查看HTML报告时,发现报告格式不美观!  解决一:临时解决方法1、进入Manage Jenkins->Scriptconsole,输入如下命令并进行执行。System.setPropert......
  • Jenkins Pipeline 流水线 - 添加节点 使用代理
    Jenkins安装在Windows上Docker在Linux上流程将Docker在Jenkins节点中维护Pipeline中指定某些阶段使用哪个节点添加节点CheckingJavaversionint......
  • Jenkins Pipeline 流水线 - 拉代码(SVN) + Maven 编译打包
    JenkinsPipeline流水线步骤拉取SVN代码->Maven构建->Docker编译->发布至阿里云仓库->K8S更新Jenkins插件安装Localization:Chinese(Simplified)Subve......
  • Jenkins Blue Ocean
    介绍BlueOcean是pipeline的可视化UI。同时兼容经典的自由模式的job。JenkinsPipeline从头开始设计,但仍与自由式作业兼容,BlueOcean减少了经典模式下的混乱并为团......
  • Jenkins Pipeline 流水线-Demo
    JenkinsPipeline流水线流水线既能作为任务的本身,也能作为Jenkinsfile使用流水线可以让我们的任务从UI手动操作,转换为代码化,像dockerfile一样。从shell命令到配置文......
  • gitlab+jenkins+harbor+k8s部署微服务-准备工作
    DevOps,字面意思是Development&Operations的缩写,也就是开发&运维。一、Code阶段工具在code阶段,我们需要将不同版本的代码存储到一个仓库中,常见的版本控制工具就是SVN或者......
  • Jenkins Pipeline
    内置支持片段生成器:http://localhost:8080/pipeline-syntax/ConceptJenkinsPipelineisasuiteofpluginswhichsupportsimplementingandintegratingcontinuousdel......
  • Jenkins Plugin Developer Guide
    Plugintutorial​​Plugintutorial​​重要接口ExtensionPoint&Describable:apluginmaysimplyimplementextensionpoints,[email protected]......