天天看点

shell脚本自动部署Springboot项目到云服务器

如何用shell脚本自动部署Springboot项目

在开发Springboot项目时,我们可能需要经常更新服务器上的代码,并把项目部署在服务器上,而每次都需要输入一连串的命令,这样效率实在不高,所以写个脚本,利用脚本执行所有的命令,可以大大提升速度(bi ge)。

环境

  • 云服务器操作系统为Centos/Linux系统
  • 代码使用git管理
  • 打成jar包
  • 之前,手动部署过项目,运行环境没有问题(java/nohup/git等都能正常使用)

编写shell脚本

1. 在项目根目录下创建脚本
touch build.sh
2. 授权
chmod 755 build.sh
3. 编写脚本

这里使用的是8081端口,Springboot项目打包后,会多出一个target目录,jar包就存放在该目录下,所以我把项目运行后的日志也存放在target下。因为是重新部署,所以需要停止原来的项目,杀死占用8081端口的进程,具体见以下脚本。

#!/bin/bash
echo '自动部署Springboot项目脚本...'
echo '1. 拉取github代码...'
git pull
echo '2. 检查8081端口是否被占用...'
pid_blog=`lsof -i :|grep -v "PID"|awk '{print $2}'`
if [ "$pid_blog" != "" ];
then
    echo '8081端口被占用'
    echo $pid_blog
    kill - "$pid_blog"
    echo $pid_blog '进程已被杀死'
else
    echo "端口未被占用"
fi
echo '3. 清理原有项目...'
mvn clean
echo '4. 打包...'
mvn install
echo '5. 后台运行jar包...'
nohup java -jar target/my-blog-.-SNAPSHOT.jar > target/blog.out &
           
4. 执行
./build.sh

打开浏览器访问你的地址或者打开日志查看启动是否成功

实际测试结果

[[email protected]_0_12_centos blog]# ./test.sh
自动部署Springboot项目脚本...
 拉取github代码...
Already up-to-date.
 检查端口是否被占用...
端口被占用

 进程已被杀死
 清理原有项目...
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------< com.my.blog.website:my-blog >---------------------
[INFO] Building my-blog -SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ my-blog ---
[INFO] Deleting /liqkjm/project/github/blog/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  s
[INFO] Finished at: --T13::+:
[INFO] ------------------------------------------------------------------------
 打包...
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------< com.my.blog.website:my-blog >---------------------
[INFO] Building my-blog -SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ my-blog ---
[INFO] Using 'UTF-' encoding to copy filtered resources.
[INFO] Copying  resources
[INFO] Copying  resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ my-blog ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling  source files to /liqkjm/project/github/blog/target/classes
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[,] sun.misc.BASE64Decoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[,] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[,] sun.misc.BASE64Decoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[,] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[,] sun.misc.BASE64Decoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[,] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[,] sun.misc.BASE64Encoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/Tools.java:[,] sun.misc.BASE64Decoder is internal proprietary API and may be removed in a future release
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/DateKit.java: /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/DateKit.java uses or overrides a deprecated API.
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/DateKit.java: Recompile with -Xlint:deprecation for details.
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/MapCache.java: Some input files use unchecked or unsafe operations.
[WARNING] /liqkjm/project/github/blog/src/main/java/com/my/blog/website/utils/MapCache.java: Recompile with -Xlint:unchecked for details.
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ my-blog ---
[INFO] Using 'UTF-' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /liqkjm/project/github/blog/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ my-blog ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling  source files to /liqkjm/project/github/blog/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ my-blog ---
[INFO] Surefire report directory: /liqkjm/project/github/blog/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.my.blog.website.TranscationTest
Tests run: , Failures: , Errors: , Skipped: , Time elapsed:  sec - in com.my.blog.website.TranscationTest
Running com.my.blog.website.controller.IndexControllerTest
Tests run: , Failures: , Errors: , Skipped: , Time elapsed:  sec - in com.my.blog.website.controller.IndexControllerTest
Running com.my.blog.website.AsyncTest

          _____           _______                   _____
         /\    \         /::\    \                 /\    \
        /::\____\       /::::\    \               /::\    \
       /:::/    /      /::::::\    \              \:::\    \
      /:::/    /      /::::::::\    \              \:::\    \
     /:::/    /      /:::/~~\:::\    \              \:::\    \
    /:::/    /      /:::/    \:::\    \              \:::\    \
   /:::/    /      /:::/    / \:::\    \             /::::\    \
  /:::/    /      /:::/____/   \:::\____\   _____   /::::::\    \
 /:::/    /      |:::|    |     |:::|    | /\    \ /:::/\:::\    \
