Exporting GitHub Secrets and Variables as Environment Variables in GitHub Actions

Eugene A
2 min readAug 23, 2023

In a continuous integration and continuous deployment (CI/CD) environment like GitHub Actions, handling secrets securely is essential. Sometimes, developers need to export multiple secrets as environment variables. Here’s a guide for doing just that.

Why Might You Need This?

If you have multiple secrets that you need to access as environment variables within your GitHub Actions workflow, you might find it tedious to manually map each one. By storing your secrets as a JSON object, you can quickly export them as environment variables in one go.

Step 1: Creating the Secret

First, create a GitHub secret in your repository or organization that contains a JSON object with your key-value pairs. In your GitHub repository, navigate to “Settings” -> “Secrets” and create a new secret. For example, you might create a secret named APPSETTINGS_VARIABLES_FOR_TESTS with the following content:

{
"YOUR_SECRET": "supersecret"
}

Step 2: Using the Secret in Your Workflow

Next, you’ll want to use this secret in your GitHub Actions workflow. Here’s a YAML snippet that demonstrates how to do this:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Export variables from secret
run: |
ENV_JSON='${{ secrets.APPSETTINGS_VARIABLES_FOR_TESTS }}'
echo "$ENV_JSON" | jq -r 'to_entries[] | "\(.key)=\(.value)"' >> $GITHUB_ENV

This code snippet does the following:

  1. Reads the secret containing the JSON object.
  2. Uses the jq command to parse the JSON object and extract the key-value pairs.
  3. Writes each key-value pair as an environment variable to $GITHUB_ENV, making them available as environment variables in subsequent steps of the job.

Security Considerations

While this method can be convenient, it’s essential to consider the security implications. Be sure to:

  • Only use this method with trusted tools and runners.
  • Be mindful of what you’re logging to avoid accidentally exposing sensitive information.
  • Understand the permissions and access controls in place for your secrets.

Convenience for .NET Developers with appsettings.json

.NET developers working with appsettings.json in ASP.NET Core projects can find this approach especially convenient for handling test configurations. Instead of manually adding each variable to your GitHub Actions environment, you can:

  1. Structure your variables in a JSON object, similar to your appsettings.json.
  2. Create a single GitHub secret (e.g., APPSETTINGS_VARIABLES_FOR_TESTS) with the JSON object.
  3. Use the secret in your workflow as described earlier.

By doing so, you simplify the synchronization of your development and testing environments in GitHub Actions, without needing to handle each variable individually. This method keeps your workflow efficient and maintainable, particularly when managing numerous environment variables.

Conclusion

This technique provides a streamlined way to handle multiple secrets in GitHub Actions. It can save time and simplify your workflow, but it must be used with care and an understanding of the security considerations involved.

Remember, handling secrets responsibly is crucial to maintaining the integrity and security of your code and data. Always follow best practices and consult your organization’s security guidelines.

--

--