使用凭据检查 Jenkins Pipeline Git SCM?
Checkout Jenkins Pipeline Git SCM with credentials?
我正在关注 this tutorial:
node {
git url: 'https://github.com/joe_user/simple-maven-project-with-tests.git'
...
}
但是它没有说明如何添加凭据。 Jenkins 确实有特定的 "Credentials" 部分,您可以在其中定义用户 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 文件?
在理想情况下,我希望有一个用于管道作业和回购密钥的存储库,然后启动 Docker Jenkins,并在那里动态添加这些作业和密钥,而无需在 Jenkins 控制台中进行任何配置。
您可以在管道中使用以下内容:
git branch: 'master',
credentialsId: '12345-1234-4696-af25-123455',
url: 'ssh://git@bitbucket.org:company/repo.git'
如果您使用的是 ssh url,那么您的凭据必须是用户名 + 私钥。如果您使用的是 https 克隆 url 而不是 ssh 克隆,那么您的凭据应该是用户名 + 密码。
如果您想使用 ssh 凭据,
git(
url: 'git@github.com<repo_name>.git',
credentialsId: 'xpc',
branch: "${branch}"
)
如果你想使用用户名和密码凭证,你需要像@Serban 提到的那样使用 http 克隆。
git(
url: 'https://github.com/<repo_name>.git',
credentialsId: 'xpc',
branch: "${branch}"
)
使用特定凭据显式结账
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 文件中使用这两个阶段。
对于值得加入讨论的内容...我所做的最终对我有所帮助...因为管道 运行 在已清理的 docker 图像中的工作区内每次 运行 秒。我获取了在我的管道中对存储库执行必要操作所需的凭据,并将它们存储在 .netrc 文件中。这使我能够成功授权 git 回购操作。
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...
'''
}
添加一个使用 git 插件的简单示例 GitSCM:
checkout([
$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'CleanCheckout']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '<gitCredentials>', url: '<gitRepoURL>']]
])
在您的管道中
stage('checkout'){
steps{
script{
checkout
}
}
}
它用
帮我解决了
checkout scm: ([
$class: 'GitSCM',
userRemoteConfigs: [[credentialsId: '******',url: ${project_url}]],
branches: [[name: 'refs/tags/${project_tag}']]
])
我正在关注 this tutorial:
node {
git url: 'https://github.com/joe_user/simple-maven-project-with-tests.git'
...
}
但是它没有说明如何添加凭据。 Jenkins 确实有特定的 "Credentials" 部分,您可以在其中定义用户 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 文件?
在理想情况下,我希望有一个用于管道作业和回购密钥的存储库,然后启动 Docker Jenkins,并在那里动态添加这些作业和密钥,而无需在 Jenkins 控制台中进行任何配置。
您可以在管道中使用以下内容:
git branch: 'master',
credentialsId: '12345-1234-4696-af25-123455',
url: 'ssh://git@bitbucket.org:company/repo.git'
如果您使用的是 ssh url,那么您的凭据必须是用户名 + 私钥。如果您使用的是 https 克隆 url 而不是 ssh 克隆,那么您的凭据应该是用户名 + 密码。
如果您想使用 ssh 凭据,
git(
url: 'git@github.com<repo_name>.git',
credentialsId: 'xpc',
branch: "${branch}"
)
如果你想使用用户名和密码凭证,你需要像@Serban 提到的那样使用 http 克隆。
git(
url: 'https://github.com/<repo_name>.git',
credentialsId: 'xpc',
branch: "${branch}"
)
使用特定凭据显式结账
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 文件中使用这两个阶段。
对于值得加入讨论的内容...我所做的最终对我有所帮助...因为管道 运行 在已清理的 docker 图像中的工作区内每次 运行 秒。我获取了在我的管道中对存储库执行必要操作所需的凭据,并将它们存储在 .netrc 文件中。这使我能够成功授权 git 回购操作。
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...
'''
}
添加一个使用 git 插件的简单示例 GitSCM:
checkout([
$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'CleanCheckout']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '<gitCredentials>', url: '<gitRepoURL>']]
])
在您的管道中
stage('checkout'){
steps{
script{
checkout
}
}
}
它用
帮我解决了checkout scm: ([
$class: 'GitSCM',
userRemoteConfigs: [[credentialsId: '******',url: ${project_url}]],
branches: [[name: 'refs/tags/${project_tag}']]
])