Rancher 2.1针对CI/CD进行了新的升级,带来了更好用的pipeline,这篇文章针对Rancher 2.1版本,讲解如何使用Rancher pipeline来做SpringBoot工程的CI/CD.
演示代码工程准备
首先创建一个SpringBoot的demo工程,push到github上(演示代码工程地址:https://github.com/huangll99/demo)。
Rancher设置代码库
首先需要设置代码仓库的认证,使用github需要创建自己的app,获取Client ID和Client Secret.
在流水线模块选择设置代码库,选择认证&同步代码库,填入申请的Client ID和Client Secret.
添加compile阶段
编辑步骤,使用maven镜像作为编译阶段的基础镜像。使用mvn clean package命令编译打包SpringBoot工程。
添加publish阶段
编辑步骤,将maven打包得到得可执行jar包,构建为docker镜像
dockerfile如下:
FROM 10.3.10.131:5000/gsafety/java:8
MAINTAINER [email protected]
ADD target/demo-0.0.1-SNAPSHOT.jar /app.jar
EXPOSE 8080
ENTRYPOINT ["java","-Xmx300m","-Dserver.port=8080","-jar","app.jar"]
然后推送到镜像仓库,这里使用了自己内部部署得harbor镜像仓库。
添加deploy阶段
使用的deploy.yml如下:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: demo-deployment
spec:
replicas: 1
template:
metadata:
labels:
app: demo
track: stable
spec:
containers:
- name: demo
image: 10.3.10.131:5000/gsafety/demo:v3
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: demo-svc
spec:
type: NodePort
selector:
app: demo
ports:
- protocol: TCP
port: 8080
targetPort: 8080
完整的pipeline配置文件如下:
stages:
- name: compile
steps:
- runScriptConfig:
image: maven:v1
shellScript: mvn clean package
- name: publish
steps:
- publishImageConfig:
dockerfilePath: ./Dockerfile
buildContext: .
tag: gsafety/demo:v3
pushRemote: true
registry: 10.3.10.131:5000
env:
PLUGIN_DEBUG: "true"
PLUGIN_INSECURE: "true"
- name: deploy
steps:
- applyYamlConfig:
path: ./deployment.yaml
timeout: 60
branch:
include:
- master