Environment name does not support matrix

It looks like the new environment property does not support a matrix variable. I would expect the following to be possible:

jobs:
  deploy:
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        environment: [dev, test]
      fail-fast: false
    environment: ${{ matrix.environment }}

I get the following error however: Unrecognized named-value: 'matrix'.

1 Like

Similarly, I would expect the following to work:

on: deployment

jobs:
  deploy:
    runs-on: ubuntu-20.04
    environment: ${{ github.event.deployment.environment }}
    env:
      ENVIRONMENT: ${{ github.event.deployment.environment }}
    steps:
      - name: Deploy to ${{ github.event.deployment.environment }}
        env:
          FOO: ${{ secrets.FOO }}
        run: echo "FOO in $ENVIRONMENT is ${FOO:0:1}+${FOO:1}"

Given that an environment can have up to 100 secrets that get mapped in env (and potentially more from the org and repo), it’s not sustainable to maintain duplicate workflows that change only the static environment string.

Additionally, because there is not an environment filter available for the deployment event trigger, it seems like the above on: deployment workflow would need to be rewritten to act as a switch statement that triggers one of the duplicated static environment workflows based on the value of github.event.deployment.environment. Something like this:

on: deployment

jobs:
  deploy:
    runs-on: ubuntu-20.04
    env:
      ENVIRONMENT: ${{ github.event.deployment.environment }}
    steps:
      - run: |
          if [ $ENVIRONMENT = "production" ]; then
            # trigger deploy-production workflow
          elif [ $ENVIRONMENT = "staging" ]; then
            # trigger deploy-staging workflow
          elif [ $ENVIRONMENT = "develop" ]; then
            # trigger deploy-develop workflow
          else
            # not a supported environment
          fi

Am I thinking about environments wrong? How is this feature intended to be used with only static strings?

Hello,

I’m able to make this kind of setup work:

deploy-prod:
    name: Deploy prod
    runs-on: linux
    needs: deploy-staging
    strategy:
      matrix:
        include:
          - environment: europe
            url: https://europe.my.service
          - environment: asia
            url: https://asia.my.service
          - environment: america
            url: https://america.my.service
          - environment: africa
            url: https://africa.my.service
    environment:
      url: ${{ matrix.url }}
      name: ${{ matrix.environment }}
    steps:
      - uses: actions/checkout@v2

      - name: deploy
        uses: ./.github/actions/deployments
        with:
          version: ${{ needs.deploy-staging.outputs.version }}
          environment: ${{ matrix.environment }}

I did not test with the ‘short’ version: environment: ${{matrix.xxx}}

@ngraef you use a on: deployment but the environment syntax will already manage the deployment (creation, change status, … ). So here it is like you have a 'when a new deployment is created, create a new deployment". Fortunatly, github prevent this infinite circle to happens.

@GregoireW You’re right, expression evaluation does work with the long form environment syntax:

jobs:
  deploy:
    environment:
      name: ${{ github.event.deployment.environment }}

As you mentioned, this does indeed cause an infinite loop of deployments. I have created a separate topic for that issue: Using environment secrets with deployment trigger causes infinite deployment loop