我关注this tutorial:
node {
git url: 'https://github.com/joe_user/simple-maven-project-with-tests.git'
...
}
然而,它没有说明如何添加凭据。 Jenkins确实有特定的“凭据”部分,您可以在其中定义用户user& pass,然后获取要在作业中使用的ID,但如何在管道说明中使用它?
我尝试过:
git([url: 'git@bitbucket.org:company/repo.git', branch: 'master', credentialsId: '12345-1234-4696-af25-123455'])
没有运气:
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.
有没有办法在管道中配置信用卡,还是我必须将SSH密钥放到Jenkin的Linux用户的.ssh / authorized_keys文件中?
在理想世界中,我想拥有一个管道作业和repo-keys的存储库,然后启动Docker Jenkins,并在那里动态添加这些作业和密钥,而无需在Jenkins控制台中配置任何内容。
答案 0 :(得分:105)
您可以在管道中使用以下内容:
git branch: 'master',
credentialsId: '12345-1234-4696-af25-123455',
url: 'ssh://git@bitbucket.org:company/repo.git'
如果您正在使用ssh网址,那么您的凭据必须是用户名+私钥。如果您使用https克隆网址而不是ssh网址,则您的凭据应为用户名+密码。
答案 1 :(得分:12)
如果您想使用ssh凭证,
git(
url: 'git@github.com<repo_name>.git',
credentialsId: 'xpc',
branch: "${branch}"
)
如果您想使用用户名和密码凭据,则需要使用http clone作为@Serban提及。
git(
url: 'https://github.com/<repo_name>.git',
credentialsId: 'xpc',
branch: "${branch}"
)
答案 2 :(得分:9)
使用特定凭据明确结帐
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"
}
}
根据当前Jenkins作业中的配置凭据结帐
stage('Checkout code') {
steps {
checkout scm
}
}
您可以在单个Jenkins文件中使用这两个阶段。
答案 3 :(得分:2)
使用git插件GitSCM为您添加一个快速示例:
checkout([
$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'CleanCheckout']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '<gitCredentials>', url: '<gitRepoURL>']]
])
在您的管道中
stage('checkout'){
steps{
script{
checkout
}
}
}
答案 4 :(得分:1)
使用
为我解决了checkout scm: ([
$class: 'GitSCM',
userRemoteConfigs: [[credentialsId: '******',url: ${project_url}]],
branches: [[name: 'refs/tags/${project_tag}']]
])
答案 5 :(得分:0)
这对于我来说100%使用jenkins脚本
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"
}
}
答案 6 :(得分:0)
对于值得添加到讨论中的内容...我所做的最终帮助了我...由于管道是在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...
'''
}