Skip to content

πŸ”  Fetch and use deployment environment variables without hitting deployment.

License

Notifications You must be signed in to change notification settings

raven-actions/environment-variables

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

πŸ”  Environment Variables Action

GitHub - marketplace GitHub - release GitHub - ci Codecov GitHub - license


⚠️ The name of the action may be confusing, and it is not related to the ordinary meaning of the environment variables, as we know on the OS level (after all, it can set environment variables anyway), but to variables on the GitHub Deployment Environments.

One way to handle deployments is to use Deployment Environments. However, there may be cases where Deployment Environment variables need to be used before the deployment process.

With this GitHub Action, you can access Deployment Environment variables from GitHub workflow not associated with any deployment. For example, this allows you to delay approval until after the planning stage but before applying it or any other situations where you must utilize Deployment Environment variables without proceeding with Environment deployment.

The action will fetch all variables from the Deployment Environment and set them in the workflow as action output and/or as environment variables on the OS level.

πŸ“‘ Table of Contents

πŸ› οΈ Usage

Prerequisites (permissions)

You cannot use the built-in GitHub Token (well-known as ${{ github.token }} or ${{ secrets.GITHUB_TOKEN }}) with this action because it does not support Deployment Environments read access.

You have to create a fine-grained Personal Access Token on the Developer Settings page, or for more scalable organization-wide scenarios, you can consider GitHub Apps. It has a higher rate limit than PATs but requires additional configuration steps on your GitHub workflow. Please follow the GitHub App Setup example.

No matter which way you choose the required scope for this GitHub Action is environments:read.

Quick Start

The minimum required inputs are github-token with your GitHub Token and environment with the deployment environment name that you want to use to fetch variables.

The below example assumes the Deployment Environment name is staging and contains two variables:

  • MY_VAR1 with value Lorem ipsum
  • FOOBAR with value Abc123
- name: Staging Environment Variables
  id: staging-env-vars
  uses: raven-actions/environment-variables@v2
  with:
    github-token: ${{ secrets.MY_GH_TOKEN }}
    environment: staging

In the subsequence step, you can use variables in two ways.

  1. as action outputs, where the output name is the variable key.

    - run: |
        echo "${{ steps.staging-env-vars.outputs.MY_VAR1 }}"
        echo "${{ steps.staging-env-vars.outputs.FOOBAR }}"
  2. as environment variables, where the env name is the variable key.

    - run: |
        echo "${{ env.MY_VAR1 }}"
        echo "${FOOBAR}"

Whatever method you choose the output will be:

Lorem ipsum
Abc123

πŸ’‘ In some scenarios, you may use variables from multiple Deployment Environments in the same Job where the variable key is the same. To avoid override, you can prefix environment variables with the env-prefix input. See the πŸ“₯ Inputs section for more details.

πŸ“₯ Inputs

Name Type Required Default Description
github-token string true not set GitHub token to use for API authentication with environment:read. scope.
environment string true not set Deployment Environment name.
output-to string false all Output type. One of:
- action returns variables as action output
- env returns variables as environment variables
- all action output + envvars.
env-prefix string false not set Prefix for environment variables. Environment variables prefix will be upper-cased and striped from any special characters. A double underscore __ is placed between environment prefix and env name (MYPREFIX__VAR).
repository string false github.repository To fetch variables from Deployment Environment placed in different repository, set full repository name in the owner/repo format.
dry-run boolean false false Whether or not to actually perform operation. Set to true for testing. Will not set outputs/envvars - just log entries.

πŸ“€ Outputs

  • The action does not have any static action outputs. Only dynamic action output based on your variables from Deployment Environment, if output-to is all or action.
  • The action does not have any static environment variables outputs. Only dynamic environment variables output based on your variables from Deployment Environment, if output-to is all or env.

πŸ‘₯ Contributing

Contributions to the project are welcome! Please follow Contributing Guide.

πŸ›‘οΈ License

This project is distributed under the terms of the MIT license.