结帐Jenkins Pipeline Git SCM凭证?

Ren*_*der 79 git ssh jenkins jenkins-pipeline

我正在学习本教程:

node {
  git url: 'https://github.com/joe_user/simple-maven-project-with-tests.git'
  ...
}
Run Code Online (Sandbox Code Playgroud)

但是,它没有说明如何添加凭据.Jenkins确实有特定的"凭据"部分,您可以在其中定义用户user&pass,然后获取要在作业中使用的ID,但是如何在管道指令中使用它?

我尝试过:

git([url: 'git@bitbucket.org:company/repo.git', branch: 'master', credentialsId: '12345-1234-4696-af25-123455'])
Run Code Online (Sandbox Code Playgroud)

没运气:

stderr: Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Run Code Online (Sandbox Code Playgroud)

有没有办法在管道中配置信用卡,还是我必须将SSH密钥放到Jenkin的Linux用户的.ssh/authorized_keys文件中?

在理想的世界中,我想拥有一个管道作业和repo-keys的存储库,然后启动Docker Jenkins,并在那里动态添加这些作业和密钥,而无需在Jenkins控制台中进行任何配置.

Ser*_*tin 121

您可以在管道中使用以下内容:

git branch: 'master',
    credentialsId: '12345-1234-4696-af25-123455',
    url: 'ssh://git@bitbucket.org:company/repo.git'
Run Code Online (Sandbox Code Playgroud)

如果您使用的是ssh网址,那么您的凭据必须是用户名+私钥.如果您使用的是https克隆网址而不是ssh网址,则您的凭据应为用户名+密码.

  • @prayagupd,您应该能够从凭证页面(`http:// yourjenkinsinstall/credentials`)获取凭证ID.无需搜索配置文件. (15认同)
  • 它很有用,但`credentialsId`来自[`/var/lib/jenkins/credentials.xml`](/sf/answers/2492223401/)中的id,因为我不得不努力弄明白. (3认同)
  • 您知道是否可以重复使用作业中定义的凭据? (3认同)
  • 对于那些询问“如何生成凭据ID”的人。在这里如何找到它。[1。单击Jenkins主页上的凭据2。然后,您将看到一个包含您创建的所有凭据的表。3. ID在此表中] (3认同)
  • 这解决了它,谢谢。我不知道 SSH-url 和 HTTPS-url 需要不同的凭据才能使用! (2认同)

f-s*_*ety 22

如果要使用ssh凭据,

  git(
       url: 'git@github.com<repo_name>.git',
       credentialsId: 'xpc',
       branch: "${branch}"
    )
Run Code Online (Sandbox Code Playgroud)

如果要使用用户名和密码凭据,则需要使用http clone作为@Serban提及.

    git(
       url: 'https://github.com/<repo_name>.git',
       credentialsId: 'xpc',
       branch: "${branch}"
    )
Run Code Online (Sandbox Code Playgroud)

  • 如何生成此凭证? (10认同)

Upu*_*era 17

使用特定凭据明确签出

    stage('Checkout external proj') {
        steps {
            git branch: 'my_specific_branch',
                credentialsId: 'my_cred_id',
                url: 'ssh://git@test.com/proj/test_proj.git'

            sh "ls -lat"
        }
    }
Run Code Online (Sandbox Code Playgroud)

要根据当前Jenkins作业中的配置凭据进行结帐

    stage('Checkout code') {
        steps {
            checkout scm
        }
    }
Run Code Online (Sandbox Code Playgroud)

您可以在单个Jenkins文件中使用这两个阶段.

  • 如何生成此certificateId? (2认同)
  • 看看 - https://support.cloudbees.com/hc/en-us/articles/203802500-Injecting-Secrets-into-Jenkins-Build-Jobs (2认同)

小智 6

对于值得添加到讨论中的内容...我所做的最终帮助了我...因为管道是在 docker 映像内的工作区中运行的,每次运行时都会清理该映像。我获取了在管道内的存储库上执行必要操作所需的凭据,并将其存储在 .netrc 文件中。这使我能够成功授权 git repo 操作。

withCredentials([usernamePassword(credentialsId: '<credentials-id>', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
    sh '''
        printf "machine github.com\nlogin $GIT_USERNAME\n password $GIT_PASSWORD" >> ~/.netrc
        // continue script as necessary working with git repo...
    '''
}
Run Code Online (Sandbox Code Playgroud)


avi*_*amg 5

使用git插件GitSCM为您添加一个快速示例:

    checkout([
        $class: 'GitSCM', 
        branches: [[name: '*/master']], 
        doGenerateSubmoduleConfigurations: false, 
        extensions: [[$class: 'CleanCheckout']], 
        submoduleCfg: [], 
        userRemoteConfigs: [[credentialsId: '<gitCredentials>', url: '<gitRepoURL>']]
    ])
Run Code Online (Sandbox Code Playgroud)

在您的管道中

stage('checkout'){
    steps{
        script{
            checkout
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我广泛搜索了一个像这样的简单的“结帐”示例,谢谢。 (2认同)

Sar*_*ang 5

它为我解决了使用

checkout scm: ([
                    $class: 'GitSCM',
                    userRemoteConfigs: [[credentialsId: '******',url: ${project_url}]],
                    branches: [[name: 'refs/tags/${project_tag}']]
            ])
Run Code Online (Sandbox Code Playgroud)