Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRE_COMMANDS environment variable syntax #1605

Closed
timothysparg opened this issue Jul 14, 2022 · 6 comments
Closed

PRE_COMMANDS environment variable syntax #1605

timothysparg opened this issue Jul 14, 2022 · 6 comments
Labels
question Further information is requested

Comments

@timothysparg
Copy link

timothysparg commented Jul 14, 2022

It appears that you should be able to set PRE_COMMANDS as an environment variable, but I can't seem to get the syntax worked out, could somebody enlighten me as to how it should look?

It would appear that you need to pass it in as some kind of structured format, as I get the following stacktrace when I try use a normal string:

    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.10/site-packages/megalinter-0.1-py3.10.egg/megalinter/run.py", line 9, in <module>
    linter = megalinter.Megalinter({"cli": True})
  File "/usr/local/lib/python3.10/site-packages/megalinter-0.1-py3.10.egg/megalinter/MegaLinter.py", line 114, in __init__
    self.pre_commands_results = pre_post_factory.run_pre_commands(self)
  File "/usr/local/lib/python3.10/site-packages/megalinter-0.1-py3.10.egg/megalinter/pre_post_factory.py", line 13, in run_pre_commands
    return run_pre_post_commands("PRE_COMMANDS", "[Pre]", mega_linter)
  File "/usr/local/lib/python3.10/site-packages/megalinter-0.1-py3.10.egg/megalinter/pre_post_factory.py", line 42, in run_pre_post_commands
    return run_commands(pre_or_post_commands, log_key, mega_linter)
  File "/usr/local/lib/python3.10/site-packages/megalinter-0.1-py3.10.egg/megalinter/pre_post_factory.py", line 52, in run_commands
    pre_command_result = run_command(command_info, log_key, mega_linter)
  File "/usr/local/lib/python3.10/site-packages/megalinter-0.1-py3.10.egg/megalinter/pre_post_factory.py", line 60, in run_command
    if command_info.get("cwd", "root") == "workspace":
AttributeError: 'str' object has no attribute 'get'

My latest attempts where to try and use a JSON syntax, but this doesn't seem to be the correct approach either

[{"cwd": "workspace", "command:": "echo \"hello world\""}]

@timothysparg timothysparg added the question Further information is requested label Jul 14, 2022
@timothysparg timothysparg changed the title PRE_COMMAND environment variable syntax PRE_COMMANDS environment variable syntax Jul 14, 2022
@Kurt-von-Laven
Copy link
Collaborator

Kurt-von-Laven commented Jul 14, 2022

I also struggled with this, and we would gladly accept a pull request to improve the pertinent documentation. This is the syntax we have been using in our MegaLinter config files:

*_PRE_COMMANDS:
  - command: echo hello world

I have never even tried setting this via an environment variable, but if someone gets that working, I would be curious to learn how they did it.

@nvuillam
Copy link
Member

nvuillam commented Jul 14, 2022

We use get_list method to get PRE_COMMANDS value
When it is defined as ENV var, it's just a split on ,, so it works with list of strings, but it can not work with list of objects (like PRE_COMMANDS)

I'll try to check if I can have a different behaviour if the string starts with [

@timothysparg
Copy link
Author

I also struggled with this, and we would gladly accept a pull request to improve the pertinent documentation. This is the syntax we have been using in our MegaLinter config files:

*_PRE_COMMANDS:
  - command: echo hello world

I have never even tried setting this via an environment variable, but if someone gets that working, I would be curious to learn how they did it.

This is what I do as well but because I'm trying to workaround some git shenanigans in a repository, I also have to inject an environment variable.
I would prefer that this workaround only exists for our CI, as this means that we can't run locally without editing the config file.

PRE_COMMANDS: #$REPO_TOKEN is injected in the megaLinter workflow
  - command: git config --global url."https://oauth2:$REPO_TOKEN@github.com".insteadOf ssh://git@github.com

@Kurt-von-Laven
Copy link
Collaborator

You could use if [[ $CI == 'true' ]]; then ... assuming your CI platform sets CI=true.

nvuillam added a commit that referenced this issue Jul 14, 2022
* [automation] Auto-update linters version, help and documentation

* [MegaLinter] Apply linters fixes

* Fix build issues

* Build doc

* [automation] Auto-update linters version, help and documentation

* [MegaLinter] Apply linters fixes

* [MegaLinter] Apply linters fixes

* Fix SARIF count

* Disable trivy in PR DEV job (now secured by Ox)

* DevSkim: Ignore megalinter-reports

Fixes #1603

* Fix build.py for DOCKERFILE_DOCKERFILELINT

* Fix SARIF

* Fix SARIF reading

* Fix SARIF test method

* Fix counting sarif errors

* [MegaLinter] Apply linters fixes

* elif

quick build

* Load JSON when list obj objects in env vars

#1605

* changelog

quick build

* Update secretlint test cases

* [MegaLinter] Apply linters fixes

* AutoFix pre_commands using npm install

Fixes #1258

* Fix SARIF test class again

* Also get errors number from SARIF invocations

* Fix againnnn

* Fix test cases

* Linter fixes

Co-authored-by: nvuillam <nvuillam@users.noreply.github.com>
@timothysparg
Copy link
Author

@nvuillam oh wow, super fast turn around, just tried this with 6.0.4, and it works great!

my syntax (for Github Actions) was as follows

        env:
          PRE_COMMANDS: >-
            [{"cwd": "workspace", "command": "git config --global url.\"https://oauth2:${{ secrets. REPO_TOKEN }}@github.com\".insteadOf ssh://git@github.com"}]

@nvuillam
Copy link
Member

@timothysparg > I'm glad it works :) You can star the repo if you like 😇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants