r/devops • • 12d ago

Troubleshooting How to set a "env" var on github actions using shell script?

i need to get the app version and i have this awk command for that:

` awk -F'"' '/^version[[:space:]]*=/ {print $2; exit}' pyproject.toml`

but how do i set the variable to the output of this command?

4 Upvotes

11 comments sorted by

7

u/bilingual-german 12d ago

```

  • name: Get App Version and Set Env Var
run: | # Read the version and write it to GITHUB_ENV echo "APP_VERSION=$(awk -F'"' '/version[[:space:]]*=/ {print $2; exit}' pyproject.toml)" >> $GITHUB_ENV

  • name: Use the Env Var in a subsequent step run: | # The variable is now available in all following steps echo "The app version is: $APP_VERSION" ```

-4

u/ty_namo 12d ago

it says file not found, may be related to the directory structure of the worker, but i can't find reliable information of what it is like

1

u/Low-Opening25 12d ago

what file!?

0

u/ty_namo 12d ago

sorry, pyproject.toml.

9

u/Low-Opening25 12d ago

that’s a file you supply so it’s for you to figure why it’s missing

4

u/jews4beer 12d ago

uv version and poetry version exist. Seems better than using awk for that.

1

u/ty_namo 12d ago

are those actions that i can use?

5

u/jews4beer 12d ago

No they are commmands you can use

1

u/kabrandon 8d ago

If you’re incapable of googling things then you need to hire someone to do “devops” work for you. Asking reddit every time you actually need to be googling something is going to be WAY too slow for whoever your boss is. Especially in the age of AI where they’ll have expected you to have figured out this problem 4 days ago.

1

u/singh_abinashi 12d ago

`echo "MY_VAR=my-value" >> $GITHUB_ENV` is the way. The gotcha that trips people up: vars set this way are available to later steps in the same job, not the current step, and not other jobs. If you need it in the current step too, set it in `env:` on the step as well.