Jenkins 远程部署SpringBoot与Vue | Eddie'Blog
Jenkins 远程部署SpringBoot与Vue

Jenkins 远程部署SpringBoot与Vue

eddie 424 2021-07-06

目录

序号标题说明
1a_serverJenkins
2b_server发布的服务器

a_server 部分

安装插件

Publish Over SSH

配置远程地址

Dashboard => 配置 => Publish over SSH

图片.png

说明

序号标题说明
1Name自定义
2Hostname服务器地址
3Username用户名
4Remote Directory远程目录
5Passphrase / Password用户密码
6Key.ssh/id_rsa
7Port端口, 默认:22

构建自由风格项目

图片.png

b_server

创建账户

比如:创建一个 eddie 账户

adduser eddie
passwd eddie

创建SSH-KEY

一路回车

ssh-keygen -t rsa

查看key

cat /home/eddie/.ssh/id_rsa

非Root用户使用docker指令

groupadd docker
gpasswd -a eddie docker
systemctl restart docker

结构图

/home/eddie/deploy/java-pc-gateway
|-- bak
|   |-- gateway-0.0.1-SNAPSHOT-execute.jar.20210705230235
|   |-- gateway-0.0.1-SNAPSHOT-execute.jar.20210705230435
|   |-- gateway-0.0.1-SNAPSHOT-execute.jar.20210706085934
|   |-- gateway-0.0.1-SNAPSHOT-execute.jar.20210706093637
|   `-- gateway-0.0.1-SNAPSHOT-execute.jar.20210706095558
|-- Dockerfile
|-- gateway-0.0.1-SNAPSHOT-execute.jar
`-- run.sh
创建Dockerfile
FROM java:8
MAINTAINER eddie.lee <xxxxx@qq.com>
VOLUME /tmp
ADD gateway-0.0.1-SNAPSHOT-execute.jar /api.jar
RUN bash -c 'touch /api.jar'
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
#EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/api.jar"]
新建脚本
#!/bin/bash
now=`date +"%Y%m%d%H%M%S"`
# 定义端口号
server_port='9005'
# 定义应用名称
app_name='gateway'
# 定义应用版本
app_version='0.0.1-SNAPSHOT-execute'
# 定义应用环境
profile_active='prod'
# 打包后在jenkins的目录
source_path='/home/eddie/deploy/java-pc-'${app_name}
# Dockerfile执行jar包的地址
des_path='/home/eddie/deploy/java-pc-'${app_name}'/bak'
# 复制jar包
cp ${source_path}/${app_name}-${app_version}.jar ${des_path}/${app_name}-${app_version}.jar.${now}
# echo '----copy jar----'
# 删除容器及镜像
docker stop java-pc-${app_name}
echo '----stop container----'
docker rm java-pc-${app_name}
echo '----rm container----'
docker rmi eddie/${app_name}:${app_version}
echo '----rm image----'
# 打包编译docker镜像
docker build -t eddie/${app_name}:${app_version} .
echo '----build image----'
docker run -p ${server_port}:${server_port} --name java-pc-${app_name} \
-e 'spring.profiles.active'=${profile_active} \
-e TZ="Asia/Shanghai" \
-v /etc/localtime:/etc/localtime \
-d eddie/${app_name}:${app_version}
echo '----start container----'
额外贴个Vue前端部署

jenkins pipline script

pipeline{
      agent any
      parameters {
          //string(name: 'PERSON', defaultValue: 'Mr Eddie', description: 'https://blog.csdn.net/eddielee9217')
        gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'BRANCH', type: 'PT_BRANCH'
      }
      environment{
        BRANCH =  sh(returnStdout: true,script: 'echo $branch').trim()
        GIT_URL = 'git@gitee.com:xxxx-team/web.git'            // 记得修改
        GIT_CREDENTIALSID = 'eddie-123-456-789-123456789'      // 记得修改
      }

      stages{

        stage('Git'){
          steps{
            sh 'echo Checkout Git Start ...'
            git branch: "${params.BRANCH}", credentialsId: GIT_CREDENTIALSID, url: GIT_URL
          }
        }

       stage("Check file") {
            steps {
                script {
                    try{
					    out = sh(script: "[ -f /var/jenkins_home/workspace/html/pc/pc-pipeline/package.json ]  && echo 'true' || echo 'false' ", returnStdout: true)
                        println out
                    } catch(Exception e) {
                        println e
                        error("fond error during check file download.")
                    }
                }
            }
        }

       stage('NodeJs'){
          steps{
            sh'''
            npm -v
            node -v
            npm install
            npm run build
            '''
          }
        }
        //...
        stage('Send') {
		  steps{
			sshPublisher(publishers: [sshPublisherDesc(configName: 'i-eddie',
										   transfers: [sshTransfer( cleanRemote: false,
																	excludes: '',
																	execCommand: '',
																	execTimeout: 120000,
																	flatten: false,
																	makeEmptyDirs: false,
																	noDefaultExcludes:false, 
																	patternSeparator: '[, ]+',
																	remoteDirectory: "deploy/vue-pc/dist",
																	remoteDirectorySDF: false,
																	removePrefix: 'dist',
																	sourceFiles: 'dist/**')],
											usePromotionTimestamp: false,
											useWorkspaceInPromotion: false,
											verbose: false)])
		  }
        }
    }
}

configName: 'i-eddie' 对应上面配置的自定义名称

/home/eddie/deploy/vue-pc/
`-- dist
    |-- css
    |-- favicon.ico
    |-- fonts
    |-- img
    |-- index.html
    |-- js
    `-- media