Shell Commands

Using Shell Commands in a flow file allows you to access your shell from a Liquibase operation. You can join multiple commands in sequence, or redirect output to files for later processing which can be done via cat, grep, or other methods.

Note: The flow feature requires Liquibase 4.17.0+.

Syntax

To use shell commands in your Flow file, you must set the type argument to shell rather than liquibase:

- type: shell
  command: foobar

You can use a single command here or chain multiple commands together with &&. For example, to print the environment variables that begin with 'LIQUIBASE_':

echo 'Printing LIQUIBASE_* env vars' && env | grep 'LIQUIBASE_'

Alternatively, you can put bash -c in front of a double-quoted command, which runs a completely new shell. (You can use a similar method to invoke cmd, gitbash, or similar.) If you need to quote something inside of the command, use single quotes to distinguish the inside from the outside of the shell command in double quotes:

bash -c "echo 'Printing LIQUIBASE_* env vars' && env | grep 'LIQUIBASE_'"

Chained Command Example in Flow file

stages:
  chainedCommandExample:
    actions:
    - type: shell
      #command: bash -c "echo 'Printing Liquibase_* env vars' && env | grep 'LIQUIBASE_'"
      command: echo 'Printing LIQUIBASE_* env vars' && env | grep 'LIQUIBASE_'

After you set up your Flow file, run the flow command:

liquibase flow --flow-file=flows/<path/to/your/flowfile>.yaml

The shell bash commits successfully and all environment variables display as intended.

Shell command options

  • Shell commands that utilize an equals sign (=) can be applied in flow files.
  • One of the many ways to use this feature is by exporting environment variables from within a flow command to use later. For example:

    - type: shell
      command: bash -c "UNDEPLOYED=$(grep -c 'have not been applied './${PROJNAME}/${STATUSFILE}') && echo $UNDEPLOYED"
  • You can specify the shell interpreter to execute the shell command. This can include but is not limited to bash, cmd, powershell, etc. For example:
  • - type: shell
      command: bash -c "UNDEPLOYED=$(grep -c 'have not been applied' './${PROJNAME}/${STATUSFILE}') && echo $UNDEPLOYED"
  • Multi-line shell commands can be applied in flow files. This can be applied by using the vertical line character (|) to begin the command. For example:
  • - type: shell
      command: |
        UNDEPLOYED=$(grep -c 'have been applied' '/tmp/status.txt')
        echo $UNDEPLOYED

Related links