如何用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