调取已录入的凭据
功能介绍
在持续集成中您可以通过凭据 ID 来代替明文配置账号密码、密钥等认证信息,从而更加安全地进行拉取代码、推送制品等操作。
在了解如何在持续集成中使用凭据之前,请确保您已初步了解 CODING 的《凭据管理》功能,如何创建凭据、查看凭据、更新凭据均请参考上述文档链接。
使用凭据 ID
在持续集成的 Jenkinsfile 文件里,通 credentialsId 来引用凭据 ID 。
凭据授权
在您创建好凭据后,您需要先给构建任务(Job)进行授权。在项目中,点击【项目设置】->【开发者选项】->【凭据管理】->【详情】,勾选需要授权的持续集成任务,则此任务拥有权限使用该凭据 ID。
如果您此时还未创建持续集成任务,也记得在稍后创建完任务后,去凭据管理中进行授权。
Jenkinsfile 配置
withCredentials 解析 【用户名 + 密码】类型的凭据
pipeline {
  agent any
  stages {
    stage('解析用户名+密码类型的凭据') {
      steps {
          // usernamePassword 表示需要传入一个是用户名密码类型的凭据 ID
          withCredentials([usernamePassword(
          // 当然也可以直接写固定值,credentialsId: "xxxx-xxxx-xxxxx"
            credentialsId: "${REMOTE_CRED}",
            passwordVariable: 'password',
            usernameVariable: 'userName'
          )]) {
            sh "echo $userName" 
            sh "echo $password"
            echo userName
            echo password
            sh "docker login -u $userName -p $password xxx-docker.pkg.coding.net"
          }
      }
    }
  }
}
withCredentials 解析 【SSH 私钥】类型的凭据
pipeline {
  agent any
  stages {
    stage('阶段-1') {
      steps {
        checkout([
          $class: 'GitSCM',
          branches: [[name: env.GIT_BUILD_REF]],
          userRemoteConfigs: [[
            url: env.GIT_REPO_URL,
            credentialsId: env.CREDENTIALS_ID
          ]]])
        }
      }
      stage("阶段名称") {
        steps {
            // sshUserPrivateKey 表示需要传入一个是 SSH 私钥类型的凭据 ID
          withCredentials([sshUserPrivateKey(
            // REMOTE_CRED 是一个环境变量,环境变量的值就是凭据的 ID
            // 当然也可以直接写固定值,credentialsId: "xxxx-xxxx-xxxxx",
                  credentialsId: "${REMOTE_CRED}",
                  keyFileVariable: "privateKeyFilePath"
          )]) {
            script {
                // 使用 ssh 插件
                remoteConfig = [:]
                remoteConfig.name = "my-remote-server"
                remoteConfig.host = "${REMOTE_HOST}"
                remoteConfig.allowAnyHosts = true
                remoteConfig.user = userName
                remoteConfig.port = 22
                // SSH 私钥文件地址
                remoteConfig.identityFile = privateKeyFilePath
                writeFile(file: 'test.sh', text: 'ls')
                sshCommand(remote: remoteConfig, command: 'for i in {1..5}; do echo -n \"Loop \$i \"; date ; sleep 1; done')
                sshScript(remote: remoteConfig, script: 'test.sh')
                sshPut(remote: remoteConfig, from: 'test.sh', into: '.')
                sshGet(remote: remoteConfig, from: 'test.sh', into: 'test_new.sh', override: true)
                sshRemove(remote: remoteConfig, path: 'test.sh')
                // 使用 ssh 命令
                sh "ssh -i $privateKeyFilePath root@1.1.1.1 'ls && pwd'"
            }
          }  
        }
      }
  }
}
检出代码使用【 SSH 私钥】类型的凭据 ID
譬如,我想通过 SSH 方式检出另外一个代码仓库的代码,我创建了一个 SSH 私钥类型的凭据 ID。在 Jenkinsfile 里即可在需要的地方通过该凭据 ID 检出仓库代码。
在 Jenkinsfile 中,按照如下格式填写 branches、SSH 形式的仓库地址 url 等信息对这个代码仓库进行检出,其中通过 credentialsId 来引用凭据 ID :
pipeline {
  agent any
  stages {
    stage('检出') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]], userRemoteConfigs: [[url: 'git@e.coding.net:anywhere/helloworld.git', credentialsId: 'db78fb2c-b146-xxxx-xxxx-xxxxxxxxxxxx']]])
      }
    }
  }
}
检出代码使用【用户名 + 密码】类型的凭据 ID
譬如,我想通过【用户名 + 密码】的方式检出另外一个代码仓库的代码,我创建了一个用户名密码类型的凭据 ID。在 Jenkinsfile 里即可在需要的地方通过凭据 ID 检出仓库代码。
在 Jenkinsfile 中,按照如下格式填写 branches、HTTP 形式的仓库地址 url 等信息对这个代码仓库进行检出,其中通过 credentialsId 来引用凭据 ID :
pipeline {
  agent any
  stages {
    stage('检出') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: env.GIT_BUILD_REF]], userRemoteConfigs: [[url: 'https://e.coding.net/anywhere/helloworld.git', credentialsId: 'd14d6c1c-44fa-xxxx-xxxx-xxxxxxxxxxxx']]])
      }
    }
  }
}
云账号类型凭据
pipeline {
    agent any
    stages {
        stage('获取云 API 密钥') {
            steps {
                withCredentials([cloudApi(credentialsId: '此处填写您上传凭据后所生成的凭据 ID', secretIdVariable: 'CLOUD_API_SECRET_ID', secretKeyVariable: 'CLOUD_API_SECRET_KEY')]) {
                       sh 'CLOUD_API_SECRET_ID=${CLOUD_API_SECRET_ID}'
                       sh 'CLOUD_API_SECRET_KEY=${CLOUD_API_SECRET_KEY}'
                 }
                 withCredentials([[$class: 'CloudApiCredentialsBinding', credentialsId: '此处填写您上传凭据后所生成的凭据 ID', secretIdVariable: 'CLOUD_API_SECRET_ID', secretKeyVariable: 'CLOUD_API_SECRET_KEY']]) {
                       sh 'CLOUD_API_SECRET_ID=${CLOUD_API_SECRET_ID}'
                       sh 'CLOUD_API_SECRET_KEY=${CLOUD_API_SECRET_KEY}'
                 }
            }
        }
    }
}
2024-12-17最近更新在阅读中是否遇到以下问题?*
您希望我们如何改进?*
如果您希望得到回复,请留下您的邮箱地址。