具有Nightly部署的主分支的声明性Jenkins管道

问题描述 投票:6回答:1

我想将一些工作转换为新的Jenkins 2.0声明性管道。目前他们有3个不同的工作:

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

为此,我在春天有一个带有maven的小项目,这将是我开始的最好的例子(简单,容易和快速建立)。

目前我已经有了一个用于CI构建的Multibranch管道,但我希望将这个工作整合到Nightly和Sprintly构建中。

  • 每晚:在夜间通过主分支运行Cron作业以部署在夜间服务器中。
  • 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"'
        }
    }
git jenkins nightly-build
1个回答
4
投票

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

  1. 将在每个分支中运行maven构建的多分支管道(可以配置为每n分钟轮询scm或由存储库中的webhook启动。
  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"'
    }
}

如果您有任何问题,请告诉我,我将很乐意为您提供帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.