kubesphere通过devops部署项目

2022-09-21 16:29:00
admin
原创
22
摘要:kubesphere通过devops部署项目

一、kubesphere通过devops部署前端

  新建前端项目saber

  新建devops的凭证

pipeline {
  agent {
    node {
      label 'nodejs'
    }

  }
  stages {
    stage('拉取代码') {
      agent none
      steps {
        container('nodejs') {
          checkout(scm: [$class: 'SubversionSCM', locations: [[cancelProcessOnExternalsFail: true,  credentialsId: 'svn', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'https://10.18.1.2:9011/svn/HiEdu1.0/trunk/src/edu/saber']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']], poll: false)
          sh 'ls'
          sh 'pwd'
        }

      }
    }
 stage('项目编译') {
      agent none
      steps {
        container('nodejs') {
          sh '''
            mkdir node_modules
            tar -xzvf node-sass.tar.gz
            mv node-sass node_modules/
            npm config set registry http://10.18.224.74:8081/repository/npm-fe-hosted/
            npm install
            npm run build:dev
            pwd
            ls
            '''
        }

      }
    }
    stage('构建镜像') {
      agent none
      steps {
        container('nodejs') {
          sh 'docker build -f Dockerfile  -t blade-saber:SNAPSHOT-$BUILD_NUMBER  .'
        }

      }
    }

    stage('推送镜像') {
      agent none
      steps {
        container('nodejs') {
          withCredentials([usernamePassword(credentialsId : 'docker-local' ,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 blade-saber:SNAPSHOT-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/blade-saber:SNAPSHOT-$BUILD_NUMBER'
            sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/blade-saber:SNAPSHOT-$BUILD_NUMBER'
          }

        }

      }
    }

    stage('deploy to dev') {
      agent none
      steps {
        container('nodejs') {
          withCredentials([kubeconfigContent(credentialsId : 'k8s-config' ,variable : 'KUBECONFIG_CONTENT' ,)]) {
            sh '''mkdir ~/.kube
echo "$KUBECONFIG_CONTENT" > ~/.kube/config
envsubst < deploy/blade-saber.yaml | kubectl apply -f -'''
          }

        }

      }
    }

  }
  environment {
    DOCKER_CREDENTIAL_ID = 'dockerhub-id'
    GITHUB_CREDENTIAL_ID = 'github-id'
    KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
    REGISTRY = 'registry.kubeoperator.io:8083'
    DOCKERHUB_NAMESPACE = 'hiedu'
    GITHUB_ACCOUNT = 'kubesphere'
    APP_NAME = 'devops-java-sample'
  }
  options {
    timeout(time: 1, unit: 'HOURS')
  }
}


二、kubesphere通过devops部署后端

  新建前端项目blade-system

  新建devops的凭证


pipeline {
  agent {
    node {
      label 'maven'
    }

  }
  stages {
    stage('代码下载') {
      agent none
      steps {
        container('maven') {
          checkout(scm: [$class: 'SubversionSCM', locations: [[cancelProcessOnExternalsFail: true,  credentialsId: 'svn', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'https://10.18.1.2:9011/svn/HiEdu1.0/trunk/src/edu/bladex/bladex']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']], poll: false)
        }

      }
    }

    stage('代码编译') {
      agent none
      steps {
        container('maven') {
          sh '''mvn  install -DskipTests
           '''
        }

      }
    }

    stage('构建镜像') {
      agent none
      steps {
        container('maven') {
          sh 'cd blade-service/blade-system && docker build -f Dockerfile  -t blade-system:SNAPSHOT-$BUILD_NUMBER  .  --build-arg profiles=$profiles'
        }

      }
    }

    stage('推送镜像') {
      agent none
      steps {
        container('maven') {
          withCredentials([usernamePassword(credentialsId : 'docker-local' ,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 blade-system:SNAPSHOT-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/blade-system:SNAPSHOT-$BUILD_NUMBER'
            sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/blade-system:SNAPSHOT-$BUILD_NUMBER'
          }

        }

      }
    }

    stage('deploy to dev') {
      agent none
      steps {
        container('maven') {
          withCredentials([kubeconfigContent(credentialsId : 'k8s-config' ,variable : 'KUBECONFIG_CONTENT' ,)]) {
            sh '''mkdir ~/.kube
echo "$KUBECONFIG_CONTENT" > ~/.kube/config
envsubst < blade-service/blade-system/deploy/blade-system.yaml | kubectl apply -f -
'''
          }

        }

      }
    }

  }
  environment {
    DOCKER_CREDENTIAL_ID = 'dockerhub-id'
    GITHUB_CREDENTIAL_ID = 'github-id'
    KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
    REGISTRY = 'registry.kubeoperator.io:8083'
    DOCKERHUB_NAMESPACE = 'hiedu'
    GITHUB_ACCOUNT = 'kubesphere'
    APP_NAME = 'devops-java-sample'
  }
  options {
    timeout(time: 1, unit: 'HOURS')
  }
  parameters {
    string(name: 'profiles', defaultValue: 'dev', description: '环境配置')
  }
}

三、新建凭证步骤

    发表评论
    评论通过审核之后才会显示。
    文章分类
    联系方式
    联系人: 郑州-小万
    电话: 13803993919
    Email: 1027060531@qq.com
    QQ: 1027060531
    网址: www.wanhejia.com