Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass environment variables to Github action

Github provides secrets, whose values can be used in workflows. Unfortunately, the values of secrets is protected and we can't easily see it in the repo or debug it in the workflow as it is scrubbed.

Is there a way to define an "environment variable" in the repository that can be easily seen and debugged? My use case is for configuration that can be easily modified if the repo is forked.

like image 531
Anugerah Erlaut Avatar asked Jun 21 '26 15:06

Anugerah Erlaut


1 Answers

You can store environment variables in an .env file like this:

FOO=bar

Then you can write code to append data from that file to $GITHUB_ENV:

name: CI

on:
  workflow_dispatch:

jobs:
  foo:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - run: cat .env >> $GITHUB_ENV

      - name: Use the value
        run: echo $FOO

You'll need to do cat .env >> $GITHUB_ENV (and use actions/checkout) for each job where you need to access env vars from that file.

DO NOT STORE SECRETS IN .env -- use it only for storing configurations, etc.

Complete code: https://github.com/brc-dd/env-from-file

You can also change .env to something like .env.github to keep things more organized.

like image 73
brc-dd Avatar answered Jun 24 '26 08:06

brc-dd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!