開發(fā)離不開項目的部署二驰,運維每强,因此為了以后的開發(fā)便利,我最近學(xué)習(xí)了下服務(wù)的自運維娇掏,現(xiàn)在總結(jié)一下
參考:https://my.oschina.net/yimingkeji/blog/2878371
技術(shù)
Jenkins+docker+git+Gitee 部署SpringBoot項目
環(huán)境
- Win10
- 虛擬機:VMware
- 系統(tǒng):centos7
操作流程
1.安裝虛擬機呕寝,配置centos系統(tǒng),聯(lián)通網(wǎng)絡(luò)
2.配置Java環(huán)境
3.下載婴梧,安裝下梢,配置jenkins
1)Jenkins的介紹
Jenkins是一個廣泛用于持續(xù)構(gòu)建的可視化web工具,可用于自動化與構(gòu)建塞蹭、測試孽江、交付或部署軟件相關(guān)的各種任務(wù)。
2)看下Jenkins的工作流程
- 我們在本地開發(fā)項目番电。記得加上dcoker的依賴和Dockerfile的配置(后面說)竟坛。然后通過本地git提交到碼云(Github)。
- 這時碼云上的項目中配置鉤子程序(Webhooks)钧舌。當(dāng)我們在本地push的時候觸發(fā)鉤子。
這個URL的地址要觸發(fā)Jenkis(后面介紹)
- Jenkins接收到通知以后涎跨,會調(diào)用虛擬機上的git洼冻。將遠(yuǎn)程碼云倉庫的項目pull到服務(wù)器上
- 接下來的工作中心就在Jenkins上。根據(jù)配置(后面詳細(xì)說)隅很,將項目用服務(wù)器上mvn打成jar包
- 利用docker結(jié)合項目的dockerfile撞牢,打包成docker鏡像,然后啟動即可叔营。(具體配置看后面)
3)Jenkins的下載和安裝
看參考即可
不同的是我沒有安裝到tomcat下面屋彪,隨tomcat啟動,而是直接安裝linux中绒尊,然后設(shè)置啟動
這可以百度下
4.配置docker
- 首先是插件的安裝畜挥,可以看參考,會遇到無法連接到外網(wǎng)服務(wù)器的問題婴谱,百度解決即可
- 進(jìn)去后蟹但,首先全局配置,-》看參考 配置maven谭羔、JDK华糖、Docker、git
這些都需要安裝到服務(wù)器上瘟裸,git還需生成私鑰客叉,公鑰。公鑰放到碼云遠(yuǎn)程倉庫,私鑰放到Jenkins上兼搏,使得通過Jenkin利用git將代碼拉到服務(wù)器上卵慰。
- 安裝docker
- Springboot項目
項目地址:https://gitee.com/yimingkeji/docker-springboot
docker依賴
<!-- Docker maven plugin start -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>${project.artifactId}</imageName><!--[a-z 0-9] 不然docker:build會報錯-->
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<!--dockerfile的地址-->
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<!-- Docker maven plugin end -->
Dockerfile內(nèi)容:
FROM hub.c.163.com/dwyane/openjdk:8
VOLUME /tmp
ADD docker-test-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
EXPOSE 8081
dockerfile的jar包的名字要和項目的名字保持一致
<groupId>com.wuhen</groupId>
<artifactId>docker-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
然后上傳到自己的碼云遠(yuǎn)程倉庫
5.使用Jenkins部署項目到docker
看參考
其中
第一次執(zhí)行shell 添加shell:
#!/bin/bash -il
mvn docker:build
echo "當(dāng)前docker 鏡像:"
docker images | grep docker-test
echo "啟動容器----->"
docker run --name dockertest -p 8081:8081 -d docker-test
echo "啟動服務(wù)成功!"
項目構(gòu)建成功后再次構(gòu)建shell有所改變
#!/bin/bash -il
# 先刪除之前的容器
echo "remobe old container"
docker ps -a | grep dockertest | awk '{print $1}'| xargs docker rm -f
# 刪除之前的鏡像
echo "romove old image"
docker rmi docker-test
# 構(gòu)建鏡像
mvn docker:build
# 打印當(dāng)前鏡像
echo "current docker images"
docker images | grep docker-test
# 啟動容器
echo "start container"
docker run --name dockertest -p 8081:8081 -d docker-test
# 打印當(dāng)前容器
echo "current container"
docker ps -a | grep dockertest
echo "star service success!"
這個容器端口映射:項目的啟動端口好為8081向族,映射到外部服務(wù)器端口為8081(可以固定呵燕。也可以隨機),所以在外部瀏覽器訪問IP:8081,即映射到項目上件相,添加路徑即可訪問
控制臺輸出
Started by user unknown or anonymous
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/docker-test
> /usr/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> /usr/bin/git config remote.origin.url git@gitee.com:zhege/docker-test.git # timeout=10
Fetching upstream changes from git@gitee.com:zhege/docker-test.git
> /usr/bin/git --version # timeout=10
using GIT_SSH to set credentials gitee
> /usr/bin/git fetch --tags --progress git@gitee.com:zhege/docker-test.git +refs/heads/*:refs/remotes/origin/*
> /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> /usr/bin/git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 8b9c5e5cdda5cf060be846f2c53b9662cd8d1c7f (refs/remotes/origin/master)
> /usr/bin/git config core.sparsecheckout # timeout=10
> /usr/bin/git checkout -f 8b9c5e5cdda5cf060be846f2c53b9662cd8d1c7f
Commit message: "測試0.8"
> /usr/bin/git rev-list --no-walk 8b9c5e5cdda5cf060be846f2c53b9662cd8d1c7f # timeout=10
[docker-test] $ /usr/local/maven/apache-maven/bin/mvn -s /usr/local/maven/apache-maven/conf/settings.xml -gs /usr/local/maven/apache-maven/conf/settings.xml clean install -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.wuhen:docker-test >------------------------
[INFO] Building docker-test 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ docker-test ---
[INFO] Deleting /var/lib/jenkins/workspace/docker-test/target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ docker-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ docker-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /var/lib/jenkins/workspace/docker-test/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ docker-test ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ docker-test ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ docker-test ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ docker-test ---
[INFO] Building jar: /var/lib/jenkins/workspace/docker-test/target/docker-test-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.2.1.RELEASE:repackage (repackage) @ docker-test ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ docker-test ---
[INFO] Installing /var/lib/jenkins/workspace/docker-test/target/docker-test-0.0.1-SNAPSHOT.jar to /root/.m2/repository/com/wuhen/docker-test/0.0.1-SNAPSHOT/docker-test-0.0.1-SNAPSHOT.jar
[INFO] Installing /var/lib/jenkins/workspace/docker-test/pom.xml to /root/.m2/repository/com/wuhen/docker-test/0.0.1-SNAPSHOT/docker-test-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.245 s
[INFO] Finished at: 2019-11-19T15:24:43+08:00
[INFO] ------------------------------------------------------------------------
[docker-test] $ /bin/bash -il /tmp/jenkins6420905348235517809.sh
bash: no job control in this shell
remobe old container
8ecc1c0ad528
romove old image
Untagged: docker-test:latest
Deleted: sha256:e21b5d37eec2178a134b2b11ff2c520c9e4195da30514fdf3a9c908c7d1923bc
Deleted: sha256:f6687ce9698053b270d31032bba9953d1df257038b356827161a7ef6b3a0d6fa
Deleted: sha256:1770755bad3191affc457705afe435cd5d21a60fadd543a67b4df65de66a5cf7
Deleted: sha256:0a45f6912e8188a2ef73537590483f7284f12856aab29263bb03f9a41f5d86e8
Deleted: sha256:046c6fab78820fb8d39f3c502a882a71841fdbc23ecb18f39bd712e7e9d556bd
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.wuhen:docker-test >------------------------
[INFO] Building docker-test 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- docker-maven-plugin:0.4.13:build (default-cli) @ docker-test ---
[INFO] Copying /var/lib/jenkins/workspace/docker-test/target/docker-test-0.0.1-SNAPSHOT.jar -> /var/lib/jenkins/workspace/docker-test/target/docker/docker-test-0.0.1-SNAPSHOT.jar
[INFO] Copying /var/lib/jenkins/workspace/docker-test/src/main/docker/Dockerfile -> /var/lib/jenkins/workspace/docker-test/target/docker/Dockerfile
[INFO] Building image docker-test
Step 1/5 : FROM hub.c.163.com/dwyane/openjdk:8
---> 96cddf5ae9f1
Step 2/5 : VOLUME /tmp
---> Running in 486c1231762b
Removing intermediate container 486c1231762b
---> 13ba15843faa
Step 3/5 : ADD docker-test-0.0.1-SNAPSHOT.jar app.jar
---> 728f47311940
Step 4/5 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
---> Running in 5e60e14f90b9
Removing intermediate container 5e60e14f90b9
---> fb9135e0b8d3
Step 5/5 : EXPOSE 8081
---> Running in b37736b77608
Removing intermediate container b37736b77608
---> b245fecbacc9
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built b245fecbacc9
Successfully tagged docker-test:latest
[INFO] Built docker-test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.657 s
[INFO] Finished at: 2019-11-19T15:24:50+08:00
[INFO] ------------------------------------------------------------------------
current docker images
docker-test latest b245fecbacc9 Less than a second ago 658MB
start container
6a5195061803f56761fbb4289c6a6035fd3f91afd1057d9c8051cb69c7ef4a75
current container
6a5195061803 docker-test "java -Djava.securit…" 1 second ago Up Less than a second 0.0.0.0:8081->8081/tcp dockertest
star service success!
Finished: SUCCESS
可以結(jié)合參考和我的文章選擇自己合適的配置路線再扭,與其說是寫總結(jié),也是結(jié)合自己弟參考文章的補充