2017-08-04 83 views
3

我想将一些工作转换为新的Jenkins 2.0声明式管道。目前他们是3个不同的工作:声明式Jenkins管道与每晚部署主分支

  1. CI-> PollSCM每5分钟(只有主人),建立和运行测试。
  2. 勉强 - >每晚运行(在夜间服务器上进行构建,测试,集成测试和部署)
  3. Sprintly - >这是一个参数化作业,每周四使用手动创建标签运行。 (构建,测试,集成测试和部署sprintly服务器上)

为此,我在春天与Maven一个小项目,这将是对我来说,开始最好的例子(简单,方便,快捷是建)。

目前我已经有一个CI构建的Multibranch管道,但是我想在Nightly和Sprintly构建中集成到这个工作中。

  • 夜间:夜间在主分支上运行一个Cron作业,在Nightly Server中部署为 。
  • Sprintly:通过Sprintly_tag生成,由我在主分支中生成,并部署到Sprintly Server中。

目前,我有这个JenkinsFile

pipeline { 
agent { 
    label 'master' 
} 
tools { 
    maven "Apache Maven 3.3.9" 
    jdk "Java JDK 1.8 U102" 
} 
triggers { 
     cron ('H(06-08) 01 * * *') 
     pollSCM('H/5 * * * *') 
} 
stages { 
    stage('Build') { 
     steps { 
      sh 'mvn -f de.foo.project.client/ clean package' 
     } 
     post { 
      always { 
       junit allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml' 
       archiveArtifacts allowEmptyArchive: true, artifacts: '**/target/*.war' 
      } 
     } 
    } 
    stage('Deploy') { 
       sh 'echo "Deploy only master"' 
    } 
} 

它运行每个分支当东西拉Git和也跑在晚上大约1点钟(但仍在运行的所有分支机构)。

任何想法或暗示要做到这一点?不需要执行部署的代码我只想知道如何过滤/拆分处于同一JenkinsFile中的分支

非常感谢大家!

编辑: 我也可以使用,但它会在夜间运行,所有的树枝(我只能为cron作业使这个过滤器?)

 stage('DeployBranch') { 
     when { branch 'story/RTS-525-task/RTS-962'} 
     steps { 
      sh 'echo "helloDeploy in the branch"' 
     } 
    } 
    stage('DeployMaster') { 
     when { branch 'master'} 
     steps { 
      sh 'echo "helloDeploy in the master"' 
     } 
    } 

回答

1

四个月后读我自己的问题后,我意识到我在尝试设置触发器和作业方面完全错了。 我们应该有3项不同的工作:

  1. 多枝管道将运行Maven构建的每一个分支(可配置轮询SCM每n分钟或通过在资源库中一个网络挂接启动。
  2. 将被配置为在夜晚(在作业配置中,不在管道中)触发的流水线(夜间呼叫),并将部署到夜间系统并仅使用主分支(也在Jenkins作业中配置)
  3. 管道(称为sprintly),但参数有一定的标签来运行和使用,只有主分支

管道应保持在简单的像:

pipeline { 
agent { 
    label 'master' 
} 
tools { 
    maven "Apache Maven 3.3.9" 
    jdk "Java JDK 1.8 U102" 
} 
stages { 
    stage('Build') { 
     steps { 
      sh 'mvn -f de.foo.project.client/ clean package' 
     } 
     post { 
      always { 
       junit allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml' 
       archiveArtifacts allowEmptyArchive: true, artifacts: '**/target/*.war' 
      } 
     } 
    } 
    stage('Deploy') { 
      when (env.JOB_NAME.endsWith('nightly') 
       sh 'echo "Deploy on nighlty"' 
      when (env.JOB_NAME.endsWith('sprintly') 
       sh 'echo "Deploy only sprintly"' 
    } 
} 

如果您有任何问题,让我知道我会很乐意帮助:)