How to remove an environment variable on GitHub actions?
Asked Answered
D

1

15

Context

On https://docs.github.com/en/actions/learn-github-actions/environment-variables they describe a way to add an environment variable with yaml:

jobs:
  job_name:
    env:
      NAME: value

On https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions/#setting-an-environment-variable linked from the same place, shows how to add an environment variable from script steps:

echo "NAME=value" >> $GITHUB_ENV

Question

I'm looking for a way to do the opposite: given that an env var was added with one of the above options, fully remove it so that subsequent job steps don't see it either.

I tried

echo "NAME=" >> $GITHUB_ENV

but it just sets it to empty string, I need it gone so that env doesn't list it any more.

Resources

Raised it GitHub community too: https://github.community/t/remove-environment-variable-from-job/214815 (dead link, not sure where GitHub migrated these...)

Deutsch answered 27/11, 2021 at 17:42 Comment(9)
Do you change a lot of other variables between setting and removing NAME. A somewhat brute force way to do this would be to save a copy of GITHUB_ENV prior to adding NAME and then overwrite the new one with the copy once you are done using NAME.Noctambulous
GITHUB_ENV points to a file, are you suggesting I could sed the variable out of it? Note: the variable was not set by me, it was set by one of the uses steps.Deutsch
Nope @Tyberius, the file pointed by $GITHUB_ENV is changing between every step. It only records the delta to be propagated to env., does not contain existing variables.Deutsch
Interesting question! I was wondering: as you can use env variable at different levels (workflow, jobs and steps)... Why not add this variable as output on an initial step, and only use the output as env variable on specific steps through the syntax env: NAME: ${{ steps.<step_id>.outputs.<output_name> }}, (or in other jobs) instead of adding it to the $GITHUB_ENV. Wouldn't it be an option?Ackley
Nope, @GuiFalourd, I'm not the one creating this env var, but it's a nice workaround for that case. I'll keep this in mind for other CIs because I've been communicating with env vars everywhere...Deutsch
Hey @Deutsch were you able to solve this? I'm struggling with the same problemMonaco
@AvijitGupta Nope, as far as I remember I was able to work around by changing the logic (if on a previous step that created it) to make sure it's not created.Deutsch
I'm curious about why you want to remove the environment variable, @TWiStErRob? Is it a security problem? Right now it looks like this is not possible yet: github.com/actions/runner/issues/1126. Also, the community post seems to be gone.Illicit
@Illicit you piqued my interest, I had no clue, too long ago, but would you believe I actually found it: commit so the answer is: setup-android sets up too many things, but one of the env vars point to an invalid location. This breaks further steps, and if I was able to unset it, it would just work. You can generalize this to "an action sets a variable that affects other steps".Deutsch
R
1

As of today, this is not possible, see https://github.com/actions/runner/issues/1126 ticket that tracks this feature request.

You will have to rely on your own build scripts to add logic for unsetting these variables that might be defined before they are called.

Rambunctious answered 8/8, 2024 at 8:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.