MeterSphere集成pipeline流水线


  • 第一步:Jenkins安装meterSphere插件https://github.com/metersphere/jenkins-plugin/releases
  • 第二步:配置pipeline:目前这个插件对声明式 pipeline 语法支持有问题。可以先用脚本式 pipeline ,或者在声明式 pipeline 中增加 script 步骤。
    参考如下
pipeline {
  stages {
    stage("xxx") {
      steps{
        script {
          metersphere xxxxxxx
        }
      }
    }
  • 本人工程中的pipeline:
pipeline {
  agent {
    node {
      label 'maven'
    }

  }
  stages {
    stage('拉取代码') {
      steps {
        container('maven') {
          git(url: 'https://gitee.com/houchengwei/ttc.git', credentialsId: 'gitee', branch: 'dev', changelog: true, poll: false)
        }

      }
    }

    stage('项目编译') {
      steps {
        container('maven') {
          sh 'mvn clean package -Dmaven.test.skip=true'
          sh 'ls'
        }

      }
    }

    stage('构建镜像') {
      steps {
        container('maven') {
          sh '''ls
cd target
ls'''
          sh 'docker build -t ttc:latest -f Dockerfile ./'
        }

      }
    }

    stage('推送镜像') {
      steps {
        container('maven') {
          withCredentials([usernamePassword(credentialsId : 'ali-docker-hub' ,passwordVariable : 'DOCKER_PWD_VAR' ,usernameVariable : 'DOCKER_USER_VAR' ,)]) {
            sh 'echo "$DOCKER_PWD_VAR" | docker login $REGISTRY -u "$DOCKER_USER_VAR" --password-stdin'
            sh 'docker tag ttc:latest registry.cn-hangzhou.aliyuncs.com/houchengwei/ttc-dev:SNAPSHOT-$BUILD_NUMBER'
            sh 'docker push registry.cn-hangzhou.aliyuncs.com/houchengwei/ttc-dev:SNAPSHOT-$BUILD_NUMBER'
          }

        }

      }
    }

    stage('部署到k8s') {
      steps {
        container('maven') {
          withCredentials([kubeconfigFile(credentialsId : 'demo-kubeconfig' ,variable : 'KUBECONFIG' )]) {
            sh 'envsubst < deploy.yaml | kubectl apply -f -'
          }

        }

      }
    }
    stage ('MeterSphere test') {
      steps {
        script {
           meterSphere method: 'testPlan', 
            mode: 'serial',  
            msAccessKey: 'OxAndz9rgaptCfed', 
            msEndpoint: 'http://124.223.33.148:8081/', 
            msSecretKey: 'gyo7HrmaOLDsR0k7',  
            projectId: '7e32a382-7e45-11ec-8cf6-0242ac1e0a02', 
            resourcePoolId: '',  
            result: 'jenkins', 
            testCaseId: '',  
            testPlanId: 'c610e64f-4b39-4cd6-a2a5-b3a070902fb9',  
            testPlanName: '',  
            workspaceId: '7e32893f-7e45-11ec-8cf6-0242ac1e0a02' 
        }
    }
    }

  }
  environment {
    DOCKER_CREDENTIAL_ID = 'dockerhub-id'
    GITHUB_CREDENTIAL_ID = 'github-id'
    KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
    REGISTRY = 'registry.cn-hangzhou.aliyuncs.com'
    DOCKERHUB_NAMESPACE = 'houchengwei'
    GITHUB_ACCOUNT = 'kubesphere'
    APP_NAME = 'ttc'
  }
  parameters {
    string(name: 'TAG_NAME', defaultValue: '', description: '')
  }
}

你可能感兴趣的:(jenkins,k8s,运维,groovy)