Skip to content

Pre-commands

MegaLinter can run custom commands before running linters (for example, installing a plugin required by one of the linters you use).

Example in .mega-linter.yml config file

PRE_COMMANDS:
  - command: npm install eslint-plugin-whatever
    cwd: root        # Will be run at the root of the MegaLinter Docker image
    secured_env: true  # True by default. If set to false, no global variables will be hidden (for example, if you need GITHUB_TOKEN)
    run_before_linters: True # Will be run before the execution of the linters themselves; required for npm/pip commands that cannot be run in parallel
  - command: echo "pre-test command has been called"
    cwd: workspace   # Will be run at the root of the workspace (usually your repository root)
    continue_if_failed: False  # Will stop the process if the command fails (return code > 0)
  - command: pip install flake8-cognitive-complexity
    venv: flake8 # Will be run within the flake8 Python virtualenv. There is one virtualenv per Python-based linter, with the same name
  - command: export MY_OUTPUT_VAR="my output var" && export MY_OUTPUT_VAR2="my output var2"
    output_variables: ["MY_OUTPUT_VAR","MY_OUTPUT_VAR2"] # Will collect the values of output variables and update MegaLinter's own ENV context
  - command: echo "Some command called before loading MegaLinter plugins"
    cwd: workspace   # Will be run at the root of the workspace (usually your repository root)
    continue_if_failed: False  # Will stop the process if the command fails (return code > 0)
    tag: before_plugins # Tag indicating that the command will be run before loading plugins
  - command: echo "Some command called after running MegaLinter linters"
    run_after_linters: True # Will be run after the execution of the linters themselves
Property Description Default value
command Command line to run Mandatory
cwd Directory where to run the command (workspace or root) workspace
run_before_linters If set to true, runs the command before the execution of the linters themselves, required for npm/pip commands that cannot be run in parallel false
run_after_linters If set to true, runs the command after the execution of the linters themselves false
secured_env Apply filtering of secured environment variables before calling the command (default true).
Be careful if you disable it!
true
continue_if_failed If set to false, stop the MegaLinter process in case of command failure true
venv If set, runs the command in the related Python venv
output_variables ENV variables to read from output after running the commands, and store in MegaLinter's ENV context so they can be reused in subsequent commands []
tag Tag defining at which command entry point the command will be run (available tags: before_plugins)