Docker:pipeline编写基本技巧- jenkins配置通过免交互方式拉取git源码管理仓库的代码

时间:2023-12-12 20:12:56

工作中,从git仓库拉取代码有2种方式:交互式和非交互式

什么是交互式?就是拉取需要权限才能访问的代码时,需要输入密码

Docker:pipeline编写基本技巧- jenkins配置通过免交互方式拉取git源码管理仓库的代码

免交互式呢? 是通过密钥,私钥的方式,让服务端信任客户端,产生信任后,任何一次客户端向服务端发起的请求时,不需要密码,而是直接进行读写等操作。

在持续集成中,jenkins需要去git代码仓库拉取代码,在这个过程中不方便交互式的输入代码,所以采用免交互式会比较方便。

密钥私钥的生成和使用:

在客户机linux机器上 ssh-keygen 一路回车

[root@ ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:llzcheb6UtbggaOnU6FTjssrLTIM5CQ5FjsnBq2zo5I root@192.168.1.40
The key's randomart image is:
+---[RSA ]----+
| . .. |
|... . .o. |
|.oo o+. |
|**o. . o= + |
|oO+ S* = + |
|o o .= = + . |
|.o o o * + |
|E + o * . . |
|. o o.o . |
+----[SHA256]-----+
[root@ ~]# ls .ssh/
id_rsa id_rsa.pub known_hosts

#通过ssh-copy-id命令将/root/.ssh/id_rsa.pub公钥copy到192.168.1.30git源码管理服务器上

[root@192 ~]# ssh-copy-id git@192.168.1.30
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
git@192.168.1.30's password:


Number of key(s) added: 1


Now try logging into the machine, with: "ssh 'git@192.168.1.30'"
and check to make sure that only the key(s) you wanted were added.

 

到192.168.1.30git源码管理服务器上,查看id_rsa.pub存放到了哪里

[root@ ~]# su - git
[git@ ~]$ cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPW1GD64WQr/1WdklohJrS35i3MS7Om2m9vXlDW/ZuvuucYiSzEiI9Thr3ooPBlavLRofh/oS3xzu+jHydm9hY20IV73Kmua3BWw3eMLUT6PXs2fvpGpspx9doVmJZ6B4IGN/4VYoFUPTYpy+WxxcqkxPeMQrZ5mcemzlUkB6GcSmNEI2U0lP4z7h+H0i+JvoVd7CF7FCAiYLxsBxLFdQnJxdfvybaQ+qt6bvShqg8Ijus4rveqFSO7NRamGX4KZmvIdgVQ1uMLT5PYuC2u6F+hLhCLg7XoqzqxGtl+4kIgGedQzxxsHzxa1D7x1Hx7SsTlb9xJKJRVE89P0/KOtnf root@192.168.1.40

将客户端(jenkins)服务器的私钥配置到jenkins管理界面里,

git代码仓库拉代码的pipeline脚本产生方法

Docker:pipeline编写基本技巧- jenkins配置通过免交互方式拉取git源码管理仓库的代码

下图是上图步骤3里,点开后的配置

Docker:pipeline编写基本技巧- jenkins配置通过免交互方式拉取git源码管理仓库的代码

下图是ssh-keygen产生的私钥 .ssh/id_rsa

Docker:pipeline编写基本技巧- jenkins配置通过免交互方式拉取git源码管理仓库的代码

到此,步骤5里,就产生了pipeline脚本,免交互方式让jenkins服务器拉取git代码仓库里的代码

作者: 梅梅~

出处: https://www.cnblogs.com/keeptesting

关于作者:专注软件测试,测试运维相关工作,请多多赐教!

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 欢迎沟通交流加微信联系。 微信:yangguangkg20140901 暗号:博客园.