Jenkins+Gitlab+Harbor+k8s 简易流水线

1、jenkins插件安装:

Docker Commons Plugin

CloudBees Docker Hub/Registry Notification  等。

2、jenkins全局工具配置

Jenkins+Gitlab+Harbor+k8s 简易流水线_第1张图片

3、凭证配置,需配置gitlab、harbor的账户凭证

Jenkins+Gitlab+Harbor+k8s 简易流水线_第2张图片

4、harbor中创建xxl-job

Jenkins+Gitlab+Harbor+k8s 简易流水线_第3张图片

5、gitlab中创建Dockerfile和deploy.yml

Jenkins+Gitlab+Harbor+k8s 简易流水线_第4张图片

dockerfile

FROM primetoninc/jdk:1.8
run mkdir -p /opt/admin
COPY xxl-job-admin/target/xxl-job-admin-2.3.0.jar /opt/admin
WORKDIR /opt/admin
CMD java -jar xxl-job-admin-2.3.0.jar

deploy.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: xxl-admin
spec:
  replicas: 1
  selector:
    matchLabels:
      app: xxl-job-admin
  template:
    metadata:
      labels:
        app: xxl-job-admin
    spec:
      containers:
      - name: xxl-job-admin
        imagePullPolicy: IfNotPresent
        image: "harbor.zyc.cmiot/xxl-job/xxl-job-admin:2.3.0"
      nodeName: 10.xx.xx.xx
         
  

6、创建jenkins流水线

Jenkins+Gitlab+Harbor+k8s 简易流水线_第5张图片

pipeline

pipeline{
    agent any
    tools {
        maven 'my-maven'
        jdk 'my-jdk'
    }
    environment {
        DOCKER_CERT_PATH = credentials('b6df91be-f296-48c9-9e3e-f6d96e2877d2')
    }
    stages{
        stage('git clone'){
            steps{
               git branch: 'main', credentialsId: '3b0fa744-7bd1-4e99-903f-56d12339d99d', url: 'http://10.xx.xx.105:8011/root/xxl-job.git'               
            }

        }
        stage('build'){
            steps{
                sh '''
                    mvn clean package
                    docker build -t harbor.zyc.cmiot/xxl-job/xxl-job-admin:2.3.0 .
                '''
            }
        }    
        stage('push image to harbor'){
            steps{
                sh '''
                    docker push harbor.zyc.cmiot/xxl-job/xxl-job-admin:2.3.0
                '''
            }
        }
        stage('deploy'){
            steps{
                sh '/opt/kube/bin/kubectl delete -f deploy.yml && /opt/kube/bin/kubectl apply -f deploy.yml'
            }
        }
    }
}

说明:docker将自动使用DOCKER_CERT_PATH来登录harbor,无需在进行docker login登录

7、运行流水线结果

(1)流水线阶段视图

Jenkins+Gitlab+Harbor+k8s 简易流水线_第6张图片

(2)harbor仓库

Jenkins+Gitlab+Harbor+k8s 简易流水线_第7张图片

(3)k8s pod

Jenkins+Gitlab+Harbor+k8s 简易流水线_第8张图片

参考

1、docker流水线凭证:https://plugins.jenkins.io/docker-commons/

2、https://blog.csdn.net/shykevin/article/details/100165402

你可能感兴趣的:(jenkins,docker,jenkins,k8s)