Search code examples
automationcontinuous-integrationgithub-actions

Accessing environment variable in github actions


I have a web application which has different environment like development, stage and prod. Each of the environment have specific URL like

  1. development:- my-app-dev.com
  2. stage:- my-app-stg.com
  3. prod:- my-app.com

I am building a github action which requires these URLs, I created environments under settings of my github repo and added variable name as URL in all 3 environments. Now, i want to access this URL basis of my environment selection during runner.

So far this is how i am selecting a environment

name: Run Playwright Tests
on:
  workflow_dispatch:
    inputs:
      environment:
        type: environment
        description: Select the environment

Now how do i use the environment to access the variable inside it.

I tried this

name: Run Playwright Tests
on:
  workflow_dispatch:
    inputs:
      environment:
        type: environment
        description: Select the environment
jobs:
  build:
    runs-on: uhg-runner-m

    steps:
      - name: get-env-var
        run: echo "Environment is:$URL"

I am expecting the output to be my-app-dev.com, if i select development during workflow dispatch

Here is the image of how i set environment variable enter image description here


Solution

  • Define environments in Github - Dev, staging and prod.

    Define environment specific variable for all the 3 environments - URL (Not Prod_URL, Staging_URL but same name). This variable value will be different for your different github environments.

    Set environment in your job -

    environment: ${{ inputs.environment }}

    Use the variable in your job - ${{ vars.URL}}