Dynamic Tasks

Mint supports generating tasks dynamically. This is helpful if you want to use repository contents to determine which tasks to run, such as running one task per package in a monorepo project. Mint also does not currently support matrix or parallel functionality in task definitions, so you'll need to use dynamic tasks if you want configurable parallelism without duplicating task definitions.

Mechanics

To generate dynamic tasks, write *.yaml files to the $MINT_DYNAMIC_TASKS directory. The yaml file needs to contain an array of task definitions. The file names are arbitrary and are not used for anything. You can generate more than one file in the directory if you'd like; there isn't a practical difference between generating one file with many tasks in the array versus generating multiple files.

Basic Example

Here's an example using a bash script to generate a dynamic task. Using tee isn't necessary – we're only using it here so that the logs from generate-dynamic-task will display the contents of the file that got generated. We could have written this as cat << EOF > $MINT_DYNAMIC_TASKS/tasks.yaml instead.

tasks:
  - key: generate-dynamic-task
    run: |
      cat << EOF | tee $MINT_DYNAMIC_TASKS/tasks.yaml
        - key: dynamic-task
          run: echo this is a dynamically generated task
      EOF

Multiple Tasks

These two examples are effectively equivalent.

Multiple tasks in the array:

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

Multiple dynamic task files:

tasks:
  - key: generate-dynamic-task
    run: |
      cat << EOF | tee $MINT_DYNAMIC_TASKS/task-1.yaml
        - key: dynamic-task-1
          run: echo this is the first dynamic task
      EOF
      cat << EOF | tee $MINT_DYNAMIC_TASKS/task-2.yaml
        - key: dynamic-task-2
          run: echo this is the second dynamic task
      EOF

Ruby Example

In practice, you'll most likely generate dyanmic tasks using a scripting language rather than bash. Here's an example that uses Ruby to generate dynamic tasks.

tasks:
  - key: ruby
    call: mint/install-ruby 1.0.9
    with:
      ruby-version: 3.2.2
  - 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.