需要部署到staging

Need to deploy to staging

您好,我正在尝试仅在代码被推送到 staging 时部署到 staging,仅当代码被推送到 master 时才尝试部署到 master 请帮助我完成此操作,这是 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
  }
}

你利用env.BRANCH_NAME来实现它。

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

在您的管道中,您正在执行两次(主和暂存)git 在同一目录(即工作区目录)中检出。所以最终结果将是登台目录的检出。

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

以下示例用于分期:

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
  }
}