Jenkins配置SSH Key下载代码

2020-10-23 11:26:31 浏览数 (1)

在写这篇文章之前,我一直使用用户名和密码方式进行各种操作,因为比较方便省心,但是在使用的过程中,偶尔会出现异常问题,导致Git下载失败,所以建议大家都使用ssh key方式更新代码。

在Jenkins机器上生成ssh key

  • 执行生成命令
代码语言:javascript复制
ssh-keygen -t rsa
  • 查看秘钥,并拷贝公钥内容

在上一步生成的过程中会显示秘钥存储位置

因为我们采用的是docker部署Jenkins,所以默认登录用户是jenkins,拷贝公钥内容cat /var/jenkins_home/.ssh/id_rsa.pub

代码语言:javascript复制
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCw2pNxQiO2O8MEJoetY5EnPUeP1uXKSThgD5ESjcGvYoQayh77DnCqqlYPNlcU2VkgTGQNECCvE2HixS9eF6ScKiu005MkAuE0//tiBLWe9tqmD2ms8QTEFpSbgjPzfLNs2XjY3ChLFc5t6ib3yDudwQAyw57 Bxj7I94WpOngn0L ObI0LDwKxPI0UIQSZyuqBrKOSIA6eG3L9xQ7hFF5/r6phDj9Sls7j7L4ZnNynX65zJYsrYmMdcbQT6B39YCSQmYvf6iC4Xqa iU4NMhgUm5d3 J84Kxs3FWC/Aq4XsBeLa/ 9fSZqN0uJCXeQh3l1eGgWwxHQLsL7tZPtNMZ jenkins@fa2f55e38fca

添加公钥到gitee上

在Jenkins中新增ssh key凭据

“系统管理”->“Manage Credentials”,添加凭据

将第一步中生成的私钥内容拷贝cat /var/jenkins_home/.ssh/id_rsa

代码语言:javascript复制
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcnNhAAAAAwEAAQAAAQEAsNqTcUIjtjvDBCaHrWORJz1Hj9blykk4YA REo3Br2KEGsoe w5wqqpWDzZXFNlZIExkDRAgrxNh4sUvXheknCortNOTJALhNP/7YgS1nvbapg9prPEExBaUm4
Iz83yzbNl42NwoSxXObeom98g7nc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G5tSXu8kN4NLDBgkZNjpBvDKjdx3fNhiqZnAAAAgQDXQI2OpNAnPlYKOhPUZG4OgQF2Yb8hRDH1FEkspfme4zHk4yCXkCH zV1GT8XNsyznmEaNhEkUtdhfYuRAfVOEAakrsq/opFeJq6XzRgnXeX3uEohCnSKmVOIP/7
vyvT0Ydx1ib/l2ndAhIUEuJVOqbH51glRSw3pu3KQbA8ApGwAAAIEA0lUuP8zdmjoxASACeYVE/JrC7AxO/ojVAgK3vm fJ2mWAg icwtKDkD4rNQMkicxS/myGU2Qf2/U1ucn6TaT0TF8 npOS7c9s9abn8cHE f7dmxH4bdU7gRJ lrbFO0Fz8Ix0CjUxYpfaoMK5p97aa7mT3qDavSyLlnoAowAi9sAAAAUamVua2luc0BmYTJmNTVlMzhmY2EBAgMEBQYH-----END OPENSSH PRIVATE KEY-----

将内容添加到上图的private key中

添加测试工程

新建一个自由风格的工程,填写ssh git路径

启动构建,查看控制台日志输出

我们在Jenkins执行目录下查看代码是否下载成功

0 人点赞