Environment variables "leak" into Docker container action. Why? How to prevent?

Imagine, you have have a custom third-party Docker container action implemented with Java. This actions uses JAVA_HOME environment variable to run it’s logic. Think about Gradle application plugin which creates and executable Java application that relies on that environment variable. Imagine this Docker container action is based on openjdk:17.

This action is really simple, it prints the environment and JDK location:

#!/usr/bin/env sh

echo "Environment:"

printf "\n"
echo "JAVA_HOME:"
printenv JAVA_HOME

printf "\n"
echo "Java location:"
command -v java

printf "\n"
echo "Java version:"
java --version

If you just run this custom action, it would work:

- uses: madhead/actions-env-leak@main

The output would be:


Java location:

Everything is fine.

Now, imagine your workflow also needs Java, but a different version:

- uses: actions/setup-java@v2
    distribution: 'adopt'
    java-version: '11'

Now, if you run the custom action again, it is screwed up:


Java location:

Notice that JAVA_HOME now points to a wrong location! This variable just leaked from the workflow! Now, if the custom action relies on it to run the Java application, it would fail, because this path is not valid inside the container!

This is what actually happened with one of my actions. The issue is discussed here.

I have created a minimal reproducible example here.

Why do the variables leak? Why is this behaviour implicit? Shouldn’t it be explicit? The workflow author may want to override some of the variables, but if should not be the default, shouldn’t it? Can action or workflow authors prevent this leakage from happening right now?


Conceptually similar issue: Dockerd leaks environment variables into the containers · Issue #25 · moby/moby · GitHub

This is what a support told me on my request to clarify the behavior:

Thank you for reaching out to GitHub Support and sorry to hear you’ve experienced this issue.

I’ve reached out to the Actions team and they confirmed that most (if not all) environment variables from the runner are passed through to a container when run to allow access to things like secrets and other custom environment variables.

I’m afraid there are currently no plans to change the behaviour around environment variables in these Actions - sorry about that.

We really appreciate feedback on how we can make GitHub even better and the best way to report any feature requests is directly to us through our feedback form: Share feedback - GitHub Support

Our roadmap is now publicly visible, so we recommend that you keep an eye on the GitHub Blog and roadmap for the latest announcements about new features.