432 lines
11 KiB
Markdown
432 lines
11 KiB
Markdown
---
|
||
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
|
||
|
||

|
||
|
||
配置Git
|
||
|
||

|
||
|
||
添加Git账户
|
||
|
||

|
||
|
||
指定Jenkins从Git拉取代码目录
|
||
|
||

|
||
|
||

|
||
|
||
指定Jenkins定时获取Git
|
||
|
||
时间设置成:H/2 * * * *
|
||
|
||
意思是每2分钟检查Git是否有变化,如果有变化就会重新构建和部署
|
||
|
||

|
||
|
||
构建步骤
|
||
|
||
因为Jenkins在Windows环境下所以使用 Execute Windows batch command
|
||
|
||

|
||
|
||
命令:
|
||
|
||
```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文件
|
||
|
||

|
||
|
||
添加如下代码
|
||
|
||
```xml
|
||
<environmentVariables>
|
||
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
|
||
</environmentVariables>
|
||
```
|
||
|
||

|
||
|
||
在模块中删除WebDAVModule
|
||
|
||
## Jenkins安装NodeJS环境
|
||
|
||
在Jenkins 系统管理中 插件管理 可用插件中安装`NodeJS Plugin`
|
||
|
||
并在系统管理中 全局工具配置中配置NodeJS
|
||
|
||

|
||
|
||
## 部署.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环境
|
||
|
||

|
||
|
||
构建步骤
|
||
|
||
```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
|
||
|
||
增加参数化构建过程
|
||
|
||

|
||
|
||
配置NodeJS环境
|
||
|
||

|
||
|
||
构建过程
|
||
|
||
```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`插件
|
||
|
||

|
||
|
||
授权策略选择`项目矩阵授权策略`
|
||
|
||
分配以下权限
|
||
|
||

|
||
|
||
## Jenkins部署dotNet 6项目到远程Linux服务器上
|
||
|
||
系统管理 -> 插件管理 安装`Publish Over SSH`插件
|
||
|
||
安装好后 在 系统管理 -> 系统配置中设置`SSH Servers`
|
||
|
||

|
||
|
||
并点击高级
|
||
|
||

|
||
|
||
设置密码
|
||
|
||
构建步骤
|
||
|
||

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

|
||
|
||
Exec command
|
||
|
||
```shell
|
||
cd /root/download
|
||
pwd
|
||
rm -rf XiaodaERP/
|
||
```
|
||
|
||
再进行上传文件夹部署操作
|
||
|
||

|
||
|
||
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
|
||
```
|
||
|
||
# Jenkins环境配置
|
||
|
||
## Docker容器内安装NVM
|
||
|
||
首先以bash命令行交互形式进入容器
|
||
|
||
```shell
|
||
docker exec -it jenkins /bin/bash
|
||
```
|
||
|
||
下载并执行nvm-sh脚本
|
||
|
||
```shell
|
||
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
|
||
```
|
||
|
||
执行完成,需应用环境变量
|
||
|
||
```shell
|
||
source ~/.bashrc
|
||
```
|
||
|
||
全局安装pnpm
|
||
|
||
```shell
|
||
npm install -g pnpm
|
||
```
|
||
|
||
设置源
|
||
|
||
```shell
|
||
pnpm config set registry https://registry.npmmirror.com
|
||
```
|
||
|
||
查看nodejs位置
|
||
|
||
```shell
|
||
npm prefix -g
|
||
```
|
||
|
||
## Docker容器内安装JDK
|
||
|
||
首先下载好需要的jdk包
|
||
|
||
复制到容器内用户目录下
|
||
|
||
```shell
|
||
docker cp docker cp ~/OpenJDK21U-jdk_aarch64_linux_hotspot_21.0.8_9.tar.gz jenkins:/root
|
||
```
|
||
|
||
以交互模式进入到容器内
|
||
|
||
```shell
|
||
docker exec -it jenkins /bin/bash
|
||
```
|
||
|
||
解压压缩包
|
||
|
||
```shell
|
||
cd ~
|
||
tar -zxvf OpenJDK21U-jdk_aarch64_linux_hotspot_21.0.8_9.tar.gz
|
||
```
|
||
|
||
# Jenkins Pipeline(流水线)
|
||
|
||
```shell
|
||
pipeline {
|
||
agent any
|
||
|
||
tools {
|
||
nodejs 'NodeJS 22.17.0'
|
||
}
|
||
|
||
environment {
|
||
GIT_URL = 'http://192.168.6.20:9980/line-group/dify-conversation.git'
|
||
BRANCH_NAME = 'erp-conversation'
|
||
}
|
||
|
||
stages {
|
||
stage('Preparation') {
|
||
steps {
|
||
// 清理工作空间
|
||
// deleteDir()
|
||
|
||
// 拉取代码
|
||
git branch: "${BRANCH_NAME}",
|
||
url: "${GIT_URL}",
|
||
credentialsId: '118322d7-1666-4f0b-b48b-349dcead864c',
|
||
changelog: true,
|
||
poll: false
|
||
}
|
||
}
|
||
|
||
stage('Setup Environment') {
|
||
steps {
|
||
sh '''
|
||
node --version
|
||
pnpm --version
|
||
'''
|
||
}
|
||
}
|
||
|
||
stage('Intall Dependencies') {
|
||
steps {
|
||
sh '''
|
||
# 清理缓存
|
||
pnpm store prune || true
|
||
|
||
# 安装依赖
|
||
pnpm install --frozen-lockfile
|
||
|
||
# 检查依赖
|
||
pnpm list --depth 0
|
||
'''
|
||
}
|
||
}
|
||
|
||
stage('Build Production') {
|
||
steps {
|
||
sh '''
|
||
# 清理旧构建
|
||
rm -rf .next || true
|
||
|
||
# 生产环境构建
|
||
pnpm run build
|
||
|
||
# 验证构建结果
|
||
if [ -d ".next" ]; then
|
||
echo "构建成功,文件列表:"
|
||
ls -la .next/
|
||
echo "总文件数:$(find dist -type f | wc -l)"
|
||
else
|
||
echo "构建失败,.next目录不存在"
|
||
exit 1
|
||
fi
|
||
'''
|
||
}
|
||
|
||
post {
|
||
success {
|
||
archiveArtifacts artifacts: '.next/**/*', fingerprint: true
|
||
}
|
||
}
|
||
}
|
||
|
||
stage('Quality Check') {
|
||
steps {
|
||
sh '''
|
||
# 代码检查
|
||
pnpm run lint --no-fix || echo "代码检查完成"
|
||
|
||
# 类型检查
|
||
pnpm run type-check || echo "类型检查完成"
|
||
'''
|
||
}
|
||
}
|
||
}
|
||
|
||
post {
|
||
cleanup {
|
||
cleanWs()
|
||
}
|
||
success {
|
||
echo 'Pipeline completed successfully!'
|
||
}
|
||
failure {
|
||
echo 'Pipeline failed!'
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|