1. 程式人生 > 其它 >jenkins 動態slave配置(3) demo

jenkins 動態slave配置(3) demo

技術標籤:JenkinsKubernetes

pipeline {
  agent {
    kubernetes {
      yaml """
apiVersion: v1
kind: Pod
metadata:
  labels:
    some-label: some-label-value
spec:
  containers:
  - name: alpine
    image: registry.cn-shanghai.aliyuncs.com/devobs/tpl:alpine-00
    command:
    - cat
    tty: true
    volumeMounts:
      - name: docker
        mountPath: /var/run/docker.sock
      - name: ku
        mountPath: /usr/bin/kubectl
      - name: bui
        mountPath: /usr/bin/docker

  volumes:
  - name: docker
    hostPath:
      path: /var/run/docker.sock
  - name: ku
    hostPath:
      path: /usr/bin/kubectl
  - name: bui
    hostPath:
      path: /usr/bin/docker
"""
    }
  }

options{  
timestamps ()
disableConcurrentBuilds()
}

 environment {
   name="alpinevv"
   image="registry.cn-shanghai.aliyuncs.com/devobs/test:${name}$BUILD_ID"
}

  stages {
    stage('get code ') {
      steps {
       checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'http://172.19.0.53:13000/admins/tests.git']]])
       
        container('alpine') {
            withCredentials([usernamePassword(credentialsId: 'dockerop', passwordVariable: 'Password', usernameVariable: 'Username')]) {
           sh 'docker login -u$Username -p$Password registry.cn-shanghai.aliyuncs.com '
          sh 'apk add wget'
         sh 'wget --http-user=upload_02  
[email protected]
-c http://172.19.0.50:7890/down/ex_probeereuka.jar' sh ''' cat <<EOF> Dockerfile FROM nginx:alpine ENV TZ='Asia/Shanghai' ENV TIMEZONE Asia/Shanghai RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories && \ echo "http://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories && \ apk update && apk upgrade && \ apk add tzdata bash-doc bash && \ ln -snf /usr/share/zoneinfo/$TIMEZONE /etc/localtime && \ echo $TIMEZONE > /etc/timezone && \ mkdir /lib64 && \ ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2 EOF docker build -t ${name}:${BUILD_ID} . docker tag ${name}:$BUILD_ID ${image} docker push ${image} echo ${image} docker rmi ${image} docker rmi ${name}:${BUILD_ID} echo ${name}:${BUILD_ID} ''' } } } } stage('deploy') { steps { container('alpine') { sh ''' cat <<EOF> k8s.tpl apiVersion: apps/v1 kind: Deployment metadata: name: {{name}}-test namespace: test labels: version: v1 app: {{name}} spec: replicas: 1 selector: matchLabels: version: v1 app: {{name}} template: metadata: labels: version: v1 app: {{name}} spec: containers: - name: {{name}} image: {{image}} ports: - containerPort: 80 env: - name: aliyun_logs_catalina value: "stdout" - name: aliyun_logs_access value: "/tmp/*.log" volumeMounts: - name: log mountPath: /tmp/ volumes: - name: log emptyDir: {} --- apiVersion: v1 kind: Service metadata: name: {{name}}-test namespace: test spec: #type: NodePort selector: version: v1 app: {{name}} ports: - port: 80 targetPort: 80 #nodePort: 80 EOF ''' sh "sed -e 's#{{name}}#${name}#g;s#{{image}}#${image}#g' ./k8s.tpl > k8s.yaml" sh 'kubectl apply -f k8s.yaml' } } } } }