Context based on branch?

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 ?

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:

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!

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

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

      - 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 :sob:

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 >>”. "

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

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