Jenkins自由风格

image-20230825164146206

image-20230825164230805

image-20230825164314291

image-20230825164519842

jenkins想要去gitlab中拉代码条件

  • jenkins能识别gitlab的域名
  • jenkins要将公钥放入gitlab中
  • jenkins机器上要有git命令

image-20230825110129149

报错原因:jenkins服务器上没有git命令

##安装git
[root@jenkins ~]# yum install -y git

image-20230825200856113

报错原因:没有将jenkins公钥放入gitlab

## 给jenkins用户创建密钥对
[root@jenkins jenkins]# usermod jenkins -s /bin/bash
[root@jenkins jenkins]# su - jenkins 
-bash-4.2$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/jenkins/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /var/lib/jenkins/.ssh/id_rsa.
Your public key has been saved in /var/lib/jenkins/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:6vktU9xu09vdNdUQn9IOQ1rH8ZrUscs1PPvUd5wXTcM jenkins@jenkins
The key's randomart image is:
+---[RSA 2048]----+
|             o+=o|
|            + +EB|
|           . +oO*|
|             .*=@|
|        S. .  o*X|
|       .  o .  .*|
|      .  . . . .o|
|     . .o.  + ..=|
|      o..o.. ...+|
+----[SHA256]-----+

## 查看公钥
-bash-4.2$ cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDD46Zx+0BCuclJ20AscVPwEqOzEnwJEUITnu+hglPLsoE2eUq6QS/79NHe9v0aJC7HdEBOPOaH2Q80j+5DEraE0orJt9iLsDWjQsX422YZ0RfJ/3E3LkWLZQsRlvOGcWivCmWPBg/t0aZxOJl5vFq2J4PWt0JeGp9YxbznH7KABG2ar7/4rBGOP1eWGsMUBVSluIIZB+kDvBclZGC6e+fi5v434XMkMshiKuF9ESomroLxzn767bllAjiCaAKYLyeD5/aZN5mUU1IZOXVRSb5ElEqJqMVzInAn4rChRjsqlWZ2O0GkvFRYZWeFhXENRPP/SPr+6S6xnj3vis7tDOH/ jenkins@jenkins

image-20230825201957402

image-20230825202058876

image-20230825202228411

image-20230825202244807

image-20230825202314974

image-20230825202347324

报错原因:jenkins放入公钥和私钥都不好使

-bash-4.2$ git ls-remote -h git@gitlab.wodeyumengouwo.com:root/root.git
The authenticity of host 'gitlab.wodeyumengouwo.com (10.0.0.91)' can't be established.
ECDSA key fingerprint is SHA256:5KRH49USplFqTLegk1cdAiaJ7C1EJR+PjWAswdkRE8E.
ECDSA key fingerprint is MD5:b3:4f:97:67:42:72:0d:12:d2:69:3c:86:9e:2c:fd:fa.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.wodeyumengouwo.com,10.0.0.91' (ECDSA) to the list of known hosts.
e9771b8fd1290b3e0e56a48eff240c7cf4884954        refs/heads/ceo_branch
bda9898a6848feb6319fc8f58452f07bd3944ba4        refs/heads/master
725b734a607c37cde7c24ce0d16141770da3673e        refs/heads/mishu_branch

image-20230825202528631

image-20230825202610743

image-20230825202723169

image-20230825202822231

image-20230825203156455

echo "-------------------------华丽的分割线------------------"
code_dir='/www/code'

cd $WORKSPACE &&\
tar zcf web_${GIT_COMMIT}.tar.gz ./* && \
for ip in 7;do
    scp web_${GIT_COMMIT}.tar.gz root@172.16.1.${ip}:${code_dir} 
    ssh root@172.16.1.${ip} "cd ${code_dir} && tar xf web_${GIT_COMMIT}.tar.gz"
done

echo "git_commit: $GIT_COMMIT"
echo "job_name: $JOB_NAME"
echo "workspace: $WORKSPACE"
##免密
-bash-4.2$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/jenkins/.ssh/id_rsa.pub"
The authenticity of host '172.16.1.7 (172.16.1.7)' can't be established.
ECDSA key fingerprint is SHA256:5KRH49USplFqTLegk1cdAiaJ7C1EJR+PjWAswdkRE8E.
ECDSA key fingerprint is MD5:b3:4f:97:67:42:72:0d:12:d2:69:3c:86:9e:2c:fd:fa.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.1.7's password: 

Number of key(s) added: 1

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

image-20230825203555843

image-20230825203735832

image-20230825203747387

参数化构建

git tag -a '标签' -m 描述

image-20230825203856742

image-20230825203915244

image-20230825204056309

image-20230825204128536

image-20230825211453803

image-20230825211806553

image-20230825212751313

image-20230825212818002

image-20230825212843267

image-20230825212902149

image-20230827173349224

image-20230827173459806

image-20230827173604410

image-20230827173643286

image-20230827173709869

image-20230827173903578

image-20230827174045229

image-20230827175000874

echo "-------------------------华丽的分割线------------------"
code_dir='/www/code'

cd $WORKSPACE &&\
tar zcf web_${GIT_COMMIT}.tar.gz ./*

if [ $env == 'test' ];then
    echo $env
    scp web_${GIT_COMMIT}.tar.gz root@172.16.1.${ip}:${code_dir} 
    ssh root@172.16.1.${ip} "cd ${code_dir} && tar xf web_${GIT_COMMIT}.tar.gz"
elif [ $env == 'dev' ];then
    echo $env
else
    echo $env
fi

echo "git_commit: $GIT_COMMIT"
echo "job_name: $JOB_NAME"
echo "workspace: $WORKSPACE"

image-20230827180748466

image-20230827180809924

  • 开发环境所以没变

image-20230827185212755