Jenkins使用PipeLine进行前端打包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
pipeline {
# 这个是Jenkins Pipeline配置
agent any
# 超时时间
options {
timeout(time: 5, unit: 'MINUTES')
timestamps()
}
# 使用的工具
tools {
nodejs 'v22.20.0'
}
# 配置环境变量
environment {
# telegram机器人token和chat_id
TELEGRAM_BOT_TOKEN = 'xxxxxx'
TELEGRAM_CHAT_ID = '-xxxx'
# 部署路径和git地址,分支
DEPLOY_PATH = '/www/wwwroot/xx/xxx/'
APP_NAME = "xxx"
GIT_BRANCH_NAME = "main"
GIT_URL = "http://xx/xx/xxx.git"
# git拉取的凭证权限id,可以在系统管理-凭据配置里添加git用户 后提取id
GIT_CREDENTIALSID = "xxx-xx-xx-xxx-xxx"
}

stages {
stage('初始化') {
steps {
echo '开始构建...'
}
}
stage('拉代码') {
steps {
git branch: "${GIT_BRANCH_NAME}", credentialsId: "${GIT_CREDENTIALSID}", url: "${GIT_URL}"
}
}

stage('通知') {
steps {
script {
sh '''
curl -s -X POST https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage \
-d chat_id=${TELEGRAM_CHAT_ID} \
-d text="web部署ing...耐心等待一下"
'''
}
}
}

stage('安装依赖') {
steps {
echo '拉依赖...'
sh 'pnpm install'
}
}

stage('构建') {
steps {
echo '构建中...'
sh 'pnpm run build:prod'
}
}

stage('部署') {
steps {
echo '开始部署...'
sh '''
rm -rf ${DEPLOY_PATH}
cp -r dist/ ${DEPLOY_PATH}
'''
}
}

stage('成功通知') {
steps {
script {
sh '''
curl -s -X POST https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage \
-d chat_id=${TELEGRAM_CHAT_ID} \
-d text="web部署完毕!!![庆祝][庆祝][庆祝]"
'''
}
}
}
}

post {
failure {
script {
sh '''
ERROR_MSG="构建失败! 查看日志: ${BUILD_URL}console"
curl -s -X POST https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage \
-d chat_id=${TELEGRAM_CHAT_ID} \
-d text="web部署失败! ${ERROR_MSG}![失望][失望][失望]"
'''
}
}
}
}