Jenkins 前端自动发布 [git 钩子]
Jenkins是一个插件平台,可以集成很多环境。很多时候我们是给前端人员配置的FTP提交代码,但是由于更新不方便。发布新的代码ftp会被清空,为节省前端开发时间,所以我们就需要Jenkins 配合git钩子使用
Jenkins 静态资源自动发布 [git 钩子]
Jenkins
一、Jenkins介绍及安装
安装和介绍都是老生常谈的内容了,说了N+1+2+3+4+5+.....的话题了,这里直接跳过了
Jenkins yum安装https://www.abcdocker.com/abcdocker/3174
Jenkins War安装https://www.abcdocker.com/abcdocker/2897
以前写过Jenkins 钩子的文章,很多就不具体说明了,可以查阅
持续集成+自动化部署[代码流水线管理及Jenkins和gitlab集成]
持续集成之Jenkins+Gitlab实现持续集成 [二]
Jenkins 自动化部署上线
二、Jenkins插件
1.Gitlab Hook #gitlab钩子,push代码后自动触发拉取操作
2.Publish Over SSH # scp拷贝插件 (主要是将包拷贝到web服务器上)
3.Build Authorization # 钩子触发器
需要提前安装好gitlab [Jenkins安装] [Git安装]
Gitlab插件安装 gitlab
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504020644476.png)
SSH 插件安装 Publish Over SSH
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100646375.png)
Build Authorization

三、配置Jenkins
我们需要新建一个node项目
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100648428.png)
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100648593.png)
设置部分一
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100649354.png)
源码管理配置部分二
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100650617.png)
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100651536.png)
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100651391.png)
构建触发器配置部分三
#生成令牌 [root@www.abcdocker.com]# openssl rand -hex 10 5c7e3ab062f63aaace06
下面的Build when a change is pushed to GitLab. GitLab webhook UR是我们把钩子插件安装上就有的我需要开启就可以了。 别忘记点保存,这里Jenkins目前就先结束了
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100652440.png)
接下来就是gitlab的配置,SSH插件是最后我们才用上,最后用来拷贝文件所使用~
三、配置Gitlab
1.登陆到gitlab项目中,点击Settings
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100653393.png)
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100654473.png)
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100654120.png)
钩子配置
##Jenkins地址填写 例子 http://192.168.10.100:81/buildByToken/build?job=short-video-operation-release&token=ddecf363ce1c55a56d72a55d http://Jenkins地址(ip+端口)/buildByToken/build?job=JenKins项目名称&token=Jenkins配置的令牌
我的地址如下,然后我们在gitlab上点击Add Web Hook
http://10.4.82.118/buildByToken/build?job=abcdocker-node&token=5c7e3ab062f63aaace06
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100655916.png)
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100656540.png)
添加完成后,我们点击Test Hook进行测试

四、SSH 插件配置
修改系统设置,配置ssh插件
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100657985.png)
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100658973.png)
有两种认证方式
第一种:用户名密码认证
第二种:ssh免密认证 (只需将私钥放在目录下)
[root@yzsjhl82-116 ~]# ssh-keygen [root@yzsjhl82-116 ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. 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: ce:df:f7:d8:f3:be:e9:5c:e5:18:bf:b7:cd:c2:44:a5 root@yzsjhl82-116.opi.com The key's randomart image is: +--[ RSA 2048]----+ | | | .| | o | | E | | S .. .| | o .=.| | o o. +| | . . ==*| | . ..o@&| +-----------------+
我这里使用用户名密码进行操作
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100658947.png)
我们要测试用户密码是否正确,然后别忘记点最下边的保存
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100659635.png)
返回配置Jenkins
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100659272.png)
我这里使用Node Js项目作为演示
工程目录如下
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100700832.png)
Jenkins Shell配置如下
#!/bin/bash
echo "##################################"
Dist=dist_`date +%F`
cd $WORKSPACE
if [ -d $Dist ];then
rm -rf $Dist
fi
if [ -f $Dist.tar.gz ];then
rm -rf $Dist.tar.gz
fi
echo "############################### NPM Install #############################"
cnpm install
npm run build
mv dist $Dist
tar zcvf $Dist.tar.gz $Dist
if [ -f $Dist.tar.gz ];then
echo "##################### Tar Is OK ###################"
else
echo "#################### Tar Is Error"
fi
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100701926.png)
接下来是拷贝程序的步骤,找到增加构建后操作

Name :这里是自定义的ssh远程服务器,如果有多台,点击add post-build step 继续添加 Source files :源文件地址,相对地址 比如 /root/.jenkins/workspace/jobname/target/xx.war 直接写成 target/xx.war就行 Remove prefix :这里指的是忽略前面的 target/ 远程服务器直接显示就是xx.war Remote directory :远程服务器要保存的文件目录 Exec command :这里直接写 bash 脚本名称 xx.sh 这里也是远程服务器的脚本
我的工程目录的Node Js,ssh 配置如下
![Jenkins 前端自动发布 [git 钩子]-每日运维网 Jenkins 前端自动发布 [git 钩子]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504100702836.png)
我们可以查
五、gitlab钩子测试
gitlab配置Jenkins钩子是需要有人push代码,现在我们push代码进行测试

#先构建第一遍,以便生成目录 [root@www.abcdocker.com]# git clone http://gitlab.fenqi.d.xiaonei.com/frontend/short-video-operation.git #git push提交文件进行测试 echo "1234" >1.txt git add 1.txt git commit -m "text" git push origin master

相关文章:
- 基于Kubernetes Gitlab CICD
- Kubernetes 1.14 二进制集群安装
- 基于Kubernetes Jenkins CICD
- Jenkins 自动化部署上线