CICD脚本配置
1.Jenkins
# 安装插件主要是git/pipeline
# pipeline scripts脚本
pipeline {
agent any
tools {
maven "maven3.6.3"
}
stages {
stage('fetch code') {
steps {
git credentialsId: '3658e581-7e42-4466-aca8-1a622c4e2be4', url: 'https://gitee.com/liuyu1985/cicd.git'
}
}
stage('build image') {
steps {
sh "mvn clean package -Dmaven.test.skip=true dockerfile:build"
}
}
stage('push image') {
steps {
sh "mvn -Dmaven.test.skip=true dockerfile:push"
}
}
stage('deploy') {
steps {
sh "kubectl apply -f deployment.yaml"
sh "kubectl delete pods -l app=hr-web"
}
}
stage('delete dangling') {
steps {
sh 'docker rmi $(docker images -f "dangling=true" -q)'
}
}
}
}
2.maven plugin
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-plugin.version}</version>
<configuration>
<repository>${docker.registry}/${docker.image.prefix}/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
3.Dockerfile
# Dockerfile与POM文件同目录
FROM java:8
EXPOSE 8999
ARG JAR_FILE
ADD target/${JAR_FILE} /app.jar
RUN ln -sf /usr/share/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
ENTRYPOINT ["java", "-jar","/app.jar"]
4.Docker Registry
vi /etc/docker/daemon.json
# 添加以下内容
"insecure-registries":["192.168.201.130:5000"]
5.Deployment.yaml配置
apiVersion: apps/v1
kind: Deployment
metadata:
name: hr-web
namespace: default
spec:
selector:
matchLabels:
app: hr-web
replicas: 1
template:
metadata:
labels:
app: hr-web
spec:
containers:
- name: hr-web
image: 192.168.201.130:5000/liuyu/hr-web:1.0.0
imagePullPolicy: Always
resources:
requests:
memory: "256Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "100m"
ports:
- containerPort: 8999
---
apiVersion: v1
kind: Service
metadata:
name: hr-web-svc
namespace: default
labels:
app: hr-web
spec:
ports:
- port: 8999
targetPort: 8999
protocol: TCP
selector:
app: hr-web
type: ClusterIP