Jenkins部署管道

时间:2019-03-13 14:22:46

标签: docker jenkins kubernetes jenkins-pipeline

我在kubernetes集群上有Jenkins master。 我创建了新的Kubernetes集群和生产集群。

我如何促进部署到Stage Server和Production Server。

def label = "worker-${UUID.randomUUID().toString()}"

podTemplate(label: label, containers: [
  containerTemplate(name: 'docker', image: 'docker', command: 'cat', ttyEnabled: true),
  containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl:v1.8.8', command: 'cat', ttyEnabled: true)
],
volumes: [
  hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')
]) {
  node(label) {
    def myRepo = checkout scm
    def gitCommit = myRepo.GIT_COMMIT
    def gitBranch = myRepo.GIT_BRANCH
    def shortGitCommit = "${gitCommit[0..10]}"
    def previousGitCommit = sh(script: "git rev-parse ${gitCommit}~", returnStdout: true) 
    def bmfFrontend = "bmf-frontend"
    def bmfBackend = "bmf-backend"
    def tag = sh(script: "git rev-parse --short --verify HEAD")

    stage('Code Checkout and Setup') {
      try {
        container('docker') {
          sh """
            pwd
            echo "GIT_BRANCH=${gitBranch}" >> /etc/environment
            echo "GIT_COMMIT=${gitCommit}" >> /etc/environment
            echo 'Start building Frontend and Backend Docker images'
            """
        }
      }
      catch (exc) {
        println "Failed to test - ${currentBuild.fullDisplayName}"
        throw(exc)
      }
    }
    stage('Build') {
      container('docker') {
        echo 'Building Bmf Frontend Image'

      }
    }

    stage('Build BMF Frontend') {
      container('docker') {
        withCredentials([[$class: 'UsernamePasswordMultiBinding',
          credentialsId: 'docker-registry',
          usernameVariable: 'DOCKER_USER',
          passwordVariable: 'DOCKER_PASSWORD']]) {
          sh """
            docker login registry.gitlab.com -u ${DOCKER_USER} -p ${DOCKER_PASSWORD}
            cd server
            docker build --no-cache -t registry/bmf-backend:${BUILD_NUMBER} .
            docker tag registry/bmf-backend:${BUILD_NUMBER} registry/bmf-backend:latest
            docker push registry./bmf-backend:${BUILD_NUMBER}
            docker push registry/bmf-backend:latest
            """
        }
      }
    }
    stage('Cleanup') {
      container('docker') {
        echo 'Deleting the images to cleanup server space'
        sh """
          docker rmi --force registry/bmf-web/bmf-backend:${BUILD_NUMBER}
          docker rmi --force registry/bmf-web/bmf-backend:latest           
           """
      }
    }
    stage('Deploy to Stage Server') {
      container('kubectl') {
        sh "kubectl appy -f runtime/kubernetes"
      }
    }
   stage('Deploy to Production Server Optionaly') {
      container('kubectl') {
        sh "kubectl apply -f runtime/kubernetes"
  }
}

如何告诉jenkins部署在Stage服务器和Production Server上?

有没有与此相关的真实示例?

我是新手,所以我不知道其他方式,我正在寻找JenkinsX,但我需要这种方式。

0 个答案:

没有答案