声明式Jenkinspipe道和Docker

刚开始看Jenkins声明式pipe道并在Docker容器中运行我的构build。 我有一个项目,通过git拉npm包,因此需要设置ssh密钥。

从我遇到过,我可以设置构build参数,如--build-arg ssh_pub_key="$(cat ~/.ssh/id_rsa.pub)" ,然后在我的Dockerfile

 ARG ssh_pub_key 

我在Jenkins文件中采取了以下方法

 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时,我在构build图像时得到了下面的输出(没有提到--build-arg

 docker build -t 085eb412f6dd28c1a7843aa9f9ed84e7c4af3e1b -f Dockerfile . 

并没有为variables

我没有正确设置它们吗? 有没有人以不同的方式处理密钥的复制?

谢谢

UPDATE

我的Jenkinsfile现在看起来像下面但不会运行得到

 Required context class hudson.FilePath is missing Perhaps you forgot to surround the code with a step that provides this, such as: node 

似乎我不能在pipe道声明之外运行任何脚本?

 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,绑定必须在stream水线之外声明,以在定义代理时使用它们。

因此,使pipe道工作参数化。

  • 添加ssh_prv_key作为凭证参数
  • selectSecretfile
  • 将默认值设置为上传密文件
  • 重复ssh_pub_key步骤

参数化管道

然后在dockerfile additionalBuildArgs指令中使用ssh_prv_key

  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" } } } }