Jenkins 2管道-如何为连续输送管道建模

Jenkins 2管道-如何为连续输送管道建模,jenkins,jenkins-plugins,jenkins-pipeline,continuous-delivery,jenkins-2,Jenkins,Jenkins Plugins,Jenkins Pipeline,Continuous Delivery,Jenkins 2,我对詹金斯2号管道完全陌生 我曾与Jenkins 1合作过,并持有以下观点: 您可以直接启动某个阶段,比如说,我可以选择从发布阶段开始运行,跳过测试 我有一个非常简单的Jenkins 2管道定义: stage('Preparation'){ echo """ Preparing something """ } stage('Greeting') { parallel 'hello1':{ node{

我对詹金斯2号管道完全陌生

我曾与Jenkins 1合作过,并持有以下观点:

您可以直接启动某个阶段,比如说,我可以选择从发布阶段开始运行,跳过测试

我有一个非常简单的Jenkins 2管道定义:

stage('Preparation'){
    echo """
         Preparing
         something
         """
}
stage('Greeting') { 
    parallel 'hello1':{
        node{
            echo 'hello world 1'
        }
    }, 'hello2':{
        node{
            echo 'hello world 2'
        }
    }
}
在管道页面中,我有一个“Build now”,它运行从准备开始的所有阶段

我的问题是:

  • 我该如何运行我喜欢的舞台?例如,问候而不是从准备开始
  • 如何定义阶段之间的依赖关系?我的意思是在另一个舞台完成后才开始的
  • 有没有办法限制某个用户可以开始的阶段?想象一下,我只希望一个特定的用户启动问候阶段
  • 如何设置手动阶段
  • 更新:我的问题背后的真正目标是建立一个连续输送管道的模型,就像下面的Jenkins 2管道一样:

    Build stage --> AUTO --> Acceptance Stage --> MANUAL --> Production Stage
                                              --> MANUAL --> QA Stage
    
    这就是我想要的行为:

    构建阶段(任何用户都可以启动)完成后,它会自动触发验收阶段
    。这个不能手动启动,只能在成功完成构建阶段后自动启动

    验收阶段我需要只有授权用户才能手动触发QA阶段生产阶段

    业务流程是:开发人员进入构建阶段,其代码被构建和打包。验收阶段开始,使用打包的代码运行一系列自动化测试

    此时,验收阶段完成OK后,可能会发生两件事:

  • 也许QA阶段需要运行更多的测试(Cumber、manual等)。某些授权用户将在此阶段启动
  • 当产品负责人高兴时,他可以决定启动生产阶段,以便在生产环境中部署代码

  • 我正在努力用Jenkins 2管道对此进行建模。

    您的一些问题没有直接的答案,但可以通过一些额外的编码来实现。虽然某些人可能会找到其他方法来实现,但让我用我的想法来尝试:

    1) How can I run the stage I prefer? For instance Greeting instead of starting from Preparation?
    
    这可以通过将布尔参数FASTFORWARD\u添加到\u来实现,而不是使用在执行构建时提供的值来操纵构建流。因此,您的代码现在看起来像:

    if (FASTFORWARD_TO_GREETING == 'false') {
    stage('Preparation'){
        echo """
             Preparing
             something
             """
    }
    }
    
    stage('Greeting') { 
        parallel 'hello1':{
            node{
                echo 'hello world 1'
            }
        }, 'hello2':{
            node{
                echo 'hello world 2'
            }
        }
    }
    
    
    2) How do you define the dependencies between stages? I mean the stage called after another one completes
    
    阶段是串行执行的,因此如果首先定义了一个阶段,那么它将首先启动并完成,然后再移动到下一个阶段。然而,在并行步骤中,这并不成立,因为所有步骤都将并行执行。因此,在您的示例代码中,您定义的依赖关系是首先执行阶段“准备”,而不仅仅是并行执行“hello1”和“hello2”步骤。但是,无法保证将打印哪一个“hello world1”或“hello world 2”

    3) Is there a way to limit the stages that a certain user can start? Imagine that I only want a specific user to launch the Greeting stage.
    
    您可以在某个阶段之前执行手动批准步骤。例如,在您的代码中,您希望执行阶段准备,而不是在执行阶段问候语之前手动批准,您的代码将如下所示:

    stage('Preparation'){
        echo """
             Preparing
             something
             """
    }
    stage concurrency: 1, name: 'approve-greeting'
    input id: 'greeting-deploy', message: 'Proceed to Greeting?', ok: 'Deploy'
    
    stage('Greeting') { 
        parallel 'hello1':{
            node{
                echo 'hello world 1'
            }
        }, 'hello2':{
            node{
                echo 'hello world 2'
            }
        }
    }
    
    之后将发生的是,当您执行构建时,阶段准备将被执行,但在此之后,作业将等待手动批准继续进行。在Jenkins管道视图中,该阶段将被称为“approve greeting”,它将等待有人通过在视图中单击它来批准构建

    4) How do you setup manual stages?
    
    我相信答案3回答了这个问题

    如果您需要进一步的信息/解释,请告诉我

    编辑::请在下面找到进一步的答案:

    构建阶段(任何用户都可以启动它)在完成时触发 自动进入验收阶段

    显然,在Jenkins管道中,建造阶段和验收阶段都将被定义为正常阶段。因此,您的代码很简单,如:

    node {
        //define any variable here
        // Get source code from repo using checkout to directory say stackoverflow
        // Get source code from repo for acceptance test using checkout to directory say stackoverflow-test
        //Define any tool like Maven etc. location if required.
        dir('stackoverflow') {
          stage name: 'build'
            //Do required steps
        }
        dir('stackoverflow-test') {
          stage name: 'Acceptance'
            //Do required steps here 
        }
    
    在这一点上,当验收阶段已经完成OK时,两件事情可以 发生:

  • 也许QA阶段需要运行更多的测试(Cumber、manual等)。 某些授权用户将在此阶段启动

  • 当产品所有者满意时,他可以决定启动生产阶段,以便在生产环境中部署代码

  • 这可以通过使用输入选项来实现,因此在完成上述代码后,您现在可以编写:

        stage 'promotion'
          def userInput = input(
          id: 'userInput', message: 'Let\'s promote?', parameters: [
          [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Production', name: 'prod'],
          [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'ManualQA', name: 'qa']
          ])
          echo ("Env: "+userInput['prod'])
          echo ("Target: "+userInput['qa'])
    
    然后可以从上面获取值并再次操纵流。比如:

    如果prod的值为真,则进入生产阶段, 如果qa值为真,则进入qa手册阶段,就像我上面的快速前进问候语示例代码一样

    编辑2

    进一步回答评论部分的问题:

    1) 如何或在何处指定参数,如快进\u到\u问候语

    快速转发\u至\u问候语等参数将定义为作业级别

    2) 在促销阶段,您必须在ManualQA和 生产。如果用户选择ManualQA,则会跳过该阶段 生产。之后,如果用户愿意,我希望他得到提示 提升到生产阶段。如果你能提供一个完整的定义 管道会很好的

    在MaualQA阶段之后,您可以使用另一个输入步骤来处理这个问题,但这次只使用一个参数。因此,在阶段升级之后,将有阶段手册QA,然后是以下输入步骤:

    def userInput1 = input(
     id: 'userInput', message: 'Let\'s promote?', parameters: [
     [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Production', name: 'prod']
    ])
    
    3) 如何确定用户是否有权运行阶段或阶段 不理想情况下,我会根据角色来做

    我不确定如何处理角色,但我相信任何具有管理员权限或运行该作业的人都有权运行/批准该阶段,但我不能100%确定是否可以
    node{
        def user
        def userInput
        def mvnHome = tool 'M3'
    
        wrap([$class: 'BuildUser']) {
            user = env.BUILD_USER_ID
        }
    
        stage('Commit Stage'){
            echo 'Downloading from Git...'
            git 'https://github.com/codependent/spring-nio-rest.git'
            'Building project...'
            sh "${mvnHome}/bin/mvn clean install -DskipTests"
        }
    
        stage('Acceptance Stage') { 
            echo """
                 Getting image from Nexus...OK
                 Deploying image...OK
                 Executing tests...OK
                 """
            userInput = input(id: 'userInput', message: 'Select the next stage:', parameters: [
                [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Run QA tests', name: 'QA'],
                [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Run performance tests', name: 'performance']
            ])
        }
    
        if(userInput['QA']){
            stage('QA Stage') { 
             echo """
                 Getting image from Nexus...OK
                 Deploying image...OK
                 Executing QA tests...OK
                 """
            }
        }
    
        if(userInput['performance']){
            stage('Performance Stage') { 
             echo """
                 Getting image from Nexus...OK
                 Deploying image...OK
                 Executing Performance tests...OK
                 """
            }
        }
    
        stage('Production Stage') { 
            input message: 'Are you sure you want to deploy to Production?', submitter: 'codependent'
            echo 'Deploying to Production...OK'
        }
    }