# Reference a job between workflows?

**URL:** <https://discuss.circleci.com/t/reference-a-job-between-workflows/40700>\
**Category:** Build Environment\
**Tags:** workflow, circle-yml\
**Created:** [July 21, 2021, 8:19am UTC](https://discuss.circleci.com/t/reference-a-job-between-workflows/40700 "2021-07-21T08:19:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Repox](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/repox/32/13411_2.png) [@Repox](https://discuss.circleci.com/u/Repox)\
**Post date:** [July 21, 2021, 8:19am UTC](https://discuss.circleci.com/t/reference-a-job-between-workflows/40700/1 "2021-07-21T08:19:24Z")

</div>

Hello.

Say I have the two following workflows defined:

```auto
workflows:
  build-and-test:
    jobs:
      - build-and-run-tests
  deploy-my-build:
    jobs:
      - deploy-to-staging:
          requires: 
            - build-and-test
        filters:
          branches:
            only:
              - staging

```

The required job for the `deploy-to-staging` exists in another workflow. Is it somehow possible reference this or do I need to have duplicate jobs specified with different filters for the same workflow?

---

<div class="post-metadata">

**Author:** ![thekatertot](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/thekatertot/32/25169_2.png) [@thekatertot](https://discuss.circleci.com/u/thekatertot)\
**Post date:** [July 27, 2021, 6:35pm UTC](https://discuss.circleci.com/t/reference-a-job-between-workflows/40700/2 "2021-07-27T18:35:15Z")

</div>

Hey @Repox! I’m pretty sure you can’t do this. Workflows operate independent of each other. If a job is dependent on another job, they should generally be in the same workflow.

---

<div class="post-metadata">

**Author:** ![jkzilla](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/jkzilla/32/19750_2.png) [@jkzilla](https://discuss.circleci.com/u/jkzilla)\
**Post date:** [July 27, 2021, 6:50pm UTC](https://discuss.circleci.com/t/reference-a-job-between-workflows/40700/3 "2021-07-27T18:50:24Z")

</div>

Would referencing the job details via API work for you? We have a job endpoint in Preview where you can use the API in the other workflow - See:

[https://circleci.com/docs/api/v2/#tag/Job](https://circleci.com/docs/api/v2/#tag/Job)

---

<div class="post-metadata">

**Author:** ![timvisher](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/timvisher/32/27678_2.png) [@timvisher](https://discuss.circleci.com/u/timvisher)\
**Post date:** [July 27, 2021, 7:44pm UTC](https://discuss.circleci.com/t/reference-a-job-between-workflows/40700/4 "2021-07-27T19:44:47Z")

</div>

> [@Repox](#):
>
> The required job for the `deploy-to-staging` exists in another workflow. Is it somehow possible reference this or do I need to have duplicate jobs specified with different filters for the same workflow?

It looks like you’re trying to build a simple deployment pipeline. Do I have that right?

If so, I think there’s no reason to have separate workflows at all? Assuming that you want to deploy to staging every time a commit is pushed to `staging` and you want to run all the jobs defined in `build-and-test` then just make it look like:

```auto
workflows:
  build-and-deploy:
    jobs:
      - build
      - test
      - lint:
          filters:
            branches:
              ignore:
                - staging
                - main
      # `build` and `test` are run in parallel. See
      # https://circleci.com/blog/introducing-workflows-on-circleci-2-0/
      - deploy:
          name: deploy_staging
          requires: 
            - build
            - test
          filters:
            branches:
              only: staging
          deploy_env: staging
      - deploy:
          name: deploy_production
          requires:
            - build
            - test
          filters:
            branches:
              only: main
          deploy_env: production

```

In this case `deploy_production` and `deploy_staging` will require `build` and `test` but will only run on their respective branch. When the branch doesn’t match then they simply won’t run (in fact they won’t even be shown in the workflow view).

The behavior here is fairly sophisticated and I would recommend setting up a throwaway branch and just experimenting with it. Probably the most surprising bit of behavior is that if a job in a workflow requires another job which is removed because it was filtered out for whatever reason that job _will_ run (i.e. it the filter is fail → open by design). Once you get used to this behavior it’s actually fairly convenient.

Other things to be aware of:

1. [Anchors and Aliases in CCI’s YAML Guide](https://circleci.com/docs/2.0/writing-yaml/#anchors-and-aliases). I use these judiciously. I think their best application is duplicating a workflow for commits and cron execution. I go back and forth on using them for ‘duplicated’ jobs (like the `deploy_*` jobs above).
2. [CCI’s Reusable Config Reference](https://circleci.com/docs/2.0/reusing-config/). Properly using parameterized jobs can get around a lot of reuse issues.

Good luck!
