Dhaval D
Dhaval D

Reputation: 1495

How to pass environment variables from repository to Github action script

We are writing a generic github action workflow which will be checked into multiple repositories. Currently, we have to write many if and case conditions to manage some custom logic for different repos. For example, if it is a Java based repo then run build step but if it is a python repo then skip that step.

Is it possible for each repo to set an environment variable to this action saying something like JAVA-REPO=TRUE. Action can behave accordingly then.

Something similar to passing commandline arguments or reading a properties file which resides in repo and can be read by action. This will reduce complexity in the action script by a great deal.

Upvotes: 0

Views: 1549

Answers (1)

GuiFalourd
GuiFalourd

Reputation: 23280

Yes, you can set environment variables through your workflow and use them in your scripts.

For example, if you want to run a python script and use a env variable set on the action, you can do something like this in your workflow:

    steps:
      - name: Run script with env variable
        env:
          PYTHON-REPO: true
        run: python script.py     

And in your Python script, you will be able to do:

import os

repo = os.environ.get("PYTHON-REPO")

To retrieve the value and then use it.

In Java, the logic would be the same in the workflow, but in the .java class you would have to use something like this:

Boolean repo = System.getenv("JAVA-REPO");

Upvotes: 1

Related Questions