• Jenkins pipeline使用ssh凭据


    环境信息

    Linux环境

    Linux version 3.10.0-1062.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Wed Aug 7 18:08:02 UTC 2019

    > cat /etc/redhat-release
    CentOS Linux release 7.7.1908 (Core)

    Jenkins版本

    jenkins需要安装的docker插件

    环境说明

    CentOs7安装的Docker环境,使用Docker安装的Jenkins服务。

    附录

    [https://docs.docker.com/engine/install/centos/](Centos7 Docker安装指南)
    [https://www.jenkins.io/doc/book/installing/docker/](Jenkins docker安装指南)

    在Jenkins文件中使用凭据

    先让我们了解以下credentials()方法都支持哪几种凭据呢?
    支持以下三种凭据:

    如果你使用的凭据正是以上三种之一,你就可以使用credentials()辅助方法。

    否则你需要使用withCredentials(){}方法闭包进行使用凭据。
    https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#for-other-credential-types

    这里就对配置和使用的方式不叙述了,因为参考的文档写的很详细了。
    这里就对这2种使用ssh的注意事项吧。

    credentials() 辅助方法

    user and password方式

    对环境变量的值的格式格外注意。

    withCredentials(){}闭包

    withCredentials(bindings: [sshUserPrivateKey(credentialsId: 'jenkins-ssh-key-for-abc', \
                                                 keyFileVariable: 'SSH_KEY_FOR_ABC', \
                                                 passphraseVariable: '', \
                                                 usernameVariable: '')]) {
      // some block
      // 请注意,这里对插值的说明没有问题,jenkins示例使用的是sh命令的方式,而这里是在groovy script脚本的方式。
      // 在你的代码块里可以使用 "${SSH_KEY_FOR_ABC}"的方式进行引入变量的值,不过会在输出种有警告信息,脚本倒是能正常运行。
      // 警告的信息就是说,凭据不能通过任何日志的形式输出出去。“在上面的 Groovy 中使用单引号而不是 双引号来定义script (隐含参数sh)。单引号将导致 shell 将密钥扩展为环境变量。双引号可能不太安全,因为密码是由 Groovy 插入的,因此典型的操作系统进程列表会意外地泄露它”
      // 也可以使用 SSH_KEY_FOR_ABC 的方式进行引入变量的值,这种方式不会有警告信息。因为这是groovy语法,运行时脚本语言。
      // 以下是代码示例。
      
      // 示例1:会有警告信息
      def keyfilepath = "${SSH_KEY_FOR_ABC}"
      
      // 示例2:不会有警告信息
      def keyfilepath = SSH_KEY_FOR_ABC
    
      ...下面就是使用ssh等语法了
    
    }
    
    

    [https://www.jenkins.io/doc/pipeline/steps/credentials-binding/#withcredentials-bind-credentials-to-variables](Jenkins withCredentials插件介绍)

    示例pipeline脚本

    pipeline {
        agent {
            node {
                label 'agent-2'
            }
        }
        stages {
            stage('Example stage 1') {
                steps {
                    sh "pwd"
                    sh "echo 'ni'hao你好a你好啊,wo'shi我是e'mo我是恶魔xian's'我是恶魔先生 > helloworld_test_jenkins.txt"
                }
            }
            stage('print ssh primary key') {
                steps {
                    sh 'printenv'
                }
            }
            
            stage('Example stage 2') {
                steps {
                    
                    
                    
                    withCredentials([sshUserPrivateKey(credentialsId: '6581769e-f6cb-4501-a9b3-ceb51ca38fb8', keyFileVariable: 'primaryKeyVar', usernameVariable: 'userVar')]) {
                            
                        script {
                            // some block
                              def remote = [:]
                              remote.name = '192.168.115.128'
                              remote.host = '192.168.115.128'
                              remote.user = userVar
                              remote.identityFile = $primaryKeyVar
                              remote.allowAnyHosts = true
                              stage('Remote SSH') {
                                sshPut remote: remote, from: 'helloworld_test_jenkins.txt', into: '/opt/'
                              }
                        }
                        
                    }
                    
                }
            }
        }
    }
    

    常见问题

    jenkins如何打印运行任务的环境变量信息?

     steps {
         sh 'printenv'
     }
    

    将以上脚本放到你将在哪个阶段(stage)打印环境变量信息里就行。

    引用

    https://plugins.jenkins.io/credentials-binding/

    jenkins主服务器带的pipeline脚本生成器地址:http://jenkins主服务器地址/job/项目job名称/pipeline-syntax/,替换其中的描述字符即可。使用的方式参考https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#handling-credentials的文档吧

    20220920补充

    1. 使用私钥最好使用rsa算法。

    • coding使用持续集成,编写jenkins文件,使用remote.identity设置密钥字符串,最终结果是失败的,无效 invalid privatekey: [B@3a4328fa。

    • Jenkins使用pipeline流水线,编写jenkins文件,也是使用密钥字符串的形式,最终结果是失败的,无效 invalid privatekey: [B@3a4328fa。

    最后使用rsa可以正常使用。
    生成rsa密钥命令:ssh-keygen -m PEM -t rsa -b 4096

  • 相关阅读:
    对软件工程的理解及问题
    使用Junit等工具进行单元测试
    软件工程
    进销存管理系统——可行性分析
    使用Junit等工具进行单元测试
    两个人的分组
    物联网软件工程 认识与问题
    二人项目
    使用Junit等工具进行单元测试
    软件工程
  • 原文地址:https://www.cnblogs.com/XingXiaoMeng/p/16686763.html
Copyright © 2020-2023  润新知