赞
踩
使用 SSH 密钥方式拉取 Gitee 上的代码,我们只需要在服务器上使用 root 用户生成公钥和私钥,将公钥配置到 Gitee 中,再将私钥配置到 Jenkins 中。这样 Jenkins 就可以免密从 Gitee 上拉取代码了。
删除存在的密钥是防止跟着此流程走出一些未知的错
root@bpvank-virtual-machine:~# cd /root/.ssh
root@bpvank-virtual-machine:~/.ssh# ls
id_rsa id_rsa.pub
root@bpvank-virtual-machine:~/.ssh# rm id_rsa
root@bpvank-virtual-machine:~/.ssh# rm id_rsa.pub
root@bpvank-virtual-machine:~/.ssh# ls
ssh-keygen -t rsa
生成公钥和私钥,输入命令后一直按回车即可root@bpvank-virtual-machine:~/.ssh# ssh-keygen -t rsa
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:DrGdLSo+dVV9Q0doMHWw6FINU9rdH3psE0iol6FqVvw root@bpvank-virtual-machine
The key's randomart image is:
+---[RSA 2048]----+
| **==+|
| ooO==+|
| . . o.*o+++|
| + B.= o +|
| o S.= .. =.|
| .B.. E o .|
| ..+.. |
| ... |
| .. |
+----[SHA256]-----+
root@bpvank-virtual-machine:~/.ssh# ls
id_rsa id_rsa.pub
root@bpvank-virtual-machine:~/.ssh# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDbXXDH792Z1Z382EQingiZWHjXWuds++v16HIT1uzA7dmpR41dGgmg9trGlJ/sbk8DPU83aAZZSy5kp9TaLUnFfxGOQU3NNMe78kRt2eOTaAnzoqhA8ZwyuXIgQVu7234N5HyLhmv0umzH/N+2Sdg0H9orBm+55BcrVzbsKSzPatZrGRlgNyOc2O/CLak0nn4/ivAglBgZJeODwEaTQAEOcdfdQpTvsJO+EdjOlRcfWWa4ylQl3zRBzSLklzfONo4y4AnCvDNuCTLzbSWnnG9Ti0v0b0aYwvqliFqGp67/EpQyjKfctoPgkGXMm5rfbhgo9Nmhi0++6Wi2kzLGu4Fh root@bpvank-virtual-machine
root@bpvank-virtual-machine:~/.ssh# cat id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA211wx+/dmdWd/NhEIp4ImVh411rnbPvr9ehyE9bswO3ZqUeN
XRoJoPbaxpSf7G5PAz1PN2gGWUsuZKfU2i1JxX8RjkFNzTTHu/JEbdnjk2gJ86Ko
......
LyKnjUsIBjC4uk98sJ29KwKBgDR8bjDXBwo3BusyZnWBGr33iZ0AC8GBMF1gV0ce
E8zAcEmOapDMQ12ap65b3lg4Rxj8gxKo8W4KBrbRwFaQ1iukNx4qg/m5MDd4IwnQ
0J2wXpZyN+at4ez5QJFn6a2JiKIVHmnNNuMFiB82jkrvi84B7HsH
-----END RSA PRIVATE KEY-----
注意:复制私钥时头尾也需要复制添加进去!!!
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
# 1、查看git仓库目录
root@bpvank-virtual-machine:~/.ssh# find ~ -name ".git" -type d
/root/.jenkins/workspace/java-project-01/.git
/root/.jenkins/workspace/java-project-02/.git
# 2、进入刚刚创建项目的git仓库目录
root@bpvank-virtual-machine:~/.ssh# cd /root/.jenkins/workspace/java-project-02/.git
# 3、ls查看
root@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02/.git# ls
branches config description FETCH_HEAD HEAD hooks info objects refs
git remote -v
查看你当前的remote urlroot@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02/.git# git remote -v
origin https://github.com/test1/test1.git (fetch)
origin https://github.com/test1/test1.git (push)
git remote set-url
来修改你的remote url,不用https协议,改用git 协议git remote set-url origin git@gitee.com:bpvank/jenkins-testing.git
git remote -v
查看remote urlroot@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02/.git# git remote -v
origin git@gitee.com:bpvank/jenkins-testing.git (fetch)
origin git@gitee.com:bpvank/jenkins-testing.git (push)
/usr/bin/git ls-remote -h git地址
root@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02/.git# /usr/bin/git ls-remote -h git@gitee.com:bpvank/jenkins-testing.git
The authenticity of host 'gitee.com (212.64.63.215)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,212.64.63.215' (ECDSA) to the list of known hosts.
3b671ae7df7898d9ad4315bd1354a5f00ab8c158 refs/heads/master
解释:这个命令通常用于在本地git仓库中创建一个与远程git仓库的关联,或者更新本地git仓库中远程引用的最新状态信息。
root@bpvank-virtual-machine:~# cd /root/.jenkins/workspace/java-project-02
root@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02# ls
pom.xml README.md springmvctest.iml src
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。