felix-cao / Blog

A little progress a day makes you a big success!
31 stars 4 forks source link

基于 Jenkins 和 gitlab 实现前端自动化部署 #198

Open felix-cao opened 3 years ago

felix-cao commented 3 years ago

一、场景描述

之前有篇文章是基于 gitlab gitlab-ci ansible 的自动化部署,请阅读

二、软件安装

2.1 Jenkins 安装

参考 Jenkins 安装配置及与 gitlab 集成

2.2 Gitlab 安装

参考 gitlab 安装指南

2.3 在 jenkins 服务器上安装配置nodejs

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

如果出现下面的错误

Failed to connect to raw.githubusercontent.com

先去 Ping 一下 raw.githubusercontent.com 的地址

vi /etc/hosts

输入

185.199.108.133 raw.githubusercontent.com

2.4 在 jenkins 安装 nodejs 插件并配置

安装 nodejs 插件 --- 让 jenkins 可以使用 npm 命令

Manage Jenkins -> Manage plugins -> Available -> 输入 nodejs

image

全局工具配置 image

三、Jenkins 与 gitlab 的通信

3.1 配置访问秘钥

jenkins 服务器上执行命令,复制 ~/.ssh/id_rsa.pub

$ ssh-keygen # 三次回车
$ vi ~/.ssh/id_rsa.pub

3.2 在 gitlab 中设置 Deploy Keys

gitlab 中 左上角小扳手 -> Deploy Keys -> 添加刚刚复制的 public key

3.3 在 jenkins 中设置凭据

设置凭据的目的是让 jenkinsgitlab 服务器上拉取代码 http://ip:8081/credentials/store/system/domain, 左侧 Add Credentials 把私钥 ~/.ssh/id_rsa 中的内容复制到这里 Private Key

3.4 配置 webhook,jenkins 和 gitlab 对接

进入 jenkins 项目 configure, 可以看到,jenkins 配置从上往下依次分为通用、源码关联、构建触发器、构建环境、构建、和构建后操作。jenkins 整个执行流就是按照这几个顺序依次进行执行的。

3.5 设置构建脚本

image

felix-cao commented 3 years ago

四、Jenkins 将代码部署到远程服务器

Publish Over SSH 插件的主要作用是通过 SSH 连接其他 Linux 机器,向远程主机发送文件和执行命令,官方文档

4.1 安装Publish Over SSH 插件

Manage Jenkins -> Manage plugins -> Available -> 输入 Publish Over SSH

4.2 配置 SSH Servers

Manage Jenkins -> Configure System 拉到最下面可以看到 Publish over SSH 各参数解释

将Jenkins服务器的公钥复制到远程主机中,一般不用 root 用户,参考 linux 下开启 SSH,远程免密登录 #11

ssh-copy-id -i ~/.ssh/id_rsa.pub admin@192.168.1.73

4.3 配置 build step

image 配置参数

注意Source files 指的是当前这个 job 的工作目录下的所有文件及目录

felix-cao commented 3 years ago

五、在 Jenkins 中使用日期变量

5.1 安装插件

Manage Jenkins -> Manage plugins -> Available -> 输入 Date Parameter

5.2 配置参数

进入项目的configure image

5.3 使用参数

image

felix-cao commented 3 years ago

六、判断某个docker 容器是否存在

#!/bin/bash
cd /home/www/${date}
tar -xzvf ${itemName}.tar.gz
if [ "$(docker ps -q -f name=${itemNginx})" ]; then
    docker stop ${itemNginx}
fi
if [ "$(docker ps -aq -f status=exited -f name=${itemNginx})" ]; then
    # cleanup
    docker rm ${itemNginx}
fi
if [ "$(docker images -q nginx:skillman-IIoT-web)" ]; then
  docker rmi nginx:${itemName}
fi
docker build -t nginx:${itemName} .
# run your container
docker run --name ${itemNginx} -p 8082:8082 -d nginx:${itemName}
felix-cao commented 3 years ago

七、基于 git tag 构建

7.1 安装插件

Manage Jenkins -> Manage plugins -> Available -> 输入 Git Parameter

7.2 进入项目配置

image

这里的Git Parameter的参数配置如下:

接下来于第三步相同

felix-cao commented 3 years ago

Jenkins 邮件通知配置

一、安装Email Extension Plugin插件

Manage Jenkins -> Manage plugins -> Available -> 输入 Email Extension image

二、邮箱配置 SMTP

Manage Jenkins -> Configure System -> 找到 Extended E-mail Notification image

三、项目SMTP Server

进入到具体的项目配置界面点击 Configure,在配置界面点击“Add Post Build Action”,选择“Editable Email Notification”

针对该项目进行个性化的配置,选择“Advanced Settings”--> '高级'