Jenkins

Install jenkins on kubernetes

jenkins version:2.263.4
helm charts version:3.1.13
Helm Chart

1
2
3
4
5
6
7
helm repo add jenkins https://charts.jenkins.io

helm install jenkins \
--namespace=jenkins-pipelines \
--set controller.adminPassword=cloudnative \
--set persistence.storageClass=nfs-client \
jenkins/jenkins

Install plugin

Plugin

  • Kubernetes # default install
  • Blue Ocean
  • Gitlab

Jenkinsfile use

在jenkinsfile中使用动态环境变量,末尾一定要加.trim()

1
2
3
4
5
6
7
   environment {
       COMMIT = """${sh(
                returnStdout: true,
                script: 'git rev-list HEAD | wc -l'
            ).trim()}""" 
      
     }

在jenkinsfile中使用非设置的环境变量,记得转义 \$NF

1
sed -i s/`less  Chart.yaml   | grep appVersion | awk '{print \$NF}'`/$TAG$COMMIT/g Chart.yaml    

registry是在jenkins中创建的凭据

1
 docker.withRegistry('https://registry.cn-hangzhou.aliyuncs.com', 'registry')

Jekninsfile on kubernetes

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
pipeline {
  environment { 

     APP_NAME          = "server"
     TAG               = "v2.27.6"
     REGISTRY_URL      = "registry.cn-hangzhou.aliyuncs.com"

}
  
  parameters {

     choice(name: 'DEPLOY_CLUSTER', choices: ['dev', 'test', 'pre', 'prd'], description: 'Which cluster to deploy')
      
}   
  agent {
    kubernetes {
      yaml """
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: jenkins_agent
spec:
  imagePullSecrets:
  - name: aliyun.registry.com
  containers:
  - name: gradle-jdk
    image: gradle:jdk8
    imagePullPolicy: IfNotPresent
    tty: true
    volumeMounts:
    - mountPath: /root/.gradle/
      name: data
    resources:
      requests:
        memory: "1024Mi"
        cpu: "1"
      limits:
        memory: "1024Mi"
        cpu: "1"
  - name: docker
    image: docker
    imagePullPolicy: IfNotPresent
    tty: true
    resources:
      requests:
        memory: "200Mi"
        cpu: "500m"
      limits:
        memory: "200Mi"
        cpu: "500m"
    volumeMounts:
    - mountPath: /var/run/docker.sock
      name: dockersock
  - name: commit
    image: registry.cn-hangzhou.aliyuncs.com/docker-0518/duanyu/commit:stable
    imagePullPolicy: IfNotPresent
    tty: true  
  volumes:
  - name: dockersock
    hostPath:
      path: /var/run/docker.sock
  - name: data
    nfs:
      path: /data/maven/
      readOnly: false
      server: 192.168.22.164
"""
 }
}
  stages {

    stage('build') {

      steps {
        container('gradle-jdk') {
          sh """
            ./gradlew clean build -x test --no-daemon
      """
  }
 }
}
    stage('build_images') {
      
      environment {
        COMMIT = """${sh(
                returnStdout: true,
                script: 'git rev-list HEAD | wc -l'
            ).trim()}""" 
        
        HASH = """${sh(
                returnStdout: true,
                script: 'git rev-parse --short=5 HEAD'
            ).trim()}""" 
}
       
      steps {      
        container('docker') {
          script {
        
            docker.withRegistry("https://${REGISTRY_URL}", "registry") {

            def customImage = docker.build("${REGISTRY_URL}/docker-0518/duanyu/${APP_NAME}:${TAG}_${COMMIT}_${HASH}")

            customImage.push()
            
            sh "docker rmi ${REGISTRY_URL}/docker-0518/duanyu/${APP_NAME}:${TAG}_${COMMIT}_${HASH}"
    }
   }
  }
 }
}    
    stage('commit') {
      
      environment {
        COMMIT = """${sh(
                returnStdout: true,
                script: 'git rev-list HEAD | wc -l'
            ).trim()}""" 
        
        HASH = """${sh(
                returnStdout: true,
                script: 'git rev-parse --short=5 HEAD'
            ).trim()}""" 
}
     
      steps {
        container('commit') {
                
          sh """
                 
           git clone -b ${DEPLOY_CLUSTER}  git@github.com:renhao-0518/config.git
           cd ./config/${APP_NAME}/
           git config --global user.name  "duanyu"
           git config --global user.email "duanyu@tsign.cn"
           sed -i s/`less  Chart.yaml   | grep appVersion | awk '{print \$NF}'`/$TAG${COMMIT}_${HASH}/g  Chart.yaml
           git add .
           git commit -m "update images tag ${APP_NAME}:${TAG}_${COMMIT}_${HASH}"
           
          """
        retry(3) {
          sh """
           cd ./config/${APP_NAME}/
           git pull
           git push
          """   
     }
    }
   }
  }
 }
}