0

I would like to define some nested env variables like this:

name: push-deploy-docker

on:
  push:
    branches:
      - 'develop'
      - 'master'

env:
  AWS_ENV: "${{ github.ref_name == 'master' && 'prod' || contains(github.ref_name, 'release') && 'qa' || 'dev' }}"

  dev:
    AWS_ACCESS_KEY_ID: ${{ secrets.DEV_AWS_ACCESS_KEY_ID }}
    AWS_SECRET_ACCESS_KEY: ${{ secrets.DEV_AWS_SECRET_ACCESS_KEY }}
  qa:
    AWS_ACCESS_KEY_ID: ${{ secrets.QA_AWS_ACCESS_KEY_ID }}
    AWS_SECRET_ACCESS_KEY: ${{ secrets.QA_AWS_SECRET_ACCESS_KEY }}

So that at the jobs section of Github actions, I could have the dynamic call:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ [env.AWS_ENV]AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ [env.AWS_ENV]AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ [env.AWS_ENV]AWS_DEFAULT_REGION }}

When I run, Github tells me that the workflows at the env section is not valid, and the error is: "A mapping was not expected". Is there any way that I can define the env like that?

Chris Dao
  • 35
  • 5
  • It's not supported. There may be workarounds adding complexity in your workflow. Why do you need it? What is your exact use case? – Azeem May 09 '23 at 11:34
  • My use case is I would like to dynamically set the AWS credential based on the Github branch. For example: if I push code into development branch, Github will use the AWS credentials of Dev environment to automatically create EC2 and push code into AWS. – Chris Dao May 10 '23 at 05:48
  • Where is your environment being set? What does your `on` section look like? If it's dependent on the environment and the env vars are the same, shouldn't it be automatically taking the right credentials without any kind of switching? – Azeem May 10 '23 at 06:16
  • I have updated the `on` section, please take a look. The idea is I would like to check for the branch then choose the AWS credential that matches the Github branch. – Chris Dao May 10 '23 at 07:37

0 Answers0