/:::/____/       |:::|____|     |:::|____|/::\    /:::/  \:::\____\
\:::\    \        \:::\   _\___/:::/    / \:::\  /:::/    \::/    /
 \:::\    \        \:::\ |::| /:::/    /   \:::\/:::/    / \/____/
  \:::\    \        \:::\|::|/:::/    /     \::::::/    /
   \:::\    \        \::::::::::/    /       \::::/    /
    \:::\    \        \::::::::/    /         \::/    /
     \:::\    \        \::::::/    /           \/____/
      \:::\    \        \::::/____/
       \:::\____\        |::|    |
        \::/    /        |::|____|
         \/____/          ~~

Application Version: 
Spring Boot Version: .RELEASE (v1.RELEASE)
-- :: [main] INFO  com.my.blog.website.AsyncTest - Starting AsyncTest on VM_0_12_centos with PID  (started by root in /liqkjm/project/github/blog)
-- :: [main] DEBUG com.my.blog.website.AsyncTest - Running with Spring Boot v1.RELEASE, Spring v4.RELEASE
-- :: [main] INFO  com.my.blog.website.AsyncTest - The following profiles are active: dev
-- :: [background-preinit] DEBUG org.jboss.logging - Logging Provider: org.jboss.logging.Log4j2LoggerProvider
-- :: [background-preinit] INFO  org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator .Final
-- :: [background-preinit] DEBUG org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver - Cannot find javax.persistence.Persistence on classpath. Assuming non JPA  environment. All properties will per default be traversable.
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Identified candidate component class: file [/liqkjm/project/github/blog/target/classes/com/my/blog/website/dao/AttachVoMapper.class]
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Identified candidate component class: file [/liqkjm/project/github/blog/target/classes/com/my/blog/website/dao/CommentVoMapper.class]
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Identified candidate component class: file [/liqkjm/project/github/blog/target/classes/com/my/blog/website/dao/ContentVoMapper.class]
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Identified candidate component class: file [/liqkjm/project/github/blog/target/classes/com/my/blog/website/dao/LogVoMapper.class]
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Identified candidate component class: file [/liqkjm/project/github/blog/target/classes/com/my/blog/website/dao/MetaVoMapper.class]
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Identified candidate component class: file [/liqkjm/project/github/blog/target/classes/com/my/blog/website/dao/OptionVoMapper.class]
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Identified candidate component class: file [/liqkjm/project/github/blog/target/classes/com/my/blog/website/dao/RelationshipVoMapper.class]
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Identified candidate component class: file [/liqkjm/project/github/blog/target/classes/com/my/blog/website/dao/UserVoMapper.class]
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Creating MapperFactoryBean with name 'attachVoMapper' and 'com.my.blog.website.dao.AttachVoMapper' mapperInterface
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Enabling autowire by type for MapperFactoryBean with name 'attachVoMapper'.
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Creating MapperFactoryBean with name 'commentVoMapper' and 'com.my.blog.website.dao.CommentVoMapper' mapperInterface
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Enabling autowire by type for MapperFactoryBean with name 'commentVoMapper'.
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Creating MapperFactoryBean with name 'contentVoMapper' and 'com.my.blog.website.dao.ContentVoMapper' mapperInterface
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Enabling autowire by type for MapperFactoryBean with name 'contentVoMapper'.
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Creating MapperFactoryBean with name 'logVoMapper' and 'com.my.blog.website.dao.LogVoMapper' mapperInterface
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Enabling autowire by type for MapperFactoryBean with name 'logVoMapper'.
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Creating MapperFactoryBean with name 'metaVoMapper' and 'com.my.blog.website.dao.MetaVoMapper' mapperInterface
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Enabling autowire by type for MapperFactoryBean with name 'metaVoMapper'.
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Creating MapperFactoryBean with name 'optionVoMapper' and 'com.my.blog.website.dao.OptionVoMapper' mapperInterface
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Enabling autowire by type for MapperFactoryBean with name 'optionVoMapper'.
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Creating MapperFactoryBean with name 'relationshipVoMapper' and 'com.my.blog.website.dao.RelationshipVoMapper' mapperInterface
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Enabling autowire by type for MapperFactoryBean with name 'relationshipVoMapper'.
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Creating MapperFactoryBean with name 'userVoMapper' and 'com.my.blog.website.dao.UserVoMapper' mapperInterface
-- :: [main] DEBUG org.mybatis.spring.mapper.ClassPathMapperScanner - Enabling autowire by type for MapperFactoryBean with name 'userVoMapper'.
-- :: [main] DEBUG org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver - Cannot find javax.persistence.Persistence on classpath. Assuming non JPA  environment. All properties will per default be traversable.
-- :: [main] DEBUG org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver - Cannot find javax.persistence.Persistence on classpath. Assuming non JPA  environment. All properties will per default be traversable.
-- :: [main] DEBUG org.hibernate.validator.internal.engine.ConfigurationImpl - Setting custom MessageInterpolator of type org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator
-- :: [main] DEBUG org.hibernate.validator.internal.engine.ConfigurationImpl - Setting custom ConstraintValidatorFactory of type org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory
-- :: [main] DEBUG org.hibernate.validator.internal.engine.ConfigurationImpl - Setting custom ParameterNameProvider of type com.sun.proxy.$Proxy98
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ValidationXmlParser - Trying to load META-INF/validation.xml for XML based Validator configuration.
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ResourceLoaderHelper - Trying to load META-INF/validation.xml via user class loader
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ResourceLoaderHelper - Trying to load META-INF/validation.xml via TCCL
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ResourceLoaderHelper - Trying to load META-INF/validation.xml via Hibernate Validator's class loader
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ValidationXmlParser - No META-INF/validation.xml found. Using annotation based configuration only.
-- :: [main] DEBUG org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver - Cannot find javax.persistence.Persistence on classpath. Assuming non JPA  environment. All properties will per default be traversable.
-- :: [main] DEBUG org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver - Cannot find javax.persistence.Persistence on classpath. Assuming non JPA  environment. All properties will per default be traversable.
-- :: [main] DEBUG org.hibernate.validator.internal.engine.ConfigurationImpl - Setting custom MessageInterpolator of type org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator
-- :: [main] DEBUG org.hibernate.validator.internal.engine.ConfigurationImpl - Setting custom ConstraintValidatorFactory of type org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory
-- :: [main] DEBUG org.hibernate.validator.internal.engine.ConfigurationImpl - Setting custom ParameterNameProvider of type com.sun.proxy.$Proxy98
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ValidationXmlParser - Trying to load META-INF/validation.xml for XML based Validator configuration.
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ResourceLoaderHelper - Trying to load META-INF/validation.xml via user class loader
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ResourceLoaderHelper - Trying to load META-INF/validation.xml via TCCL
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ResourceLoaderHelper - Trying to load META-INF/validation.xml via Hibernate Validator's class loader
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ValidationXmlParser - No META-INF/validation.xml found. Using annotation based configuration only.
-- :: [main] INFO  com.alibaba.druid.pool.DruidDataSource - {dataSource-} inited
-- :: [main] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration
-- :: [main] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Parsed mapper file: 'file [/liqkjm/project/github/blog/target/classes/mapper/AttachVoMapper.xml]'
-- :: [main] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Parsed mapper file: 'file [/liqkjm/project/github/blog/target/classes/mapper/CommentVoMapper.xml]'
-- :: [main] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Parsed mapper file: 'file [/liqkjm/project/github/blog/target/classes/mapper/ContentVoMapper.xml]'
-- :: [main] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Parsed mapper file: 'file [/liqkjm/project/github/blog/target/classes/mapper/LogVoMapper.xml]'
-- :: [main] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Parsed mapper file: 'file [/liqkjm/project/github/blog/target/classes/mapper/MetaVoMapper.xml]'
-- :: [main] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Parsed mapper file: 'file [/liqkjm/project/github/blog/target/classes/mapper/OptionVoMapper.xml]'
-- :: [main] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Parsed mapper file: 'file [/liqkjm/project/github/blog/target/classes/mapper/RelationshipVoMapper.xml]'
-- :: [main] DEBUG org.mybatis.spring.SqlSessionFactoryBean - Parsed mapper file: 'file [/liqkjm/project/github/blog/target/classes/mapper/UserVoMapper.xml]'
-- :: [main] DEBUG org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver - Cannot find javax.persistence.Persistence on classpath. Assuming non JPA  environment. All properties will per default be traversable.
-- :: [main] DEBUG org.hibernate.validator.internal.engine.ConfigurationImpl - Setting custom MessageInterpolator of type org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator
-- :: [main] DEBUG org.hibernate.validator.internal.engine.ConfigurationImpl - Setting custom ConstraintValidatorFactory of type org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory
-- :: [main] DEBUG org.hibernate.validator.internal.engine.ConfigurationImpl - Setting custom ParameterNameProvider of type com.sun.proxy.$Proxy98
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ValidationXmlParser - Trying to load META-INF/validation.xml for XML based Validator configuration.
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ResourceLoaderHelper - Trying to load META-INF/validation.xml via user class loader
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ResourceLoaderHelper - Trying to load META-INF/validation.xml via TCCL
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ResourceLoaderHelper - Trying to load META-INF/validation.xml via Hibernate Validator's class loader
-- :: [main] DEBUG org.hibernate.validator.internal.xml.ValidationXmlParser - No META-INF/validation.xml found. Using annotation based configuration only.
-- :: [main] INFO  com.my.blog.website.AsyncTest - Started AsyncTest in  seconds (JVM running for )
开始做任务三
开始做任务一
开始做任务二
完成任务一,耗时:毫秒
完成任务三,耗时:毫秒
完成任务二,耗时:毫秒
任务全部完成,总耗时:毫秒
Tests run: , Failures: , Errors: , Skipped: , Time elapsed:  sec - in com.my.blog.website.AsyncTest

Results :

Tests run: , Failures: , Errors: , Skipped: 

[INFO] 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ my-blog ---
[INFO] Building jar: /liqkjm/project/github/blog/target/my-blog--SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.1.RELEASE:repackage (default) @ my-blog ---
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ my-blog ---
[INFO] Installing /liqkjm/project/github/blog/target/my-blog--SNAPSHOT.jar to /root/.m2/repository/com/my/blog/website/my-blog/-SNAPSHOT/my-blog--SNAPSHOT.jar
[INFO] Installing /liqkjm/project/github/blog/pom.xml to /root/.m2/repository/com/my/blog/website/my-blog/-SNAPSHOT/my-blog--SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  s
[INFO] Finished at: --T13::+:
[INFO] ------------------------------------------------------------------------
 后台运行jar包...
[[email protected]_0_12_centos blog]# nohup: redirecting stderr to stdout
           

测试链接:http://111.230.55.56:8081/

项目地址:https://github.com/liqkjm/blog

shell脚本自动部署Springboot项目到云服务器

继续阅读