jenkins + gitlab 自动构建

jenkins 部分

本示例是 jenkins 部署在 k8s 集群中

安装插件

  1. 汉化插件

    Jenkins -> manage Jenkins -> Plugin Manager -> Avaliable,输入chinese自动搜索

    image-20220723213951266

  2. kubernetes 插件

    Jenkins -> manage Jenkins -> Plugin Manager -> Avaliable,输入kubernetes自动搜索

    image-20220726170827529

配置kubernets插件

[系统管理] -> [配置集群] -> [Add a new cloud]

kubenetes 地址:https://kubernetes.default.svc.cluster.local

image-20220912205850348

ekstest		# 后边 pod 中会指定该 eks 名称
https://kubernetes.default.svc.cluster.local		# 与 eks 通信地址
http://jenkins.default.svc.cluster.local:8080		# jenkins 通信地址
jenkins-agent:50000			# agent 通信地址

点击测试,出现 k8s 版本信息为已经连接上,如果链接不上,查看 jenkins sa 配置;

jenkins 配置 pipeline 项目

image-20220912110953675

image-20220912210026619

链接记住,gitlab 上会写,链接下边还有个密钥,记得记住

gitlab 部分

创建项目,上传代码

image-20220912210141789

配置 webhook

image-20220912210210787

image-20220912210353478

到这里就配置完了,

pipeline + kubernetes 插件部分

KubernetesPod.yaml

apiVersion: v1
kind: Pod
cloud: ekstest  # 指定 eks 集群名称,在该集群中 启动 jenkins agent 来进行构建
# // 必须配置
nodeSelector:
  NetworkType: Private
metadata:
  labels:
    some-label: some-label-value
spec:
  # sa 如果需要 aws 权限,需要绑定 aws 用户,可以参考链接
  # https://docs.aws.amazon.com/zh_cn/eks/latest/userguide/specify-service-account-role.html
  serviceAccount: 'jenkins-agentd-pod-service-account'
  securityContext:
    runAsUser: 0
  resources:
  limits:
    cpu: 1
    memory: 1Gi
  requests:
    cpu: 0.5
    memory: 1Gi

  containers:
  - name: maven
    image: maven:3.8.1-jdk-8
    command:
    - cat
    tty: true
    volumeMounts:
    - mountPath: /root/.m2/repository
      name: maven-repository

    # - mountPath: /root/.m2
    #   name: maven-cnf

  - name: busybox
    image: busybox:stable
    command:
    - cat
    tty: true

  - name: amazoncli
    image: amazon/aws-cli
    command:
    - cat
    tty: true

  - name: docker
    image: docker
    command:
    - cat
    tty: true
    volumeMounts:
    - mountPath: /etc/docker/daemon.json
      name: docker-daemon
    - mountPath: /var/run/docker.sock
      name: docker-sock

  volumes:
  - name: docker-sock
    hostPath:
      path: /var/run/docker.sock

  - name: docker-daemon
    hostPath:
      path: /etc/docker/daemon.json

  - name: maven-repository
    # persistentVolumeClaim:
    #   claimName: 'jenkins-agent-pvc'
    hostPath:
      path: /tmp/maven

  # - name: maven-cnf
  #   configMap:
  #     name: maven-config

Jenkinsfile

pipeline {
  agent {
    kubernetes {
    		// 引用 根目录下的 KubernetesPod.yaml
        yamlFile 'KubernetesPod.yaml'
    }
  }


    environment {
        //顶层流水线块中使用的 environment 指令将适用于流水线中的所有步骤。
        // 获取 dingTalk 的密文; jenkins 中一定要有这个变量,不然报错
        DINGTALK_CREDS = credentials('57fb015f-6060-4251-9813-80c26087e99b')

        // docker 镜像库地址
        IMAGE_REPO = "172.21.32.13:5000/demo/myblog"
    }

  stages {
    stage('printenv') {
        steps {
        sh 'printenv'
        }
    }
    stage('check and 拉取代码') {
        steps {
            checkout scm
        }
    }
//     // stage('Run maven') {
//     //   steps {
//     //     container('maven') {
//     //       sh 'mvn -version'
//     //     }
//     //     // container('busybox') {
//     //     //   sh '/bin/busybox'
//     //     // }
//     //   }
//     // }
    stage('build-image and push') {
        steps {
            container('docker') {
                retry(2) { 
                    // sh 'sleep 36000'
                    sh 'docker build --network=host . -t ${IMAGE_REPO}:${GIT_COMMIT}'
                    // sh 'docker push ${IMAGE_REPO}:${GIT_COMMIT}'
                }
            }
        }
    }
  }
  post {
        success {
            container('amazoncli'){
                echo 'Congratulations!'
                sh """
                    curl 'https://oapi.dingtalk.com/robot/send?access_token=${DINGTALK_CREDS_PSW}' \
                        -H 'Content-Type: application/json' \
                        -d '{"msgtype": "text",
                                "text": {
                                    "content": "jenkins-hk \n 😄👍构建成功👍😄\n 项目名称: ${JOB_BASE_NAME}\n 构建地址:${RUN_DISPLAY_URL}\n PipelineLog: ${RUN_ARTIFACTS_DISPLAY_URL}"
                                }
                            }'
                """
            }
        }
        failure {
            container('amazoncli'){
                echo 'Oh no!'
                sh """
                    curl 'https://oapi.dingtalk.com/robot/send?access_token=${DINGTALK_CREDS_PSW}' \
                        -H 'Content-Type: application/json' \
                        -d '{"msgtype": "text",
                                "text": {
                                    "content": "jenkins-hk \n ❌构建失败❌\n 项目名称: ${JOB_BASE_NAME}\n 构建地址:${RUN_DISPLAY_URL}\n PipelineLog: ${RUN_ARTIFACTS_DISPLAY_URL}"
                                }
                            }'
                """
            }
        }
        always {
            container('amazoncli'){
                echo 'I will always say Hello again!'
            }
        }
    }
}

Dockerfile

FROM python:3.9
WORKDIR /src
ADD . /src
RUN pip install -r requirements.txt
CMD python app.py

测试

git代码库修改完代码,看能不能触发。