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

Feature Request: per-command pre/post workflow hooks #2149

Closed
gtirloni opened this issue Mar 18, 2022 · 3 comments · Fixed by #3708
Closed

Feature Request: per-command pre/post workflow hooks #2149

gtirloni opened this issue Mar 18, 2022 · 3 comments · Fixed by #3708
Labels
feature New functionality/enhancement

Comments

@gtirloni
Copy link
Contributor

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request. Searching for pre-existing feature requests helps us consolidate datapoints for identical requirements into a single place, thank you!
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.

Overview of the Issue

Atlantis allows the definition of pre/post workflow hooks that apply to all the commands being executed. Sometimes these hooks are only meant to work with a given command and will fail. This does not break the workflow because workflow hook failures are not fatal, but it'll spam the logs with errors that are not relevant.

If Atlantis allow per-command workflow hooks, we could define some hooks that only execute for, say, apply and not for plan.

Reproduction Steps

The configuration below just illustrated that the workflow hooks are executing commands that only really apply to the plan command in the workflow definition. When the apply command runs, it won't generate the needed file which will cause errors in the post_workflow_hooks commands. This is a simplification of the situation we had in a more complex custom workflow.

repos:
  - id: /.*/
    branch: /.*/
    workflow: default
    pre_workflow_hooks:
      - run: rm -f /tmp/somefile.txt
    post_workflow_hooks:
      - run: cat /tmp/somefile.txt
workflows:
  default:
    plan:
      - run: echo hello > /tmp/somefile.txt
    apply:
      - run: echo world > /tmp/some_other_file_that_isnt_somefile.txt

Logs

{"level":"error","ts":"2022-03-18T13:42:42.208Z","caller":"events/command_runner.go:264","msg":"Error running post-workflow hooks exit status 1: running \"...\"

Environment details

Atlantis 0.19.2

@gtirloni gtirloni added the bug Something isn't working label Mar 18, 2022
@emcniece
Copy link

emcniece commented Mar 30, 2022

I'm currently working on a custom script for the pre/post workflow hooks that only needs to run for the apply step. There is no environment variable context during execution that tells us whether we are in the plan or apply stage.

This requested feature would solve the problem by letting us specify something like pre_apply_workflow_hooks or

pre_workflow_hooks:
  - run:
    command: cat /tmp/somefile.txt
    step: apply

Another solution might be to add the ProjectPlanStatus integer to the WorkflowHookCommandContext that is used in server/events/pre_workflow_hooks_command_runner.go. I'm not sure exactly how that context could be used to populate an environment variable from there however.

@jamengual jamengual added feature New functionality/enhancement and removed bug Something isn't working labels Aug 26, 2022
@GMartinez-Sisti
Copy link
Member

This might help -> https://www.runatlantis.io/docs/custom-workflows.html#environment-variable-env-command. You could set an environment variable with the step name and then use it in the script.

@brandon-fryslie
Copy link

Couldn't you run the script as part of the custom workflow itself and add || true to the end?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality/enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants