需要进行部署

时间:2018-11-27 12:07:20

标签: jenkins jenkins-pipeline devops jenkins-cli

嗨,我正尝试仅在将代码推送到暂存时才部署到暂存,仅在将代码推送到主存时才部署到主存。请帮助我完成这一点,这是groovy脚本。

node{
  currentBuild.result = "SUCCESS"

  try {
    stage('Pull-msater') {
      // pulling master from the repo
      git 'https://github.com/raj1rana/mongoDB-docker.git'
    }

    stage('pull-staging'){
      //pulling staging from the repo
      git branch: 'staging', url: 'https://github.com/raj1rana/mongoDB-docker.git'
    }

    stage('deploy-staging') {
      //deploy to staging server
      sh 'rsync -avz  -e ssh --exclude .git /var/lib/jenkins/workspace/pipeline-test/  ubuntu@13.232.107.33:/home/ubuntu/Docker-files/'
    }

    stage('deploy-production'){
      //deploy to production server
      sh 'rsync -avz -e ssh  --exclude .git  /var/lib/jenkins/workspace/pipeline-test/  ubuntu@13.232.107.33:/home/ubuntu/master'
    }

    stage('mail fail/sucess'){
      mail body: 'project build successful',
      from: 'xxxx@yyyyy.com',
      replyTo: 'xxxx@yyyy.com',
      subject: 'project build successful',
      to: 'yyyyy@yyyy.com'
    }
  }
  catch (err) {
    currentBuild.result = "FAILURE"

    mail body: "project build error is here: ${env.BUILD_URL}" ,
    from: 'xxxx@yyyy.com',
    replyTo: 'yyyy@yyyy.com',
    subject: 'project build failed',
    to: 'zzzz@yyyyy.com'

    throw err
  }
}

2 个答案:

答案 0 :(得分:0)

您可以使用env.BRANCH_NAME来实现它。

if(env.BRANCH_NAME == 'master') {
  // some build steps
} else  {
  // some build steps
}

答案 1 :(得分:0)

在您的管道中,您在同一目录(即工作区目录)中进行了两次(主和暂存)git checkout。因此最终结果将是登台目录的签出。

您可以通过创建两个作业来简化构建过程,一个作业用于生产,一个作业用于暂存,不要将两个作业混在一起。

下面的示例用于分期:

node{
  currentBuild.result = "SUCCESS"

  try {

    stage('pull-staging'){
      //pulling staging from the repo
      git branch: 'staging', url: 'https://github.com/raj1rana/mongoDB-docker.git'
    }

    stage('deploy-staging') {
      //deploy to staging server
      sh 'rsync -avz  -e ssh --exclude .git /var/lib/jenkins/workspace/pipeline-test/  ubuntu@13.232.107.33:/home/ubuntu/Docker-files/'
    }

    stage('mail fail/sucess'){
      mail body: 'project build successful',
      from: 'xxxx@yyyyy.com',
      replyTo: 'xxxx@yyyy.com',
      subject: 'project build successful',
      to: 'yyyyy@yyyy.com'
    }
  }
  catch (err) {
    currentBuild.result = "FAILURE"

    mail body: "project build error is here: ${env.BUILD_URL}" ,
    from: 'xxxx@yyyy.com',
    replyTo: 'yyyy@yyyy.com',
    subject: 'project build failed',
    to: 'zzzz@yyyyy.com'

    throw err
  }
}

下面的示例用于生产:

node{
  currentBuild.result = "SUCCESS"

  try {
    stage('Pull-msater') {
      // pulling master from the repo
      git 'https://github.com/raj1rana/mongoDB-docker.git'
    }

    stage('deploy-production'){
      //deploy to production server
      sh 'rsync -avz -e ssh  --exclude .git  /var/lib/jenkins/workspace/pipeline-test/  ubuntu@13.232.107.33:/home/ubuntu/master'
    }

    stage('mail fail/sucess'){
      mail body: 'project build successful',
      from: 'xxxx@yyyyy.com',
      replyTo: 'xxxx@yyyy.com',
      subject: 'project build successful',
      to: 'yyyyy@yyyy.com'
    }
  }
  catch (err) {
    currentBuild.result = "FAILURE"

    mail body: "project build error is here: ${env.BUILD_URL}" ,
    from: 'xxxx@yyyy.com',
    replyTo: 'yyyy@yyyy.com',
    subject: 'project build failed',
    to: 'zzzz@yyyyy.com'

    throw err
  }
}