git - 声明式 Jenkins 管道和 Docker

标签 git docker jenkins ssh

刚开始查看 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
  • 设置上传 key 文件的默认值
  • ssh_pub_key 重复步骤

  • Parameterized Pipeline

    然后使用 ssh_prv_keydockerfile 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/

    相关文章:

    docker - 如何访问在 Ubuntu Docker 容器中运行的 Jenkins 的前端?

    git - 为什么 Git 不跟踪子目录中的更改?

    git - 使用 git 状态信息增强 "ls"?

    docker - 无法监听来自远程Docker容器的docker容器中运行的服务

    hadoop - Docker 容器独立运行但在 kubernetes 中失败

    docker - 装入的LVM卷未传递到容器

    git - 多个 Git 修复提交

    git - 无法使用可用的 ssh-key 推送到 github

    jenkins - 如何读取 Jenkins 管道中的子文件夹

    python - Django、Jenkins 和 PyLint 关注一切