Environment Variables

You can set environment variables in task definitions by specifying the env key. Tasks can also set environment variables as outputs which will take effect when including the task in a use definition of another task.

Setting Environment Variables

tasks:
  - key: example-with-env
    run: echo hello $NAME
    env:
      NAME: world

Affecting the Cache Key

By default, Mint considers the values of environment variables available to your task when determining if the task is a cache hit. This means that if you have an environment variable which is a different value every time a task executes, your task will never be a cache hit.

In some situations, you may want to exclude an environment variable from the cache key to enable a cache hit. An example of one such situation is ephemeral credentials; with this setting, commands that are deterministic (aside from the credentials) can be made to permit cache hits. You can do this by setting the cache-key field to excluded on the environment variable which is ephemeral.

tasks:
  - key: aws-example
    run: |
      # something with the aws cli
      aws ...
    env:
      AWS_ACCESS_KEY_ID:
        value: ${{ secrets.AWS_ACCESS_KEY_ID }}
        cache-key: excluded
      AWS_SECRET_ACCESS_KEY:
        value: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        cache-key: excluded

Environment Variable Outputs

Tasks can also output environment variables by writing a file to the $MINT_ENV directory. The file name will be treated as the environment variable name, and its value will be the environment variable value. Environment variables written to that file will automatically be set for other tasks with a use dependency.

tasks:
  - key: set-env-one
    run: echo hello > $MINT_ENV/ONE
  - key: set-env-two
    run: echo world >> $MINT_ENV/TWO
  - key: print-env
    use: [set-env-one, set-env-two]
    run: |
      echo $ONE $TWO
      env | grep -E 'ONE|TWO'

The print-env task will output:

hello world
ONE=hello
TWO=world

Merging

By default, if multiple tasks set an environment variable, the task is specified last in the use declaration will overwrite previous values. However, you may want to combine the values. A common use case for this is having multiple tasks which will append to the $PATH environment variable. Mint automatically merges $PATH by default, so this will work:

tasks:
  - key: path-one
    run: |
      mkdir one
      echo "echo script one" > one/one.sh
      chmod +x one/one.sh
      echo "PATH=$PWD/one" >> $MINT_ENV
  - key: path-two
    run: |
      mkdir two
      echo "echo script two" > two/two.sh
      chmod +x two/two.sh
      echo "PATH=$PWD/two" >> $MINT_ENV
  - key: use-one-and-two
    use: [path-one, path-two]
    run: |
      echo $PATH
      one.sh
      two.sh

The use-one-and-two task will output:

/var/mint-workspace/two:/var/mint-workspace/one:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
script one
script two

It's rare to need to combine environment variable updates like this, but if you do, you can specify an env-config on your task definition which will determine how environment variables set by use dependencies are applied. The default behavior for $PATH is actually the following env-config.

- key: use-one-and-two
  use: [path-one, path-two]
  run: |
    echo $PATH
    one.sh
    two.sh
  env-config:
    merge:
      PATH:
        strategy: join
        by: ':'