Using the git branch to run different commands

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!

1 Like

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.

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

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

      - 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
1 Like

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.

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

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
1 Like

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