https://github.com/amdmi3/github_env
Simple tool to manage $GITHUB_ENV in GitHub actions
https://github.com/amdmi3/github_env
environment-variables github-actions github-env
Last synced: 24 days ago
JSON representation
Simple tool to manage $GITHUB_ENV in GitHub actions
- Host: GitHub
- URL: https://github.com/amdmi3/github_env
- Owner: AMDmi3
- License: mit
- Created: 2022-04-28T13:55:27.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-04-28T16:38:41.000Z (about 4 years ago)
- Last Synced: 2025-01-18T04:44:01.818Z (over 1 year ago)
- Topics: environment-variables, github-actions, github-env
- Language: Python
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: COPYING
Awesome Lists containing this project
README
[](https://github.com/AMDmi3/github_env/actions/workflows/ci.yml)
[](https://codecov.io/gh/AMDmi3/github_env)
[](https://github.com/AMDmi3/github_env)
# github_env
When using GitHub actions, [environment
variables](https://docs.github.com/en/actions/learn-github-actions/environment-variables)
are often used. In complex scenarios, variables are stored in
[`$GITHUB_ENV`](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable)
file. However, this file does not allow straightforward appending
variables which is often needed. This small script simplifies this
task, allowing to modify environment by rewriting variables, as well
as appending, prepending, removing values from variables, and
undefining them. There's also support for conditional execution
based on expression.
## Usage
```shell
github_env.py FOO=bar # add or rewrite a variable
github_env.py FOO+=bar # append to a variable
github_env.py FOO++=bar # prepend to a variable
github_env.py FOO-=bar # remove value from a variable
github_env.py !FOO # undefine a variable
# explicitly pass path to env file, but you don't need it
# because it's retrieved from $GITHUB_ENV automatically
github_env.py --file env
# conditional execution to use with GitHub actions expressions
github_env.py --if ${{ matrix.compiler == 'clang' }} CFLAGS+=-Werror
```
## Obtaining
You can install the script in your CI environment with plain `curl`.
- It's advisable to fetch from a specific tag (and not a `master`
branch) to avoid possible breakages due to incompatible changes.
- You may set a shorter name for the script.
```shell
curl -s https://raw.githubusercontent.com/AMDmi3/github_env/0.0.1/github_env.py > e; chmod 755 e
```
## Example
It's pretty common to have a logic like this which tunes environment
based on settings from matrix:
```yaml
jobs:
build:
stragegy:
matrix:
include
- { cxx: g++, coverage: true }
- { cxx: clang++, coverage: false }
steps:
...
- name: Set up environment
run: |
echo 'CXX=${{ matrix.cxx }}' >> $GITHUB_ENV
echo 'CXXFLAGS=-Wall -Wextra -pedantic' >> $GITHUB_ENV
- name: Set up environment (compiler-specific flags)
if: ${{ matrix.cxx == 'clang++' }}
run: echo "CXXFLAGS=$CXXFLAGS -Wno-self-assign-overloaded" >> $GITHUB_ENV
- name: Set up environment (coverage)
if: ${{ matrix.coverage }}
run: |
echo "CXXFLAGS=$CXXFLAGS --coverage" >> $GITHUB_ENV
echo "LDFLAGS=$LDFLAGS --coverage" >> $GITHUB_ENV
```
And here's how it's simplified with `github_env.py` script:
```yaml
- name: Set up environment
run: |
curl -s https://raw.githubusercontent.com/AMDmi3/github_env/0.0.1/github_env.py > e; chmod 755 e
./e 'CXX=${{ matrix.cxx }}'
./e 'CXXFLAGS=-Wall -Wextra -pedantic'
./e --if ${{ matrix.cxx == 'clang++' }} 'CXXFLAGS+=-Wno-self-assign-overloaded'
./e --if ${{ matrix.coverage }} 'CXXFLAGS+=--coverage' 'LDFLAGS+=--coverage'
```
## Author
- [Dmitry Marakasov](https://github.com/AMDmi3)
## License
MIT, see [COPYING](COPYING).