Pipeline oa-CI

1. 准备源码

2. 编写dockfile

[admin@master01 oa ]$vim Dockerfile 
FROM  harbor.xx.com/library/tomcat:centos7-jdk8

ADD oa_platform/oa_console_web/target/ROOT.war webapps/ROOT
COPY unzip-6.0-24.el7_9.x86_64.rpm . 
RUN  rpm -ivh unzip-6.0-24.el7_9.x86_64.rpm && rm -rf unzip-6.0-24.el7_9.x86_64.rpm && cd webapps/ROOT && unzip ROOT.war && rm -rf ROOT.war

3. 编写 cat deploy.yaml

[admin@master01 oa ]$vim deploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: oa
  namespace: {NameSpace}                        #修改为{}特殊字符,后期好替换
spec:
  replicas: 1
  selector:
    matchLabels:
      app: bms
  template:
    metadata:
      labels:
        app: bms
    spec:
      imagePullSecrets:
      - name: harbor-admin
      containers:
      - name: oa
        image: {Image}                
        ports:
        - name: http
          containerPort: 8080
        env:            # 传递初始堆内存和最大堆内存占用
        - name: JAVA_OPTS
          value: "-server -Xms${JVM_XMS:-256m} -Xmx${JVM_XMX:-256m} -XX:+UseConcMarkSweepGC -Dsys.env=prod"
        - name: JVM_XMS
          valueFrom:
            resourceFieldRef:
              resource: requests.memory
        - name: JVM_XMX
          valueFrom:
            resourceFieldRef:
              resource: limits.memory
        resources:
          requests:
            memory: 2048Mi
          limits:
            memory: 2048Mi
        readinessProbe:         # 就绪探针;如果端口不存活,则从负载均衡中移除
          tcpSocket:
            port: http          # http是一个名字;它会获取这个名字对应的端口;
          initialDelaySeconds: 10
          failureThreshold: 3
        livenessProbe:          # 存活探针;获取url,状态码不对那么则触发重启操作
          tcpSocket:
            port: http         
          #httpGet:
            #path: /
            #port: http
          initialDelaySeconds: 10
          failureThreshold: 3
---
apiVersion: v1
kind: Service
metadata:
  name: oa-svc
  namespace: {NameSpace}
spec:
  selector:
    app: bms
  ports:
  - port: 8080
    targetPort: 8080

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: oa-ingress
  namespace: {NameSpace}
spec:
  ingressClassName: "nginx"
  rules:
  - host: "{host}"
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: oa-svc
            port:
              number: 8080

3. 上传代码

[admin@master01 oa ]$git add . 
[admin@master01 oa ]$git commit -m 'add xxx'
[admin@master01 oa ]$git push origin master

4. 编写Pipeline oa-CI脚本

pipeline {
	agent {
	  kubernetes {
		cloud 'kubernetes'

    yaml '''
      apiVersion: v1
      kind: Pod
      spec:
        imagePullSecrets:
        - name: harbor-admin
        volumes:
        - name: data
          nfs:
            server: 192.168.4.26
            path: /data/maven
        - name: dockersock
          hostPath:
            path: /run/docker.sock

        containers:
        - name: maven
          image: harbor.xx.com/ops/maven:3.8.6
          imagePullPolicy: IfNotPresent
          command: ["cat"]
          tty: true
          volumeMounts:
          - name: data
            mountPath: /root/.m2

        - name: nodejs
          image: harbor.xx.com/ops/nodejs:14.20
          imagePullPolicy: IfNotPresent
          command: ["cat"]
          tty: true

        - name: sonar
          image: harbor.xx.com/ops/sonar-scanner:2.3.0 
          imagePullPolicy: IfNotPresent
          command: ["cat"]
          tty: true  

        - name: docker
          image: harbor.xx.com/ops/docker:20.10
          imagePullPolicy: IfNotPresent
          command: ["cat"]
          tty: true  
          volumeMounts:
          - name: dockersock
            mountPath: /run/docker.sock
        
        - name: kubectl
          image: harbor.xx.com/ops/kubectl:1.22.2
          imagePullPolicy: IfNotPresent
          command: ["cat"]
          tty: true  


     '''
	  }
	}

  environment {
    gitlabID = "gitlab-root-token"
    gitlabPro = "http://gitlab.xx.com/dev/staroffice.git"


  //Harbor相关的全局变量
  Url = "harbor.xx.com"
  Pro = "oa"
  ImageName = "${Url}/${Pro}/staroffice"
  HARBOR_ID = "harbor-admin"

  //对外暴露的域名
  ingressHost = "oa.xx.com"
  }

// 实现SpringBoot应用的CI

    stages {
        stage('获取代码') {
            steps {
                container('maven') {
                  checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: "${gitlabID}", url: "${gitlabPro}"]])                }
            }
        }


        stage('编译代码') {
            steps {
                container('maven') {
            
                    //sh 'mvn package -Dmaven.test.skip=true'
                    //sh 'mvn clean package -DskipTests  -Pprod install -f staroffice_platform/pom.xml'
                    sh 'mvn clean install -Dmaven.test.skip=true -Pprod  -f staroffice_platform/pom.xml '
                }
            }
        }


    stage('生成镜像Tag'){
      steps {
        container('maven') {
          script {
            //本次git提交的commid     (git log -n1 --pretty=format:'%h')
            env.COMMITID = sh(returnStdout: true, script: "git log -n1 --pretty=format:'%h'").trim()
            //构建的时间   (date +%Y%m%d_%H%M%S)
            env.BuildTime = sh(returnStdout: true, script: "date +%Y%m%d_%H%M%S").trim()
            
            //完整的镜像Tag   (c106654_20221115_133911)
            env.ImageTag = COMMITID + "_" +  BuildTime
          }
          
          sh 'echo "镜像的Tag: ${ImageTag}"'
        }
      }
    }


        stage('制作Docker镜像') {
            steps {
                container('docker') {
                  withCredentials([usernamePassword(credentialsId: "${HARBOR_ID}", passwordVariable: 'HARBOR_PASSWORD', usernameVariable: 'HARBOR_USER')]) {
                  //登录harbor
                  sh "echo ${HARBOR_PASSWORD} | docker login $Url -u ${HARBOR_USER} --password-stdin"
                  //构建镜像
                  sh "docker build -t ${ImageName}:${ImageTag} ."
                  //推送镜像
                  sh "docker push  ${ImageName}:${ImageTag}"
                  //删除镜像
                  sh "docker rmi ${ImageName}:${ImageTag}"
                 
                  }
                }
            }
        }

        stage('交付应用至K8S') {
            steps {
                container('kubectl') {
                  withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
                    sh 'mkdir ~/.kube && cp ${KUBECONFIG} ~/.kube/config'
                    sh 'sed -i "s#{NameSpace}#oa#g" deploy.yaml'
                    sh 'sed -i "s#{Image}#${ImageName}:${ImageTag}#g" deploy.yaml'
                    sh 'sed -i "s#{host}#${ingressHost}#g" deploy.yaml'
                    sh 'cat deploy.yaml'
                    sh 'kubectl apply -f deploy.yaml'

                  }
                }
            }
        }




    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值