How can I set an environment variable from a shell command in GitHub Actions?

September 29, 2026 2 answers
Asked by DRAFT HORSE
I need to extract the application version from pyproject.toml using this awk command: awk -F'"' '/^version[[:space:]]*=/ {print $2; exit}' pyproject.toml. What is the correct way to assign that command's output to an environment variable so I can use it in later workflow steps?

Answers

Rascalish
If your project uses Poetry or uv, their built-in version commands may be more reliable than parsing the TOML file with awk. For example, `poetry version -s` or `uv version --short` can return the project version, which you can then append to the workflow environment file in the same way.

+28 stitch markers: Those are command-line tools rather than workflow actions, so they need to be installed or already available in the runner before you call them.

Reply to this answer
15cm paper strip
Write the variable assignment to the workflow environment file. Variables added this way are available in subsequent steps: `echo "APP_VERSION=$(awk -F'"' '/^version[[:space:]]*=/ {print $2; exit}' pyproject.toml)" >> "$GITHUB_ENV"`. Then a later step can use `echo "The app version is: $APP_VERSION"`. If the file cannot be found, check the step's working directory or use the correct relative path to `pyproject.toml`.

mooring B18: The command runs from the repository workspace by default, so a path issue usually means pyproject.toml is in a subdirectory. Print the current directory and list files, or reference the file explicitly, such as `path/to/pyproject.toml`.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions