本文只适合初学者搭建部署环境,大致熟悉 jenkins
工作流程,不适合生产环境部署,高手莫进。
1、本地创建一个 springboot-demo
示例微服务(参考:https://start.spring.io)
2、将代码上传到码云,方便后面 Jenkins
自动构建
3、在 Centos7
安装 Docker
,然后再在 Docker
容器安装 jdk、maven、jenkins
4、通过挂载的方式,Jenkins
使用宿主机中的 jdk、maven
环境
5、使用 Jenkins
自动构建,完成微服务 编译、打包、部署到docer容器、启动容器
6、访问服务,验证结果
1、springboot-demo
项目中创建 Dockerfile
文件。
Dockerfile
文件
FROM java:8-jre
MAINTAINER Simon <[email protected]>
ADD ./target/springboot-demo-1.0.jar /app/
CMD ["java", "-Xmx200m", "-jar", "/app/springboot-demo-1.0.jar"]
EXPOSE 8888
build.sh
文件
#!/bin/bash
# 停止容器
sudo docker stop springboot-demo
# sleep 2s
# 删除容器
sudo docker rm springboot-demo
# 删除镜像
sudo docker rmi springboot/demo:1.0
# 构建镜像 . 号表示在当前目录构建
sudo docker build -t springboot/demo:1.0 .
# 创建容器并启动
sudo docker run -d -p 8888:8888 --name springboot-demo springboot/demo:1.0
pom.xml
文件
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-clean-plugin
2.4.1
com.spotify
docker-maven-plugin
1.0.0
springboot-demo
src/main/docker
/
${project.build.directory}
${project.build.finalName}.jar
HelloWorld.java
文件
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
public class HelloWorld{
@GetMapping("/hello")
public void helloworld(HttpServletResponse response) throws IOException {
response.getWriter().write("Hello Spring-boot");
}
}
2、将代码上传到码云
这个很简单了,有个仓库地址、账号、密码就可以了。后面要用
3、安装 Docker
Docker 要求 CentOS 系统的内核版本高于 3.10, 通过 uname -r 命令查看你当前的内核版本
uname -r
使用 root 权限登录Centos。确保 yum 包更新到最新。
sudo yum update
设置 yum 源
sudo yum-config-manager --add-repo
https://download.docker.com/linux/centos/docker-ce.repo
安装 Docker
sudo yum install docker-ce #由于repo中默认只开启stable仓库,故这里安装的是最新稳定版17.12.0
4、安装 Jdk、Maven
请参考 Linux下安装Jdk、Maven
5、安装 Jenkins
首先,创建一个目录
sudo mkdir /var/local/jenkins
新建 Dockerfile
文件
FROM jenkins/jenkins:2.138.4
USER root
ARG dockerGid=999
RUN echo "docker:x:${dockerGid}:jenkins" >> /etc/group
RUN apt-get update && apt-get install -y sudo && rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y libltdl7 && apt-get update
RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers
USER jenkins
根据 Dockerfile
生成 Docker
镜像
docker build -t jenkins/jenkins:1.0 .
创建 Jenkins
容器
sudo docker run -d -p 8080:8080 --name jenkins \
-v /usr/bin/docker:/usr/bin/docker \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/bin/mv:/usr/bin/mv \
-v /var/local/jdk1.8.0_191/bin/java:/usr/bin/jdk1.8.0_191/java \
-v /var/local/jdk1.8.0_191:/var/local/jdk1.8.0_191 \
-v /var/local/apache-maven-3.6.0:/var/local/apache-maven-3.6.0 \
-v /var/jenkins_home:/var/jenkins_home jenkins/jenkins:1.0
-d 后台运行镜像
-p 8080:8080 将镜像的8080端口映射到服务器的8080端口
-p 50000:50000 将镜像的50000端口映射到服务器的50000端口
-v jenkins:/var/jenkins_home /var/jenkins_home目录为jenkins工作目录,我们将硬盘上的一个目录挂载到这个位置,方便后续更新镜像后继续使用原来的工作目录。
-v /etc/localtime:/etc/localtime 让容器使用和服务器同样的时间设置。
–name jenkins 给容器起一个别名
配置jdk、maven
环境,使用宿主机上的即可。
除了前面推荐安装好的插件,还需要安装如下插件
Maven Integration plugin
docker-build-step
6、开始构建项目
好了,终于可以 开始构建
了
Started by timer
Building in workspace /var/jenkins_home/workspace/demo
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://gitee.com/****/spring-boot-demo.git # timeout=10
Fetching upstream changes from https://gitee.com/****/spring-boot-demo.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials
> git fetch --tags --progress https://gitee.com/****/spring-boot-demo.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 13bfe1e75031f9f02e464cb4c4d464f3e50f4875 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 13bfe1e75031f9f02e464cb4c4d464f3e50f4875
Commit message: "无"
> git rev-list --no-walk 13bfe1e75031f9f02e464cb4c4d464f3e50f4875 # timeout=10
[demo] $ /bin/sh -xe /tmp/jenkins8567218160464441007.sh
+ sudo docker stop springboot-demo
springboot-demo
+ sudo docker rm springboot-demo
springboot-demo
+ sudo docker rmi springboot/demo:1.0
Untagged: springboot/demo:1.0
Deleted: sha256:08c8109be90dce081b9a6db4e3f489bb4e48be2b2eb9edae8a9070942e6086e3
Deleted: sha256:4e86c12aea4c04f91805a0d21e459935081f6448c6bb09f035e0462a48a4a6db
Deleted: sha256:2d77ea9f19ed915536fd3a37a0158d461f508c010dcc64c0bee67ec1b3842498
Deleted: sha256:584d253c6b0440f6e5c3676339f86a6a7b5ad448dd151897e6b1750a3594afad
Deleted: sha256:aa88fba4f8c30c0ca2d8e1bca3ad08a29bb2d0ffe71200889dc3c441389c1ee5
+ sudo docker build -t springboot/demo:1.0 .
Sending build context to Docker daemon 16.51MB
Step 1/5 : FROM java:8-jre
---> e44d62cf8862
Step 2/5 : MAINTAINER Simon <[email protected]>
---> Running in 0884b18b9b5a
Removing intermediate container 0884b18b9b5a
---> 91dba320d565
Step 3/5 : ADD ./target/demo-1.0.jar /app/
---> 12030cfc7a56
Step 4/5 : CMD ["java", "-Xmx200m", "-jar", "/app/demo-1.0.jar"]
---> Running in de53ba5d6fec
Removing intermediate container de53ba5d6fec
---> a4ca0fff8ed1
Step 5/5 : EXPOSE 8888
---> Running in 14135924a6c7
Removing intermediate container 14135924a6c7
---> f469398cc18a
Successfully built f469398cc18a
Successfully tagged springboot/demo:1.0
Parsing POMs
Established TCP socket on 41866
[demo] $ /var/local/jdk1.8.0_191/bin/java -cp /var/jenkins_home/plugins/maven-plugin/WEB-INF/lib/maven35-agent-1.12.jar:/var/local/apache-maven-3.6.0/boot/plexus-classworlds-2.5.2.jar:/var/local/apache-maven-3.6.0/conf/logging jenkins.maven3.agent.Maven35Main /var/local/apache-maven-3.6.0 /var/jenkins_home/war/WEB-INF/lib/remoting-3.25.jar /var/jenkins_home/plugins/maven-plugin/WEB-INF/lib/maven35-interceptor-1.12.jar /var/jenkins_home/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.12.jar 41866
<===[JENKINS REMOTING CAPACITY]===>channel started
Executing Maven: -B -f /var/jenkins_home/workspace/demo/pom.xml clean package -T 1C -Dmaven.test.skip=true -Dmaven.compile.fork=true
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< com.demo:demo >----------------------------
[INFO] Building spring-boot-demo 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ demo ---
[INFO] Deleting /var/jenkins_home/workspace/demo/target
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /var/jenkins_home/workspace/demo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ demo ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ demo ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ demo ---
[INFO] Tests are skipped.
[WARNING] Attempt to (de-)serialize anonymous class hudson.maven.reporters.BuildInfoRecorder$1; see: https://jenkins.io/redirect/serialization-of-anonymous-classes/
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ demo ---
[INFO] Building jar: /var/jenkins_home/workspace/demo/target/demo-1.0.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.6.RELEASE:repackage (default) @ demo ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.343 s
[INFO] Finished at: 2018-12-09T12:28:25Z
[INFO] ------------------------------------------------------------------------
Waiting for Jenkins to finish collecting data
[JENKINS] Archiving /var/jenkins_home/workspace/demo/pom.xml to com.demo/demo/1.0/demo-1.0.pom
[JENKINS] Archiving /var/jenkins_home/workspace/demo/target/demo-1.0.jar to com.demo/demo/1.0/demo-1.0.jar
[demo] $ /bin/sh -xe /tmp/jenkins1802648720419273571.sh
+ sudo docker run -d -p 8888:8888 --name springboot-demo springboot/demo:1.0
channel stopped
e20c5911ec02036c621cf43e0cedab1bad78d371ec637f330ea800ac996a080d
Finished: SUCCESS
好了,本文大致就演示了使用 Jenkins
构建微服务的过程,构建还有很多配置,比如定时构建(当发现 git
仓库代码有更新的时候自动构建服务)等,有兴趣可以继续深入学习。
1、docker 容器 iptables: No chain/target/match by that name.
2、Jenkins源码管理出现 Failed to connect to repository,Could not resolve host: gitee.com
3、该Jenkins实例似乎已离线
4、java.io.IOException: Cannot run program “docker”: error=2, No such file or directory
5、Jenkins 任务中找不到 构建一个maven项目
6、Jenkins源码管理出现 Failed to connect to repository,Authentication failed