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

1、jenkins插件安装:

Docker Commons Plugin

CloudBees Docker Hub/Registry Notification  等。

2、jenkins全局工具配置

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

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

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

4、harbor中创建xxl-job

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

5、gitlab中创建Dockerfile和deploy.yml

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

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 简易流水线

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 apply -f deploy.yml'
            }
        }
    }
}

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

7、运行流水线结果

(1)流水线阶段视图

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

(2)harbor仓库

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

(3)k8s pod

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

参考

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

上一篇:企业实战(6)修改Harbor镜像仓库默认存储路径


下一篇:【Docker初探】第十节:部署Docker本地镜像仓库Harbor(Helm方式,Arm64与X86_64)