# How to not repeat writing jobs with different contexts?

**URL:** <https://discuss.circleci.com/t/how-to-not-repeat-writing-jobs-with-different-contexts/33926>\
**Category:** Tips, Tricks, and Hacks\
**Created:** [December 30, 2019, 3:31pm UTC](https://discuss.circleci.com/t/how-to-not-repeat-writing-jobs-with-different-contexts/33926 "2019-12-30T15:31:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam-6over6](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/adam-6over6/32/12187_2.png) [@adam-6over6](https://discuss.circleci.com/u/adam-6over6)\
**Post date:** [December 30, 2019, 3:31pm UTC](https://discuss.circleci.com/t/how-to-not-repeat-writing-jobs-with-different-contexts/33926/1 "2019-12-30T15:31:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![aecorredor](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/aecorredor/32/24192_2.png) [@aecorredor](https://discuss.circleci.com/u/aecorredor)\
**Post date:** [April 17, 2020, 10:09pm UTC](https://discuss.circleci.com/t/how-to-not-repeat-writing-jobs-with-different-contexts/33926/2 "2020-04-17T22:09:01Z")

</div>

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

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