Skip to content

university-of-york/ds-devtool-setEnvVars

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

ds-devtool-setEnvVars

GitHub action to set environment variables for a Job in a workflow.

Motivation

This action provides support for loading a set of environment variables from a file of key value pairs such as the following:

DATABASE_NAME=production

The initial intent of this action was to create a way of storing non-sensitive configuration variables that have different values per each environment that they are deployed to. It pre-dates GitHub's own configuration variables that can be stored per each GitHub environment.

⚠️ For most use cases, we recommend implementing configuration variables over using this action.

Using configuration variables

Imagine an action that deploys a service from GitHub to an external server.

When using this action, the DATABASE_NAME is defined in one of three .env files and is passed through at deploy time:

jobs:
    deploy:
        name: 'Deploy to external server'
        runs-on: ubuntu-latest
        strategy:
            matrix:
                environment: ['dev', 'staging', 'prod']
        environment: ${{ matrix.environment }}
        steps:
            - uses: actions/checkout@v4
            - uses: actions/setup-node@v3
              with:
                  node-version: 20
            - run: npm ci
            - uses: university-of-york/ds-devtool-setEnvVars@v3
              with:
                  envFile: .env.${{ matrix.environment }}
            - run: npm run deploy
              env:
                  DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
                  EXTERNAL_SERVER: ${{ matrix.environment }}

Instead, by storing the DATABASE_NAME in configuration variables for each of our three environments, we can pass the variable directly to our deployment step:

jobs:
    deploy:
        name: 'Deploy to external server'
        runs-on: ubuntu-latest
        strategy:
            matrix:
                environment: ['dev', 'staging', 'prod']
        environment: ${{ matrix.environment }}
        steps:
            - uses: actions/checkout@v4
            - uses: actions/setup-node@v3
              with:
                  node-version: 20
            - run: npm ci
            - run: npm run deploy
              env:
                  DATABASE_NAME: ${{ vars.DATABASE_NAME }}
                  DATABASE_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
                  EXTERNAL_SERVER: ${{ matrix.environment }}

One advantage to this, other than dropping the dependency, is that the whole configuration is defined in one place. We can mix variables, secrets, and static configuration without worrying about which ones can go in the external file and which ones cannot.

Using this action

The action requires an envFile input, which is a text file containing one or more key value pairs. The path to this is resolved from the current working directory.

For example, to populate the environment with variables from a staging.env file located in the root you would configure this action as follows:

- name: Set staging env vars
  uses: university-of-york/ds-devtool-setEnvVars@v3
  with:
      envFile: 'staging.env'

Overwriting existing variables

By default, this action will not replace environment variables that have been previously defined. You may change this by setting the overwrite flag:

- name: Set staging env vars
  uses: university-of-york/ds-devtool-setEnvVars@v3
  with:
      envFile: 'staging.env'
      overwrite: true

Developer notes

Note that this action is manually released, you will need to perform the following;

npm run build
# commit your changes
npm version minor # or major, or patch depending on scope
npm run move-major-tag
# push the changes