How to Setup Jenkins Build Agents on Kubernetes Pods

来自linux中国网wiki
Evan讨论 | 贡献2024年9月3日 (二) 01:48的版本 →‎问题 log
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索


trouble

Jenkinsfile

pipeline {
  agent {
    kubernetes {
      cloud 'Kubernetes'
      yaml '''
        apiVersion: v1
        kind: Pod
        spec:
          containers:
          - name: jnlp
            image: 'registry.cn-hangzhou.aliyuncs.com/evan886/my_k8s_containers:inbound-agentv2024'
            args: ['\$(JENKINS_SECRET)', '\$(JENKINS_NAME)']
          - name: maven
            image: registry.cn-hangzhou.aliyuncs.com/evan886/my_k8s_containers:maven3.2.3-jdk-8
            command:
            - cat
            tty: true
            
          - name: docker
            //image: m.daocloud.io/docker.io/library/docker:latest
            image: registry.cn-beijing.aliyuncs.com/citools/docker:19.03.9-git
            command:
            - cat
            tty: true
            volumeMounts:
            - mountPath: "/var/run/docker.sock"
              name: sock
          - name: kubectl
            image: m.daocloud.io/docker.io/bitnami/kubectl:1.27.4
            command:
            - cat
            tty: true
            securityContext:
              runAsUser: 0
          volumes:
          - name: sock
            hostPath:
              path: "/var/run/docker.sock"
        '''
    }
  }
  environment {
     gitlab_repo_url = 'http://192.168.10.103/kubernetes/java-web-demo.git'
     gitlab_credentials_id = '42c79e35-fc3e-4067-8bbf-a5e3cc0669a8'
     harbor_registry = '192.168.10.104'
     harbor_credentials_id = 'HARBOR_ACCOUNT'
     harbor_image = '${harbor_registry}/java-web-demo/web'
     k8s_kubeconfig_id='4491d2aa-375a-459b-af0b-1278e70c6d31'
     mail_notification_recipients = '[email protected],[email protected]'
  }
  parameters {
      gitParameter branch: '', branchFilter: '.*', defaultValue: 'dev', description: '请选择发布的分支名', name: 'Branch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'GitParameterDefinition'
  }
  stages {
    stage('拉取代码') {
      steps {
        checkout scmGit(branches: [[name: "${params.Branch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${gitlab_credentials_id}", url: "${gitlab_repo_url}"]])
      }
    }
    stage('代码编译') {
      steps {
          container('maven') {
            sh """
               # 配置阿里云Maven仓库,提高依赖文件下载速度
               sed -i "s#<mirrors>#<mirrors>\
               <mirror>\
                 <id>aliyunmaven</id>\
                 <url>https://maven.aliyun.com/repository/public</url>\
                 <mirrorOf>*</mirrorOf>\
               </mirror>#" /usr/share/maven/conf/settings.xml

               mvn clean package
            """
          }
      }
    }
    stage('构建镜像') {
      steps {
          container('docker') {
            withCredentials([usernamePassword(credentialsId: "${harbor_credentials_id}", passwordVariable: 'password', usernameVariable: 'username')]) {
               sh """
                  docker version
                  image=${harbor_image}:${BUILD_NUMBER}  # 完整镜像地址
                  docker build -t \${image} .   # 根据当前目录Dockerfile文件构建镜像
                  //docker login -u ${username} -p '${password}' ${harbor_registry}  # 推送镜像之前先登录
                  //docker push \${image}   # 推送镜像到Harbor镜像仓库
               """
            }
          }
      }
    }



==


failed to set up watcher on kubernetes  
web-pipeline-demo-6-17h0h-lrwnf-8l4w5’ is offline

今天的书写少一行  参考了去年的书搞定 
办法  添加多 cloud 'Kubernetes' 这一行 Credentials

pipeline {
  agent {
    kubernetes {
      cloud 'Kubernetes'
      yaml '''
        apiVersion: v1
        kind: Pod
        metadata:
          labels:
            app: jenkins-slave
        spec:
          containers:
          - name: jnlp
            image: 'registry.cn-hangzhou.aliyuncs.com/evan886/my_k8s_containers:inbound-agentv2024'
            args: ['\$(JENKINS_SECRET)', '\$(JENKINS_NAME)']
        '''
    }
  }

    stages {
        stage('Main') {
            steps {
                echo 'HI evan'
                sh 'hostname'
            }
        }
    }
}


问题 log

 docker version
Client: Docker Engine - Community
 Version:           19.03.9
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        9d988398e7
 Built:             Fri May 15 00:22:47 2020
 OS/Arch:           linux/amd64
 Experimental:      false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[Pipeline] }

最笨办法 在node上安装上docker 

references

https://hub.docker.com/r/jenkins/inbound-agent/

https://plugins.jenkins.io/kubernetes/