美文网首页Jenkins社区JenkinsDevOPS
06 使用Workflows工作流调度工作

06 使用Workflows工作流调度工作

作者: georgesre | 来源:发表于2019-05-21 18:40 被阅读1次
    example workflow

    我们通过使用工作流以提高软件开发的速度,使其具有更快的反馈,更短的重做,并更有效的利用资源。本文档描述了工作流功能并提供了示例配置:

    概览Overview (TL;DR)

    以下是摘抄一些CircleCI工作流的定义,我们也尽量让Jenkinsfile能实现相同的工作流。

    A workflow is a set of rules for defining a collection of jobs and their run order. Workflows support complex job orchestration using a simple set of configuration keys to help you resolve failures sooner.
    With workflows, you can:

    • Run and troubleshoot jobs independently with real-time status feedback.
    • Schedule workflows for jobs that should only run periodically.
    • Fan-out to run multiple jobs in parallel for efficient version testing.
    • Fan-in to quickly deploy to multiple platforms.

    For example, if only one job in a workflow fails, you will know it is failing in real-time. Instead of wasting time waiting for the entire build to fail and rerunning the entire job set, you can rerun just the failed job.

    States

    Workflows may appear with one of the following states:

    • RUNNING: Workflow is in progress
    • NOT RUN: Workflow was never started
    • CANCELLED: Workflow was cancelled before it finished
    • FAILING: A Job in the workflow has failed
    • FAILED: One or more jobs in the workflow failed
    • SUCCESS: All jobs in the workflow completed successfully
    • ON HOLD: A job in the workflow is waiting for approval
    • NEEDS SETUP: A workflow stanza is not included or is incorrect in the Jenkinsfile file for this project

    工作流配置示例

    请移步Sample Jenkinsfile config 查看更多的配置示例

    Sequential Job Execution Example

    The following example shows a workflow with four sequential jobs. The jobs run according to configured requirements, each job waiting to start until the required job finishes successfully as illustrated in the diagram.


    Sequential Job Execution Example

    The dependencies are defined by setting the requires: key as shown. The deploy: job will not run until the build and test1 and test2 jobs complete successfully. A job must wait until all upstream jobs in the dependency graph have run. So, the deploy job waits for the test2 job, the test2 job waits for the test1 job and the test1 job waits for the build job.

    Sequential Job Execution Example

    See the Sample Sequential Jenkinsfile config for a full example.

    Fan-Out/Fan-In Workflow Example

    The illustrated example workflow runs a common build job, then fans-out to run a set of acceptance test jobs in parallel, and finally fans-in to run a common deploy job.


    Fan-Out/Fan-In Workflow Example

    The following Jenkinsfile snippet is an example of a workflow configured for fan-out/fan-in job execution:

    In this example, as soon as the THE FIRST STAGE job finishes successfully, all four acceptance test jobs start. The THE FINAL STAGE job must wait for all four acceptance test jobs to complete successfully before it starts.

    Fan-Out/Fan-In Workflow Example

    See the [Sample Fan-in/Fan-out Workflow config](https://github.com/george-sre/jenkins-test/blob/fan-in-fan-out/Jenkinsfile for a full example.

    Using "When" to control your Job Execution

    The following sections provide example for using when to manage job execution.

    Using Job build-in Environment Variables

    http://jenkins.io/doc/pipeline/tour/environment/

    Branch-Level Job Execution

    The following example shows a workflow configured with jobs on three branches: Dev, Stage, and Pre-Prod. Workflows will ignore branches keys nested under jobs configuration, so if you use job-level branching and later add workflows, you must remove the branching at the job level and instead declare it in the workflows section of your Jenkinsfile, as follows:

    Branch-Level Job Execution
    • The following Jenkinsfile snippet is an example of a workflow configured for branch-level job execution:
          stage("WHEN branch: Dev") {
                when { branch 'Dev' }
                steps {
                    echo 'something happened if this build is on Dev Branch'
                }
            }
    
    • For a full example of workflows, see the configuration file for the when controls Workflow With Branching project.
    when usages

    Scheduling a Workflow

    It's highly suggested that to limit the 'triggers' into the specific branch, we don't want the build storm if we apply the 'triggers' on multiple branches/PR/Tags. Like:
    triggers { cron(env.BRANCH_NAME == 'try-schedule-workflow' ? '1 2 */1 * *' : '') }

    It can be inefficient and expensive to run a workflow for every commit for every branch. Instead, you can schedule a workflow to run at a certain time for specific branches.

    Consider running workflows that are resource-intensive or that generate reports on a schedule rather than on every commit by adding a triggers key to the configuration.

    Specifying a Valid Schedule

    For more details, see the triggers section of the Pipeline Syntax document.

    For a full configuration example, see the Sample Scheduled Workflows configuration.

    Check the Schedule Configuration

    image.png image.png

    参考

    更多

    云平台开发运维解决方案@george.sre

    个人主页:https://geekgoogle.com

    GitHub: https://github.com/george-sre

    Mail: george.sre@hotmail.com

    简书: georgesre - 简书

    欢迎交流~

    相关文章

      网友评论

        本文标题:06 使用Workflows工作流调度工作

        本文链接:https://www.haomeiwen.com/subject/pfbjzqtx.html