前面我们已经学习了jenkin的持续集成与部署,接下来我们将使用jenkin来部署我们之前已经完成的微服务项目

先了解如何手动部署vue项目,这样后面才知道如何在jenkins中部署vue项目
因为之前的部署分析,我们在jenkins中就需要部署vue项目
所以我们直接在jenkin的服务器中安装nodejs
gitee新建仓库


本地电脑新建一个目录:ec_project,右键git bash;
设置Git账户
生成SSH公钥
设置账户公钥


测试公钥:ssh -T git@gitee.com
克隆项目:先git init,然后git clone XXXXX

将电商系统的源码拷贝到目录中:然后右键 TortoiseGit -> 添加 -> 选中全部 -> 确定







回到git刷新下我们可以看到代码已经上传好了

这时候到jenkins的服务器clone代码:

nodejs安装参考:https://blog.csdn.net/qq23001186/article/details/125698420
因为我们是root账户,所以需要:npm i --unsafe-perm
cd online-store
npm i --unsafe-perm
npm run dev
vim package.json:添加上,--host 0.0.0.0
npm run dev来运行了:使用命令npm run build将工程build成静态文件,路径在dist目录下

docker run --name nginx -d -p 80:80 nginx:latest

docker exec -it 容器id /bin/bashcat /etc/nginx/nginx.conf
cat /etc/nginx/conf.d/default.conf;
mkdir docker
docker cp nginx:/etc/nginx /docker/nginx/config/docker cp nginx:/usr/share/nginx/html /docker/nginx/data/docker cp nginx:/var/log/nginx /docker/nginx/logs/
docker -rm -f nginx来删除
docker run --name nginx -p 80:80 \
-v /docker/nginx/config/nginx:/etc/nginx \
-v /docker/nginx/data/html:/usr/share/nginx/html \
-v /docker/nginx/logs/:/var/log/nginx \
-d nginx:latest

首先我们需要知道静态文件需要保存在nginx的html目录

将之前生成的静态文件全部上传到该目录下




echo "运行 npm install"
npm i --unsafe-perm
echo "运行 run build"
npm run build
echo "构建成功"

如果kenkins控制台报错npm command not find,就是jenkins的用户权限问题
修改jenkins的用户组为root:gpasswd -a jenkins root;这一步很重要
groups jenkins:查看jenkins所在的用户组
全局添加一个SSH配置:登录后的远程目录为根目录

添加构建后操作


构建运行


git credentialsId: 'online-store', url: 'https://gitee.com/justoso/online-store.git'
sh '''echo "运行 npm install"
npm i --unsafe-perm
echo "运行 run build"
npm run build
echo "构建成功"'''


sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.124.51-root', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'echo “success”', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'docker/nginx/data/html', remoteDirectorySDF: false, removePrefix: 'dist', sourceFiles: 'dist/**')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
pipeline {
agent any
stages {
stage('pull code') {
steps {
git credentialsId: 'online-store', url: 'https://gitee.com/justoso/online-store.git'
}
}
stage('build project') {
steps {
sh '''echo "运行 npm install"
npm i --unsafe-perm
echo "运行 run build"
npm run build
echo "构建成功"'''
}
}
stage('deploy project') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.124.51-root', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'echo “success”', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'docker/nginx/data/html', remoteDirectorySDF: false, removePrefix: 'dist', sourceFiles: 'dist/**')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}


