---
title: Jenkins
date: 2022-11-09 10:30:31
author: 文永达
top_img: https://gcore.jsdelivr.net/gh/volantis-x/cdn-wallpaper/abstract/B18FCBB3-67FD-48CC-B4F3-457BA145F17A.jpeg
---
# Jenkins

```powershell
# 删除文件夹
rmdir /q /s C:\Program" "Files\nginx-1.23.2\html\dist
# 复制文件夹到指定目录
xcopy /y /e /i C:\Users\Administrator\Documents\source\XiaoDaERP-Vben\dist C:\Program" "Files\nginx-1.23.2\html\dist
# 发送文件（文件夹）到Linux远程服务器上
pscp -r -l root -pw Wyd210213 C:\Users\Administrator\Documents\source\XiaoDaERP-Vben\dist 8.140.174.251:/usr/local/nginx/html
```

```shell
docker run -d -p 80:80 -p 443:443 --name nginxweb --privileged=true
-v /usr/local/nginx/html/:/usr/share/nginx/html 
-v /usr/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf 
-v /usr/local/nginx/conf/conf.d:/etc/nginx/conf.d 
-v /usr/local/nginx/logs:/var/log/nginx nginx


ps aux | grep "nginx: worker process" | awk '{print $1}'
```

## 部署.Net Core 至 IIS

新建Item

![image-20221108224600422](http://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221108224600422.png)

配置Git

![image-20221108224810869](http://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221108224810869.png)

添加Git账户

![image-20221108224709242](http://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221108224709242.png)

指定Jenkins从Git拉取代码目录

![image-20221108224910785](http://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221108224910785.png)

![image-20221108224935821](http://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221108224935821.png)

指定Jenkins定时获取Git

时间设置成：H/2 * * * *

意思是每2分钟检查Git是否有变化，如果有变化就会重新构建和部署

![image-20221108225020629](http://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221108225020629.png)

构建步骤

因为Jenkins在Windows环境下所以使用 Execute Windows batch command

![image-20221108225404450](http://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221108225404450.png)

命令:

```powershell
c:
// 切换到Git拉取代码目录
cd C:\Users\Administrator\Documents\source\XiaodaERP
// 构建.Net Core应用
dotnet build
 // 停止应用程序池 saas
C:\Windows\System32\inetsrv\appcmd.exe stop apppool /apppool.name:saas
 // 发布.Net Core应用 需指定发布文件目录
dotnet publish -o D:\subendong\release\saas
 // 启动应用程序池 saas
C:\Windows\System32\inetsrv\appcmd.exe start apppool /apppool.name:saas
```

IIS服务器应用程序池必须为无代码托管

为防止生产环境Swagger无法使用，需在构建后的文件夹中编辑web.conf文件

![image-20221108230704528](http://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221108230704528.png)

添加如下代码

```xml
<environmentVariables>
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
```

![1668787835347](http://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/1668787835347.png)

在模块中删除WebDAVModule

## Jenkins安装NodeJS环境

在Jenkins 系统管理中 插件管理 可用插件中安装`NodeJS Plugin`

并在系统管理中 全局工具配置中配置NodeJS

![image-20221122094753314](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221122094753314.png)

## 部署.Net Core 至 Docker容器

## Windows Server 部署Vue

### 本地Nginx

```shell
yarn
yarn build
# 删除文件夹
rmdir /q /s C:\Program" "Files\nginx-1.23.2\html\dist
# 复制文件夹到指定目录
xcopy /y /e /i C:\Users\Administrator\Documents\source\XiaoDaERP-Vben\dist C:\Program" "Files\nginx-1.23.2\html\dist
```

Linux远程服务器

```shell
# 发送文件（文件夹）到Linux远程服务器上
pscp -r -l root -pw Wyd210213 C:\Users\Administrator\Documents\source\XiaoDaERP-Vben\dist 8.140.174.251:/usr/local/nginx/html
```

## Jenkins部署Hexo博客

配置NodeJS环境

![image-20221122140826369](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221122140826369.png)

构建步骤

```shell
npm install hexo-cli -g
yarn
hexo clean
hexo g --debug
docker stop xiaodablog
docker rm xiaodablog
docker images
docker image rm xiaodablog
docker build -t xiaodablog:latest .
docker image rm -f $(docker images | grep "none" | awk '{print $3}')
docker images
docker run --name xiaodablog -p 80:80 -p 443:443 -d --privileged=true -v /usr/local/nginx/logs/xiaodablog:/var/log/nginx xiaodablog:latest
```

需要注意的是刚clone下来的项目，需要现在本地构建docker镜像并启动容器

否则会报错

## Jenkins部署Vue VbenAdmin

增加参数化构建过程

![image-20221122145043478](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221122145043478.png)

配置NodeJS环境

![image-20221122145109235](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221122145109235.png)

构建过程

```shell
pwd
npm config get registry
npm config set registry http://registry.npm.taobao.org/
npm install -g yarn
yarn
yarn build
echo "npm finish"
echo $version
docker build -t xiaodaerp/vbenvue:$version .
docker stop xiaodaerpvbenvue
docker rmi -f $(docker images | grep "none" | awk '{print $3}')
docker images
docker rm xiaodaerpvbenvue
docker run --name xiaodaerpvbenvue -p 81:80 -p 444:443 -d --privileged=true -v /usr/local/nginx/logs/xiaodaerpvbenvue:/var/log/nginx xiaodaerp/vbenvue:$version
```

需要注意的是刚clone下来的项目，需要现在本地构建docker镜像并启动容器

否则会报错

## Jenkins配置用户权限

系统管理 -> 插件管理 安装`Matrix Authorization Strategy Plugin`插件

![image-20221122174638567](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221122174638567.png)

授权策略选择`项目矩阵授权策略`

分配以下权限

![image-20221122174805834](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221122174805834.png)

## Jenkins部署dotNet 6项目到远程Linux服务器上

系统管理 -> 插件管理 安装`Publish Over SSH`插件

安装好后 在 系统管理 -> 系统配置中设置`SSH Servers`

![image-20221123095111849](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221123095111849.png)

并点击高级

![image-20221123095205119](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221123095205119.png)

设置密码

构建步骤

![image-20221123095510632](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221123095510632.png)

因为是上传文件夹至远程服务器指定目录，如果文件夹中内容发生变化，比如文件名，文件夹需要进行删除操作，所以在发送文件前需要先执行远程Shell命令

![image-20221130160623349](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221130160623349.png)

Exec command

```shell
cd /root/download
pwd
rm -rf XiaodaERP/
```

再进行上传文件夹部署操作

![image-20221130160724401](https://markdownhexo.oss-cn-hangzhou.aliyuncs.com/img/image-20221130160724401.png)

Source files

```shell
XiaodaERP/**
```

Remote directory

```shell
/root/download/
```

Exec command

```shell
cd /root/download
pwd
docker stop xiaodaerpnetcore
docker rm xiaodaerpnetcore
docker image rm xiaodaerp/netcore
docker images
docker image build -f ./XiaodaERP/Dockerfile -t xiaodaerp/netcore .
docker rmi -f $(docker images | grep "none" | awk '{print $3}')
docker images
docker run --name xiaodaerpnetcore -p 7274:80 -d xiaodaerp/netcore
```

