How to not repeat writing jobs with different contexts?

Hey,

I’m trying to create an optimize CircleCI configuration file.
I have a repeating jobs that I need to write in my config.yml file.
For instance, here is the jobs that I need to run in my workflow:

---
build_and_deploy_microservice:
jobs:
  - tests
  - aws-ecr/build-and-push-image:
      name: Build & Push to branch#1
      context: environment#1
      create-repo: true
      repo: ${MICROSERVICE_REPO}
      filters:
        branches:
          only: branch#1
      requires:
        - tests
  - deploy_service_update:
      name: Deploy Micro Service#1 to Environment#1
      <<: *mc1_params
      context: environment#1
      filters:
        branches:
          only: branch#1
      requires:
        - Build & Push to branch#1
  - deploy_service_update:
      name: Deploy Micro Service#2 to Environment#1
        <<: *mc2_params
        context: environment#1
        filters:
          branches:
            only: branch#1
        requires:
          - Build & Push to branch#1
  - deploy_service_update:
      name: Deploy Micro Service#3 to Environment#1
        <<: *mc3_params
        context: environment#1
        filters:
          branches:
            only: branch#1
        requires:
          - Build & Push to branch#1
  - deploy_service_update:
      name: Deploy Micro Service#4 to Environment#1
        <<: *mc4_params
        context: environment#1
        filters:
          branches:
            only: branch#1
        requires:
          - Build & Push to branch#1
  - deploy_service_update:
      name: Deploy Micro Service#5 to Environment#1
        <<: *mc5_params
        context: environment#1
        filters:
          branches:
            only: branch#1
        requires:
          - Build & Push to branch#1

Now, I have 7 different environments and 6 different branches.
If I’m keeping it as is, I need to rewrite the above yaml config 7 times (each time for each environment).
Seems to me that it’s not right, that I’m doing something wrong.
Is anyone knows how can I optimize the above config? Is there a way to write it one time and somehow repeat it with variables?

I think using matrices will help you out (I think):

https://circleci.com/docs/2.0/configuration-reference/#dependencies-and-matrix-jobs

1 Like