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

0
0
Asked By MellowCedar47 On

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?

2 Answers

Answered By BrightLynx8 On

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`.

QuietMaple21 -

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`.

Answered By NimbleOrbit36 On

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.

CopperVale5 -

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.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.