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: Dynamically added env variables #1786

Open
michaszcz opened this issue Sep 4, 2024 · 3 comments
Open

Feature: Dynamically added env variables #1786

michaszcz opened this issue Sep 4, 2024 · 3 comments

Comments

@michaszcz
Copy link

I'd like to have some option to set env variables dynamically for all steps of the task.

Example 1:

version: '3'

tasks:
  greet:
    cmds:
      - export GREETING=Hello
      - echo $GREETING  # Currently it'll print an empty string, I'd like to receive the value `Hello`

Example 2:

version: '3'

tasks:
  task_name:
    dynamic_env:  # Currently not possible
      sh: python script_that_exports_env_variables.py
    cmds:
      - echo $DYNAMIC_VAR1 $DYNAMIC_VAR2  # Env vars set by the script

Without the feature I have to export variables and use them in one step - example:
Example 2:

version: '3'

tasks:
  task_name:
    cmds:
      - ./export_vars.sh && echo $DYNAMIC_VAR1 $DYNAMIC_VAR2
      - echo $DYNAMIC_VAR1 $DYNAMIC_VAR2  # Currently vars are not available in this step
@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Sep 4, 2024
@vmaerten
Copy link
Member

This is possible to set dynamic env variable but only one at the same time :

version: '3'

tasks:
  greet:
    env:
      GREETING: {sh: echo 'hello'}
      FOO: bar
    cmds:
      - echo $GREETING

If you want to add multiple env variable at the same time, I would use dotenv files and follow this issue : #1381

@vmaerten vmaerten added state: awaiting response Waiting for issue author to respond. and removed state: needs triage Waiting to be triaged by a maintainer. labels Sep 21, 2024
@michaszcz
Copy link
Author

Even if dotenv were available, it would still have a few issues:

  1. It would require generating a dotenv file and removing it after task execution, which could cause issues in parallel execution.
  2. Environment variables must remain the same throughout the task's execution.

@task-bot task-bot removed the state: awaiting response Waiting for issue author to respond. label Sep 24, 2024
@Madgvox
Copy link

Madgvox commented Jan 26, 2025

+1

My use case is that I'd like to dynamically define $A or $B from a script depending on what's available in the environment. I implemented this logic as well as a bunch of other checks as an internal task and exported the appropriate variable for use in subsequent commands. Obviously, this didn't end up working as exported variables aren't visible to other commands. It would be nice to be able to affect the 'global task environment' in some way from within tasks. There are other methods to achieve what I'm doing, but they subvert Task's features rather than synergize with them.

Another use case: exporting environment variables from Task into the shell environment. Running export FOO=bar in the shell will make FOO available to other commands. Creating a task that does the same thing will have no effect. This matters in cases where you might want to simplify configuring your shell for some task or another (eg. setting the AWS_PROFILE to dev/prod, or exporting session tokens into the environment).

I think there's a lot of value in Task's sandboxing of environment variables, but it would be nice to have an escape hatch. Maybe something a la Github Action's echo "FOO=bar" >> $GITHUB_OUTPUT pattern.

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

No branches or pull requests

4 participants