Search code examples
firebasecontinuous-integrationenvironment-variablesgithub-actionsshadow-cljs

How to use environment variables on GitHub actions without hard coding them? Is it possible?


I am using GitHub Actions to implement a Continuous Integration process on a Clojure/ClojureScript dynamic web app project. It uses a library called shadow-cljs for the compilation.

At the end of my yaml file, I have the current approach:


      - name: Execute a build report using shadow-cljs command shadow.cljs.build-report
        run: npx shadow-cljs run shadow.cljs.build-report app build-reports/report.html
        env:
          TFR_RELEASE: my-instance-name
          TFR_DEV: my-instance-name

As you see, I am using two environment variables TFR_RELEASE and TFR_DEV. These variables have their values being exported on my .zshrc file:

export TFR_RELEASE="my-instance-name"
export TFR_DEV="my-instance-name"

The values represent a firebase instance. This works. But, I would prefer to avoid hard coding them, since the instance may vary according to the developer or to the willingness to switch an instance.

Is it possible to make the script work without hardcoding the env values? Or, what would be an alternative strategy? Maybe creating an instance on Firebase only for the CI build test instead of using my own instance?


Solution

  • You should use Github secrets to store environment variables.

    • On GitHub.com, navigate to the main page of the repository.
    • Under your repository name, click Settings.
    • In the "Security" section of the sidebar, select Secrets, then click Actions.
    • Click New repository secret.
    • Type a name for your secret in the Name input box.
    • Enter the value for your secret.
    • Click Add secret.

    Then in your yml file,

          - name: Execute a build report using shadow-cljs command shadow.cljs.build-report
            run: npx shadow-cljs run shadow.cljs.build-report app build-reports/report.html        env:
              TFR_RELEASE: ${{ secrets.TFR_RELEASE}}
              TFR_DEV: ${{ secrets.TFR_DEV}}
    

    To access these github secrets in your code, use the "normal" method of your programming language to access an environment variable.