jenkins从gitlab拉取代码

前置条件

1.jenkins安装了凭证插件Credentials Binding插件Git插件(Git相关的插件都可以装上,省事)
2.linux服务器安装了git

凭据类型

根据安装的插件,会有更多的凭据类型
但常用的凭证类型有:Username with password(用户密码)和SSH Username with private key(SSH
密钥),还有token也很好用

用户密码类型(http方式)

新建一个test项目,在源码管理中选择Git,从gitlab中将项目的http克隆连接填上

点击添加jenkins凭据,也可以在系统管理--凭据中添加好了之后直接选择
选择"Username with password",输入Gitlab的用户名和密码,点击"确定"。

选择刚刚添加的凭据,报错消失,可以正常使用,应用并保存

测试一下,回到项目中,点击立即构建,查看构建情况,确认到吗是否克隆到了jenkins的工作空间

#代码默认放在/var/lib/jenkins/workspace/下面,可以在jenkins上更改工作空间的位置
[root@jenkins ~]# cd /var/lib/jenkins/workspace/test
[root@jenkins test]# ls
404.html              deviceManager.html       form-validation.html  login.html               readme.md
alerts.html           dianfei.html             images-icons.html     media                    real-time.html
assets                efficiencyAnalysis.html  img                   media.html               sa.html
buttons.html          energy_consumption.html  index.html            messages.html            tables.html
calendar.html         file-manager.html        js                    mstp_105_SuperAdmin.iml  typography.html
charts.html           fonts                    keyInfo.html          mstp_map.html            userMng.html
components.html       form-components.html     labels.html           other-components.html
content-widgets.html  form-elements.html       LICENSE               profile-page.html
css                   form-examples.html       list-view.html        QHME.iml

ssh秘钥的方式

1.不使用jenkins凭据

在jenkins服务器上生成秘钥(公钥和私钥)
[root@jenkins ~]# ssh-keygen
......

[root@jenkins ~]# ls .ssh/
id_rsa  id_rsa.pub  known_hosts

[root@jenkins ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC78Uo/60/2D/vqWoKG6U7+uTlhzmWlu5alOMzT4UwR6z2rPh5HYLKn5LWVovZ4LyCcCo6KkukPYDTIv+WHCTyu6UM+BZqxUt3SJ8cpUN316L69dH6M7awFdGsHWKlISdqdlDNzIf/e8t6ILBK292nb5iqmR7A+HgINAWLdXa0DGO60gSp/jkPrjEKEv7nq7plz8pxhaFaZFyym/xtL7ysESMqDUb0bgVGkvLx6wfnHdG2lJjHs24a2h0z2mpCFUqGztOZkmYELE4qZYHqZHqDe00ShIR+7wb76UV0Bfuo2d+wUbv1Ggm+2jBYIuxFNPe/1XKoYgxuFEXjSjhRuGJ8n root@jenkins

将公钥中的内容拷贝到gitlab中

jenkins中不使用凭据,直接用ssh,正常构建成功

2.使用jenkins凭据的ssh

#生成秘钥
[root@jenkins ~]# ssh-keygen

把生成的公钥放在Gitlab中 以root账户登录->点击头像->Settings->SSH Keys
复制刚才id_rsa.pub文件的内容到这里,跟之前一样

唯一的区别是指jenkins中配置ssh凭证

#将私钥中的内容复制到jenkins
[root@jenkins ~]# cat .ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAu/FKP+tP9g/76lqChulO/rk5Yc5lpbuWpTjM0+FMEes9qz4e
R2Cyp+S1laL2eC8gnAqOipLpD2A0yL/lhwk8rulDPgWasVLd0ifHKVDd9ei+vXR+
jO2sBXRrB1ipSEnanZQzcyH/3vLeiCwStvdp2+YqpkewPh4CDQFi3V2tAxjutIEq
f45D64xChL+56u6Zc/KcYWhWmRcspv8bS+8rBEjKg1G9G4FRpLy8esH5x3RtpSYx
7NuGtodM9pqQhVKhs7TmZJmBCxOKmWB6mR6g3tNEoSEfu8G++lFdAX7qNnfsFG79
RoJvtowWCLsRTT3v9VyqGIMbhRF40o4UbhifJwIDAQABAoIBAG5Rmzjb4mrYRRHy
IO4aIxFoHiOoKIhZDXtcVPcGJ0XZZLCQ3Ose+p+Mo337U1uGpkiR9cunooAxA6rW
4V8bb/Bn4d2VIpF1x9klGkXJPxCqltPUfrgwhqoJBQ4h3/QbzeRzaqPpUC1JOJCL
9kxJ/GxktaI3MF+185Q4vRSdeQH4bWkVPesLMgcrFJ6o6qTInrb8aVIIRbvsBY7l
eoK4OZM9/w96tGakEpWq+Uej1DQtNIQlvWjc1oZoxJmTbPEtsptzXSs25+tpG19S
jFeDM0GPP667zcOZm3Ty0hS5BcZCwMlb32+S3qX34Zy9hhobRWITlWzeIGD92QSf
yR3HrTECgYEA7BNkWRzujQaMEdADsvvoY+dk7/Q0gI41VTuRHjXLQhG0VDOonGXX
OT8232TY0gxjsrXJ5PxgMhYqMZNyOfSdvKNq+daO1WZ5x8nCn3muld7G5Ds4slBK
eqYBvovqmWVgbrEUPrtz1jKgBxYkxy2jKAvuOIjuPAL00wwKRqKOMQkCgYEAy83w
5ZZInEepqRWyWV/Mwy1bYcH+Kty08GAW+3RrVvJiFYCASeL8i686C/9IthF5Wdcn
2eAdvilsEROv1n9+R/A05BsuT+z4bPgT6agCNxbYEaV4cExwbO0PtPtbuIIwpO+w
9RooaWw9uk8SxFU+6M3C6JIX5WB9pYNFNGuUyq8CgYAeZSjIaHc2ZR9ns7amNtTm
+AYA9xpDawvm23KFxztSWGE1bilJabtaPY9dF+jqsnmrqshB2VSxXjU0iN1uJNai
URAW9s8RHs6TMKEEU3BtD2/gNtfH7ZqJTBJxbCZadbkCx33pzczOSVpdTzaRBok5
QL/9O3Qg+eh3u9ce3RMdsQKBgEfwKJERrA2l6ggqBWswuplGQFRQYoQEpcWXVWVq
4ZyuL9sBIcdy3Mv9jRLBCIxjvJ7uqD1qs3T1MBUTCuU+2XdRHYpK4d0Ke2ITawv1
5tpFMP2Bl/KjuKdqQjoS43xMUBlYFfFeS1dzApLp6Gju4ibQwnTPZKntlkw++sWX
F803AoGAGxhzfDbyN+VfGRaFzPad7wcMsV4DlcMfFqY+He9sZUWYDx7kKR0afLlX
I2F6Tss00TeEJWKQKWovCtOaIs+1QjtT7AhwcLPuYjSTvaQTdNYqvc1zG3hHo7tX
lnFs+QferzqHIGCkmmfPWsAuF0EdMhmi7sN9dCZ6SpFWxtKCmYA=
-----END RSA PRIVATE KEY-----


posted @ 2021-04-08 14:02  EverEternity  阅读(1880)  评论(0编辑  收藏  举报