我有一个项目正在使用多分支管道插件在 Jenkins 上构建。我正在使用声明性管道语法,我的 Jenkinsfile 看起来像这样:
pipeline {
agent { label 'blah' }
options {
timeout(time: 2, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '5'))
}
triggers { pollSCM('H/5 * * * *') }
stages {
stage('Prepare') {
steps {
sh '''
echo "Building environment"
python3 -m venv venv && \
pip install git+ssh://git@my_private_repo.git
'''
}
}
}
}
当构建在 Jenkins 盒子上运行时,构建失败,当我检查控制台输出时,pip install 命令失败并出现错误:
Permission denied (publickey).
fatal: Could not read from remote repository.
我猜测我需要将所需的 ssh key 设置到 jenkins 构建环境中,但不确定如何执行此操作。
最佳答案
您需要安装SSH Agent plugin并使用它将操作包装在 steps
指令中,以便能够从私有(private)存储库中提取。您可以使用 sshagent 指令启用 SSH 代理,您需要在其中传递一个参数,该参数表示具有 git 存储库读取权限的有效 key 的哈希值。该 key 需要在 Jenkins 的全局凭证 View 中可用(Jenkins -> Credentials [在左侧菜单上],搜索右侧 key 的 ID 字段),例如:
stage('Prepare') {
steps {
sshagent(['<hash_for_your_key>']) {
echo "Building environment"
sh "python3.5 -m venv venv"
sh "venv/bin/python3.5 venv/bin/pip install git+ssh://git@my_private_repo.git
}
}
注意:由于 steps
指令下的操作作为子进程执行,因此您需要使用长语法从虚拟环境中显式调用可执行文件。
关于python - 使用多分支管道 Jenkinsfile 时构建环境中的 SSH key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44496868/