# Using the git branch to run different commands

**URL:** <https://discuss.circleci.com/t/using-the-git-branch-to-run-different-commands/21250>\
**Category:** Build Environment\
**Created:** [April 4, 2018, 3:17pm UTC](https://discuss.circleci.com/t/using-the-git-branch-to-run-different-commands/21250 "2018-04-04T15:17:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![justinperkins](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/justinperkins/32/13066_2.png) [@justinperkins](https://discuss.circleci.com/u/justinperkins)\
**Post date:** [April 4, 2018, 3:17pm UTC](https://discuss.circleci.com/t/using-the-git-branch-to-run-different-commands/21250/1 "2018-04-04T15:17:20Z")

</div>

In process of converting from 1.0 to 2.0 …

Was using a feature of the 1.0 “deployment” section to run a different command depending on the branch name. In 2.0, I see that we use workflows to only run the build for a specific branch, but I’m struggling to find examples where certain commands are run inside a workflow. I’m likely just struggling to understand the docs and example configs.

For example, let’s say I have two branches, foo & bar. After the build has completed, I want to run the command “rake foo” for the “foo” branch and “rake bar” for the “bar” branch.

How would I achieve this? Thanks!

---

<div class="post-metadata">

**Author:** ![DylanVann](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/dylanvann/32/18147_2.png) [@DylanVann](https://discuss.circleci.com/u/DylanVann)\
**Post date:** [April 10, 2018, 5:22am UTC](https://discuss.circleci.com/t/using-the-git-branch-to-run-different-commands/21250/2 "2018-04-10T05:22:48Z")

</div>

I’ve been able to have specific workflows run depending on the branch, but it seems like it doesn’t keep the state from the main build workflow so there’s a ton of config duplication.

---

<div class="post-metadata">

**Author:** ![justinperkins](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/justinperkins/32/13066_2.png) [@justinperkins](https://discuss.circleci.com/u/justinperkins)\
**Post date:** [April 10, 2018, 1:36pm UTC](https://discuss.circleci.com/t/using-the-git-branch-to-run-different-commands/21250/3 "2018-04-10T13:36:14Z")

</div>

Yup that’s the same conclusion I’ve reached as well. Not having the best time migrating to 2.0 so far.

---

<div class="post-metadata">

**Author:** ![DylanVann](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/dylanvann/32/18147_2.png) [@DylanVann](https://discuss.circleci.com/u/DylanVann)\
**Post date:** [April 11, 2018, 1:41am UTC](https://discuss.circleci.com/t/using-the-git-branch-to-run-different-commands/21250/4 "2018-04-11T01:41:59Z")

</div>

I haven’t had a chance to test it yet, but I read on another thread that something like this should work:

```auto
      - type: deploy
        name: "Deploy to staging"
        command: |
          if ["${CIRCLE_BRANCH}" == "staging"];
            now ./_site -t ${NOW_TOKEN}
            now -t ${NOW_TOKEN} alias thing-staging
          fi
      - type: deploy
        name: "Deploy to production"
        command: |
          if ["${CIRCLE_BRANCH}" == "master"];
            now ./_site -t ${NOW_TOKEN}
            now -t ${NOW_TOKEN} alias thing
          fi

```

---

<div class="post-metadata">

**Author:** ![justinperkins](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/justinperkins/32/13066_2.png) [@justinperkins](https://discuss.circleci.com/u/justinperkins)\
**Post date:** [April 11, 2018, 1:21pm UTC](https://discuss.circleci.com/t/using-the-git-branch-to-run-different-commands/21250/5 "2018-04-11T13:21:21Z")

</div>

I ended up moving my branch detection logic into an external task, so I just execute that in one my job steps, no more need for workflows.

---

<div class="post-metadata">

**Author:** ![eddiewebb](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/eddiewebb/32/11733_2.png) [@eddiewebb](https://discuss.circleci.com/u/eddiewebb)\
**Post date:** [April 11, 2018, 2:23pm UTC](https://discuss.circleci.com/t/using-the-git-branch-to-run-different-commands/21250/6 "2018-04-11T14:23:21Z")

</div>

Just a thought, but it you can use the `attach_workspace` to pass anything from the build stage to subsequent deploy stages in the workflow. Then using the `filter` feature in the workflow you can distinguish staging, production, etc. And with less task specific logic in the jobs, you are getting some better level or repeatability/visibility into how each job behaves over time.

[https://circleci.com/docs/2.0/workflows/#using-workspaces-to-share-data-among-jobs](https://circleci.com/docs/2.0/workflows/#using-workspaces-to-share-data-among-jobs)

```
workflows:
  version: 2
  main:
jobs:
  - build
  - deploy-prod:
      requires:
        - build #alternately this could depend on deploy-dev to have a linear flow instead of split
      filters:
        branches:
          only: master
  - deploy-dev:
      requires:
        - build
      filters:
        branches:
          ignore: master #assumoing you DONT want this to run on master

version: 2
jobs:
  build:
docker:
  - image: YOURIMAGE
steps:
  - checkout
  - run:
      name: "Build"
      command: COMMANDS
   - persist_to_workspace:
      root: .
      paths:
        - directoryWithStuff

  publish-dev:
docker:
  - image: YOURIMAGE
steps:
  - attach_workspace:
      at: .
  - run:
      name: "Publish to Dev"
      command: |
        rake foo
       
  publish-prod:
docker:
  - image: YOURIMAGE
steps:
  - attach_workspace:
      at: .
  - run:
      name: Publish to Prod
      command: |
        rake bar
```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [July 10, 2018, 2:32pm UTC](https://discuss.circleci.com/t/using-the-git-branch-to-run-different-commands/21250/7 "2018-07-10T14:32:02Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
