其他分享
首页 > 其他分享> > Jenkinsfile pipline 使用 SSH 传递密钥给 Docker

Jenkinsfile pipline 使用 SSH 传递密钥给 Docker

作者:互联网

前提

首选你需要将用到的 SSH 私钥保存到 Jenkins 的凭据中,这样你会获得一个 credentialId。这不是本文要说的内容,故不在此展开赘述,详情可参考官方文档:https://www.jenkins.io/doc/book/using/using-credentials/。

只用一个 SSH Key

假若我们的 Jenkinsfile 里只用到了一个 SSH key,那么直接使用 Jenkins 的 SSH Agent 这个插件就好,在 Jenkinsfile 中具体写法为:

pipeline {
    agent {
        // ...
    }
    environment {
        // 为了构建镜像时使用 SSH,需要开启 Docker 的 Buildkit 功能
        DOCKER_BUILDKIT = "1"
    }
    stages {
        stage('Docker Build') {
            steps {
			// 启动 ssh agent 并将默认的 SSH 私钥凭据添加到其中
			sshagent(credentials: ['ssh-credentials-id']) {
					// 在这里就正常的使用 Docker 相关的传递 SSH 私钥构建
					// 详情可以参见这篇文章:https://www.cnblogs.com/lfkid/p/dockerfile-ssh-docker-build-ssh.html
                    sh 'docker build --ssh default .'
                }
            }
        }
    }
}

参考


image

标签:私钥,ssh,credentials,Docker,Jenkinsfile,pipline,SSH
来源: https://www.cnblogs.com/lfkid/p/jenkins-ssh-key-dockerfile.html