metaflow icon indicating copy to clipboard operation
metaflow copied to clipboard

allow multiple environment decorators

Open amerberg opened this issue 1 year ago • 1 comments

Allow multiple environment decorators on a step. This is helpful when a user wants to set some environment variables in code and others from the CLI using --with. When multiple environment decorators are supplied, the vars for each decorator are set in order. A message is logged in the case of conflicting values.

I've tested with the following example flow:

import os

from metaflow import (
     environment,
     step,
     FlowSpec,
)


class EnvFlow(FlowSpec):

    @environment(vars={"VAR1": "val1"})
    @environment(vars={"VAR2": "val1"})
    @environment(vars={"VAR3": "val1"})
    @step
    def start(self):
        print(f"VAR1 = {os.getenv('VAR1')}")
        print(f"VAR2 = {os.getenv('VAR2')}")
        print(f"VAR3 = {os.getenv('VAR3')}")
        self.next(self.end)

    @environment(vars={"VAR1": "val2", "VAR2": "val3"})
    @step
    def end(self):
        print(f"VAR1 = {os.getenv('VAR1')}")
        print(f"VAR2 = {os.getenv('VAR2')}")
        pass


if __name__ == "__main__":
    EnvFlow()

I've run --with environment:vars='{"VAR1":"val4", "VAR2":"val3"}' both locally and on Argo Workflows. Environment variables have the correct values in both cases, though in the case of Argo, the messages about the conflicting values are logged when argo-workflows create is run, rather than when the workflow is run.

amerberg avatar Mar 29 '24 13:03 amerberg

@savingoyal -- could you comment on this one (you had some comments)

romain-intel avatar Apr 30 '24 07:04 romain-intel