我正在拉取一个公共 git 存储库,并尝试使用 denpal(SSH 私钥)凭据将我的更改推回到存储库中。
stages {
stage('Git clone') {
steps {
git branch: 'feature/Jenkinsfile',
credentialsId: 'denpal',
url: 'git@github.com:test/denpal.git'
}
}
stage('Test Git') {
steps {
withCredentials([sshUserPrivateKey(credentialsId: 'denpal', keyFileVariable: 'SSH_KEY')]) {
sh '''
git commit --allow-empty -m "test withCredentials"
git push origin feature/Jenkinsfile
'''
}
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这会产生以下错误:
> git --version # timeout=10
using GIT_SSH to set credentials denpal
> git fetch --tags --force --progress git@github.com:test/denpal.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/feature/Jenkinsfile^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/feature/Jenkinsfile^{commit} # timeout=10
Checking out Revision ... (refs/remotes/origin/feature/Jenkinsfile)
> git config core.sparsecheckout # timeout=10
> git checkout -f ...
> git branch -a -v --no-abbrev # timeout=10
> git branch -D feature/Jenkinsfile # timeout=10
> git checkout -b feature/Jenkinsfile ...
Commit message: "empty"
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test Git)
[Pipeline] withCredentials
Masking only exact matches of $SSH_KEY
[Pipeline] {
[Pipeline] sh
+ git commit --allow-empty -m test withCredentials
[feature/Jenkinsfile 3ff21fc] test withCredentials
+ git push origin feature/Jenkinsfile
ERROR: Permission to test/denpal.git denied to technology-labs.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
[Pipeline] }
[Pipeline] // withCredentials
Run Code Online (Sandbox Code Playgroud)
我也尝试过这个,但这也失败了:
withCredentials([sshUserPrivateKey(credentialsId: 'denpal', keyFileVariable: 'private_key', passphraseVariable: '', usernameVariable: 'git')]){
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
Rob*_*rek 10
andwithCredentials()行将sshUserPrivateKey私钥写入临时文件并将位置分配给,但未使用它,因为之后$SSH_KEY没有引用。$SSH_KEY
$GIT_SSH_COMMAND您可以使用(文档)告诉 ssh 和 git 有关私钥文件的信息。
代替:
git push origin feature/Jenkinsfile
Run Code Online (Sandbox Code Playgroud)
和:
GIT_SSH_COMMAND="ssh -i $SSH_KEY" git push origin feature/Jenkinsfile
Run Code Online (Sandbox Code Playgroud)
shu*_*ckc 10
对上面 Rob 的答案进行了更全面的描述,让 git checkout 使用 Jenkins 声明性管道语法的子模块。
#!/usr/bin/env groovy
pipeline {
agent any
stages {
stage ('Clone') {
steps {
checkout scm
withCredentials([sshUserPrivateKey(credentialsId: 'bitbucket_ssh', keyFileVariable: 'SSH_KEY')]) {
sh 'GIT_SSH_COMMAND="ssh -i $SSH_KEY" git submodule update --init'
}
}
}
...
}
}
Run Code Online (Sandbox Code Playgroud)
这是https://issues.jenkins.io/browse/JENKINS-20941的解决方法
| 归档时间: |
|
| 查看次数: |
12523 次 |
| 最近记录: |