# Context based on branch?

**URL:** <https://discuss.circleci.com/t/context-based-on-branch/42735>\
**Category:** Build Environment\
**Created:** [January 26, 2022, 10:21am UTC](https://discuss.circleci.com/t/context-based-on-branch/42735 "2022-01-26T10:21:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![baramuse](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/baramuse/32/29259_2.png) [@baramuse](https://discuss.circleci.com/u/baramuse)\
**Post date:** [January 26, 2022, 10:21am UTC](https://discuss.circleci.com/t/context-based-on-branch/42735/1 "2022-01-26T10:21:15Z")

</div>

Hi,  
I’m pulling my hair here and before I don’t have not one left I thought I may ask first for an easy solution to that simple problematic :  
depending on the branch, I want to execute the exact same workflows/jobs, but with different contexts

That simple right ?

---

<div class="post-metadata">

**Author:** ![JCi](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/jci/32/27318_2.png) [@JCi](https://discuss.circleci.com/u/JCi)\
**Post date:** [January 26, 2022, 4:31pm UTC](https://discuss.circleci.com/t/context-based-on-branch/42735/2 "2022-01-26T16:31:18Z")

</div>

Hi there and thanks for posting on CircleCI Discuss!

You can leverage the `filter` parameter to ensure jobs only run on the branches you set. You can specify which context you’d like to use for each job as well. So something like:

```auto
workflows:
  myworkflow:
    jobs:
      - jobA:
          context: A_Context
          filters:
            branches:
              only: devBranchA
      - jobB:
          context: B_Context
          filters:
            branches:
              only: devBranchB

```

If you add `jobA` twice in the same workflow, it will run with a numeric value added to the end of the job name while building to differentiate the duplicate job.

I hope this helps!

---

<div class="post-metadata">

**Author:** ![baramuse](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/baramuse/32/29259_2.png) [@baramuse](https://discuss.circleci.com/u/baramuse)\
**Post date:** [January 27, 2022, 2:42pm UTC](https://discuss.circleci.com/t/context-based-on-branch/42735/3 "2022-01-27T14:42:37Z")

</div>

Hi and thanks for the answer !

My workflow/jobs are eaxctly the same, I just want to, depending on the branch, to switch the context.  
I’d rather not duplicate the jobs

---

<div class="post-metadata">

**Author:** ![zura35](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/zura35/32/29914_2.png) [@zura35](https://discuss.circleci.com/u/zura35)\
**Post date:** [April 14, 2022, 4:12am UTC](https://discuss.circleci.com/t/context-based-on-branch/42735/4 "2022-04-14T04:12:58Z")

</div>

Another hack you can do is to include the branch name in your context, so you can do something like:  
Create context `context_branchA` and `context_branchB`, then

```auto
      - jobA:
          context: context_<< pipeline.git.branch >>
          filters:
            branches:
              only: 
                - branchA
                - branchB

```

Although it would be great if `when` clause is supported for context as well 😭

---

<div class="post-metadata">

**Author:** ![pd88](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/pd88/32/30183_2.png) [@pd88](https://discuss.circleci.com/u/pd88)\
**Post date:** [May 18, 2022, 7:24pm UTC](https://discuss.circleci.com/t/context-based-on-branch/42735/5 "2022-05-18T19:24:22Z")

</div>

Does pipeline.git.branch gets reconized. I get the below error. Does the above code work for anyone?

" **Could not find context “context\_\<\< pipeline.git.branch \>\>”.**"

---

<div class="post-metadata">

**Author:** ![goodshort](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/goodshort/32/31796_2.png) [@goodshort](https://discuss.circleci.com/u/goodshort)\
**Post date:** [December 19, 2022, 5:53pm UTC](https://discuss.circleci.com/t/context-based-on-branch/42735/6 "2022-12-19T17:53:23Z")

</div>

My CircleCI pipelines are still in the making, but so far based on my testing the below works:

```yaml
      - deploy:
          filters:
            branches:
              only:
                - production
                - development
          context:
            - "back-end-<< pipeline.git.branch >>"

```
