Jenkins+Docker一键打包部署Vue项目步骤详解( 三 )

二、新建Jenkins配置打包任务 , 打包部署NodeJS(Vue)镜像1、新建任务前,安装Docker Pipeline插件 , 使用Pipeline流水线任务构建部署 , 安装Jenkins插件相关内容 , 请查看前面部署Jenkins相关文章 。

Jenkins+Docker一键打包部署Vue项目步骤详解

文章插图
2、安装完插件之后,新建一个流水线任务 。
Jenkins+Docker一键打包部署Vue项目步骤详解

文章插图
3、和之前的任务一样 , 选择“丢弃旧的构建” , 设置保持构建的最大个数为5 。
Jenkins+Docker一键打包部署Vue项目步骤详解

文章插图
4、下拉到“流水线”配置 , 选择Pipeline script
Jenkins+Docker一键打包部署Vue项目步骤详解

文章插图
流水线脚本如下:
node {# 从gitlab下载代码stage('Preparation') { // for display purposes// Get some code from a Github repositoryecho "checkout from GitLab"checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: 'git_username', url: 'http://127.0.0.1:9091/test/test.git']])}# NodeJS打包stage('Build NodeJS Vue') {echo "build nodejs code"nodejs('Node17') {sh 'echo $PATH'sh 'node -v'sh 'pnpm -v'sh 'pnpm install'sh 'pnpm run build'}}# 此处判断本机打包是否有容器 , 如果有的话需要删除stage('Delete Old Docker Container') {echo "delete docker container"sh '''if [[ "$(docker inspect $container_name 2> /dev/null | grep $container_name)" != "" ]];thenecho $container_name "容器存在,停止并删除"echo "docker stop" $container_namedocker stop $container_nameecho "docker rm" $container_namedocker rm $container_nameelseecho $container_name "容器不存在"fi'''}# 此处判断本机打包是否有镜像,如果有的话需要删除stage('Delete Old Docker Image') {echo "delete docker image"sh '''if [[ "$(docker images -q gitegg-portal 2> /dev/null)" != "" ]];thenecho gitegg-portal '镜像存在,删除镜像'docker rmi $(docker images -q gitegg-portal 2> /dev/null) --forceelseecho gitegg-portal '镜像不存在,创建镜像'fi'''}# Docker打包镜像,并保存为tarstage('Build Docker Image') {echo "start docker build portal code"// Run the docker builddocker.build 'gitegg-portal'echo "save docker images tar"sh 'docker save -o portal-image.tar gitegg-portal'}# 删除安装在本机的Docker镜像,非tar包stage('Delete New Docker Image') {echo "delete docker image"sh '''if [[ "$(docker images -q gitegg-portal 2> /dev/null)" != "" ]];thenecho gitegg-portal '镜像存在 , 删除镜像'docker rmi $(docker images -q gitegg-portal 2> /dev/null) --forceelseecho gitegg-portal '镜像不存在,创建镜像'fi'''}# 将Docker镜像tar包发送到服务器并执行部署命令stage('Send Docker Image') {echo "send docker image"sshPublisher(publishers: [sshPublisherDesc(configName: 'Test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '/opt/script/publish_docker_portal.sh gitegg-portal gitegg-portal latest 8130 8130 4413 4413', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'portal-image.tar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])}stage('Publish Results') {echo "End Publish Portal"}}5、在任务左侧点击立即构建
  • 立即构建

Jenkins+Docker一键打包部署Vue项目步骤详解

文章插图
  • 流水线任务可以在右侧显示阶段视图

Jenkins+Docker一键打包部署Vue项目步骤详解

文章插图
  • 查看构建日志:点击立即构建之后,下方会出现进度条,点击进度条就可以进入构建日志界面 。

Jenkins+Docker一键打包部署Vue项目步骤详解

文章插图
 
Jenkins+Docker一键打包部署Vue项目步骤详解

文章插图
6、构建成功后,下方会给出构建成功提示 。
Jenkins+Docker一键打包部署Vue项目步骤详解


推荐阅读