Adaptive CI Pipelines with Dynamic Tasks

Adaptive CI Pipelines with Dynamic Tasks

Published on 

by Dan Manges

In most CI platforms, tasks needs to be defined statically when the run starts. This limits the flexibility in workflows, especially as projects become more advanced and yaml configuration gets more complicated. It's especially unfortunate to have this limitation in a dev tool built for engineers.

Engineers are adept at writing code to solve problems. That's why when we built Mint, we knew we wanted to support dynamic tasks. We wanted to give engineers the ability to define tasks at runtime, utilizing any programming language and logic that they wanted.

Dynamic tasks enable building CI pipelines which are adaptive based on things like the commit contents or data from external data sources. They unlock a lot of possibilities for building highly performant CI pipelines.

Task Data Structures

YAML gets a lot of criticism in its use for configuring infrastructure. It's usually not YAML itself that's the issue though. YAML is just a format for serializing data. The pain points around YAML configuration are usually more centered around having to git push to run workflows, not being able to debug, and implementing complex logic in embedded expressions rather than just writing code.

To generate dynamic tasks in Mint, you can use any programming language to build an array of task definitions, and then serialize it as YAML.

Here's a simple example using bash:

1
2
3
4
5
6
7
8
9
tasks:
  - key: generate-dynamic-task
    run: |
      cat << EOF | tee $MINT_DYNAMIC_TASKS/tasks.yaml
        - key: dynamic-task-1
          run: echo this is the first dynamic task
        - key: dynamic-task-2
          run: echo this is the second dynamic task
      EOF

You probably don't want to use bash to generate dynamic tasks, although we've used it in some of our own workflows by leveraging envsubst with template files.

Here's a more practical example using Ruby;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
tasks:
  - key: ruby
    call: mint/install-ruby 1.0.9
    with:
      ruby-version: 3.3.1

  - key: generate-dynamic-tasks
    use: [ruby]
    run: |
      ruby -e '
        require "yaml"
        tasks = [
          {
            "key" => "dynamic-task",
            "run" => "echo this task was generated from ruby"
          }
        ]
        puts YAML.dump(tasks)
      ' | tee $MINT_DYNAMIC_TASKS/tasks.yaml

We showed implementing this Ruby script inline with the task definition, but it's more common to check the script in as a separate file, such as .mint/generate-tasks.rb and then call ruby .mint/generate-tasks.rb > $MINT_DYNAMIC_TASKS/tasks.yaml in your Mint task definition.

For more information, see the documentation on generating dynamic tasks in Mint.

Demo

Here's a 3 minute video on how easy it is to generate dynamic tasks in Mint.

Follow Along

We write a lot about software engineering best practices, CI/CD pipelines, and Mint. Follow along on X at @rwx_research, LinkedIn, or our email newsletter

Enjoyed this post? Share it!

Never miss an update

Get the latest releases and news about RWX and our ecosystem with our newsletter.