Skip to content

Latest commit

 

History

51 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📦 Variables Allocation

This GitHub Action replaces environment variable placeholders defined in one or more input files (ENV_FILES) using repository secrets and vars. It also extracts the CloudFront distribution ID (CLOUDFRONT_DIST_ID) and outputs it for use in subsequent steps.

Ideal for AWS deployment workflows, environment-based configuration, and secrets-driven .env generation.


✨ Features

  • 🪧 Replaces placeholders like '{MY_SECRET}' with actual values from secrets or vars
  • 🔄 Supports branch-prefixed variables (main_MY_SECRET, etc.)
  • 📤 Exposes CLOUDFRONT_DIST_ID as an output
  • 📄 Writes a ready-to-use .env or configuration file

📂 Expected Input Format

Each input file listed in ENV_FILES should contain lines with variables in this format:

API_KEY='{API_KEY}'
SOME_FLAG='true'

Braces ('{...}') will be replaced with actual values from GitHub repository secrets or variables.


🧠 Usage

1. Add one or more template files, e.g. .env.prod and .npmrc.template:

API_URL='{API_URL}'
SECRET_TOKEN='{SECRET_TOKEN}'

2. Define values in your GitHub repository as secrets or vars

You can define global or branch-specific variables, such as:

  • MASTER_API_URL
  • DEV_SECRET_TOKEN

3. Use the Action in your workflow:

jobs:
  generate_env:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Variable allocation & CLOUDFRONT_DIST_ID
        id: variables_allocation
        uses: matiascariboni/action-variables_allocation@v2.0.0
        with:
          ENV_FILES: |
            [
              ["./.env.prod", "./.env"],
              ["./.npmrc.template", "./.npmrc"]
            ]
          REPO_VARS: ${{ toJson(vars) }}
          REPO_SECRETS: ${{ toJson(secrets) }}

      - name: Print output
        run: echo "CloudFront ID: ${{ steps.variables_allocation.outputs.CLOUDFRONT_DIST_ID }}"

Each pair in ENV_FILES is processed sequentially: position 0 is the input template, position 1 is the resolved output file. Add as many pairs as needed to generate multiple files in a single run.


👇🏻 Inputs

Name Description Required
ENV_FILES JSON array of [input, output] file path pairs, e.g. [["./.env.prod", "./.env"], ["./.npmrc.template", "./.npmrc"]]. Processed sequentially. ✅ Yes
REPO_VARS Variables allocated on current repo ✅ Yes
REPO_SECRETS Variables allocated on current secret ✅ Yes
CLOUDFRONT Enable resolving CLOUDFRONT_DIST_ID from vars/secrets (default: false) ❌ No

🔄 Outputs

Name Description
CLOUDFRONT_DIST_ID The resolved CloudFront distribution ID

⚠️ Variable resolution order

  1. ${BRANCH}_VAR_NAME in secrets
  2. ${BRANCH}_VAR_NAME in vars
  3. VAR_NAME in secrets
  4. VAR_NAME in vars

⚖️ Special rules

  • Lines starting with // are ignored (treated as comments)
  • If a value is not found, the step fails explicitly (except CLOUDFRONT_DIST_ID, which is optional)
  • CLOUDFRONT_DIST_ID resolution is opt-in: it only runs when the CLOUDFRONT input is set to true. When disabled (the default), the step is skipped entirely and no output is set
  • If the output file ends with .json, values are always written as JSON strings
  • Prefix ~ inside a placeholder (e.g., '~{VAR}') means the value will be inserted without quotes
  • Arrays like [1,2,3] are detected and preserved as raw values
  • If the value is not a number, boolean, array, or prefixed with ~, it is wrapped in single quotes ('value')
  • Values are inserted literally: characters like &, /, \, and quotes are preserved as-is and never treated as special replacement syntax

📜 License

This project is licensed under the MIT License.


🤝 Contributing

Contributions are welcome! Feel free to open issues or pull requests.


✍ Author

Developed by Matías Cariboni.

About

Action made to replace the env variables placeholders

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages