CICD脚本配置

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值