pipeline 普通示例

agent pod template

KubernetesPod.yaml

# 定义 jenkins agent pod template
apiVersion: v1
kind: Pod
cloud: ekstest  # 指定 eks 集群名称,在该集群中 启动 jenkins agent 来进行构建
# // 必须配置
nodeSelector:
  NetworkType: Private
metadata:
  labels:
    some-label: some-label-value
spec:
  serviceAccount: 'jenkins-agentd-pod-service-account'
  securityContext:
    runAsUser: 0

  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: 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
    # 挂载 docker sock,这样docker就可以进行构建镜像了。
    - 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'

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

Jenkinsfile 示例

Jenkinsfile

pipeline {
  agent {
    kubernetes {
    		// 引用 KubernetesPod.yaml pod 配置文件
        yamlFile 'KubernetesPod.yaml'
    }
  }

    	// 定义全局变量
    environment {
        //顶层流水线块中使用的 environment 指令将适用于流水线中的所有步骤。
        // 获取 dingTalk 的密文 
        // credentials 官方链接:https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation
        DINGTALK_CREDS = credentials('97bd1f06-a2c7-48fa-83aa-b10de2c6a2fd')

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

  stages {
    stage('printenv') {
        steps {
        // 输出环境变量
        sh 'printenv'
        }
    }
    stage('check and 拉取代码') {
        steps {
        		// 拉取代码,checkout scm 方式
            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 . -t ${IMAGE_REPO}:${GIT_COMMIT}'
                    // sh 'docker push ${IMAGE_REPO}:${GIT_COMMIT}'
                }
            }
        }
    }
  }
  post {
  // 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('busybox'){
                echo 'I will always say Hello again!'
            }
        }
    }
}

jenkins 页面配置

基于 webhook 的触发,scm pipeline 配置

image-20220728161046365

image-20220728161122310

image-20220728161152606

image-20220728161213581