刚开始查看 Jenkins 声明式管道并在 docker 容器中运行我的构建。我有一个通过 git 引入 npm 包的项目,因此需要设置 ssh key 。
根据我遇到的情况,我可以设置构建参数,例如 --build-arg ssh_pub_key="$(cat ~/.ssh/id_rsa.pub)"
然后在我的 Dockerfile
ARG ssh_pub_key
我在我的 Jenkinsfile 中采用了以下方法
pipeline {
agent {
dockerfile {
args '''--build-arg ssh_prv_key="$(cat /var/lib/jenkins-git/.ssh/id_rsa)"'''
}
}
stages {
stage('Test') {
steps {
sh 'echo $ssh_prv_key'
}
}
}
}
在 Jenkins 中运行构建时,我在构建镜像时得到以下输出(未提及
--build-arg
。)docker build -t 085eb412f6dd28c1a7843aa9f9ed84e7c4af3e1b -f Dockerfile .
变量没有
我没有正确设置它们吗?有没有人以不同的方式处理 key 的复制?
谢谢
更新
我的 Jenkinsfile 现在如下所示,但不会像 get 一样运行
Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node
似乎我不能在管道声明之外运行任何脚本?
def ssh_prv_key = sh script: 'cat /var/lib/jenkins-git/.ssh/id_rsa', returnStdout: true
def ssh_pub_key = sh script: 'cat /var/lib/jenkins-git/.ssh/id_rsa.pub', returnStdout: true
pipeline {
agent {
dockerfile {
args """--build-arg ssh_prv_key=\"${ssh_prv_key}\" --build-arg ssh_pub_key=\"${ssh_pub_key}\" """
}
}
stages {
stage('Test') {
steps {
sh 'echo $ssh_prv_key'
}
}
}
}
最佳答案
这里$(cat /var/lib/jenkins-git/.ssh/id_rsa)
是一个shell命令。
AFAIK,必须在管道线之外声明绑定(bind)才能在定义代理时使用它们。
因此,使管道作业参数化。
ssh_prv_key
作为凭据参数。 Secretfile
ssh_pub_key
重复步骤然后使用
ssh_prv_key
在 dockerfile
additionalBuildArgs
指示。 pipeline {
agent {
dockerfile {
additionalBuildArgs ""--build-arg ssh_prv_key=\"$ssh_prv_key\" --build-arg ssh_pub_key=\"$ssh_pub_key\""
}
}
stages {
stage('Test') {
steps {
sh "echo $ssh_prv_key"
}
}
}
}
关于git - 声明式 Jenkins 管道和 Docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46401128/