Different build command per branch

Is there a way to specify different build commands based on the the branch the build was triggered from?

For android specifically I’d like to be able to build 1 flavor+buildType per branch.
ex:
for branch/master: gradlew assembleMasterDebug
for branch/full: gradlew assembleFullRelease

This differentiation is currently available for the deployment step, but not the test step where the build happens.

4 Likes

The circle.yml file is per branch, so each branch can have a different one. Will that meet your needs?

Could you explain what you mean by each branch can have a different circle.yml? Maybe I’m missing something.

As best as I can tell from https://circleci.com/docs/1.0/configuration/ the only place you can do per-branch config is in the deployment phase. I need per-branch config in the test phase where the apk is built.

What he means is that when you push your branch to Github, and then CI pulls it and starts doing stuff. Well you pushed code that is specific to your branch. Your circle.yml is part of that branches code.
All you need to do is make your circle.yml files specific to their branch.

So in your master branch, your circle.yml calls gradlew assembleMasterDebug
In you full branch, your circle.yml calls gradlew assembleFullRelease instead.

As long as you keep the circle.yml files separate, it will work.

2 Likes

And then as soon as I merge master into full the circle.yml on full will have both gradlew assembleMasterDebug and gradlew assembleFullRelease.

Unless theres some way to keep circleci.yml from being merged that isn’t manual?

1 Like

Ah, I misunderstood and thought the two branches were not going to be merged. In that case you are looking for something like this.

You have access to https://circleci.com/docs/1.0/environment-variables/

One of them being $CIRCLE_BRANCH which is the name of the branch. You can check that and do different things in your code/commands.

Note when using the suggested method by @oppianmatt, the following does not work (using dependencies section as example)

dependencies:
  override:
    - [ $CIRCLE_BRANCH = 'master' ] && run_something

But the following does work

dependencies:
  override:
    - if [ $CIRCLE_BRANCH = 'master' ]; then run_something; fi

Unfortunately these steps will still pollute the build output, but at least you can control the execution of commands for a specific branch.

I would like to see CircleCI support the following

dependencies:
  branch: master
    override:
      - run_something
12 Likes

Is this going to happen someday? The deployment section already supports the branch filter - would be nice to have it in test or dependencies too!

3 Likes

Still no resolution for this after almost a year?

1 Like

I used the code in yml which is based on branch

machine:
services:
- docker

general:
branches:
only:
- develop
- release

dependencies:
override:
- |
if [ $CIRCLE_BRANCH = ‘develop’ ]; then
docker build --build-arg branch=$CIRCLE_BRANCH --no-cache=true -t latest .
docker push latest
fi
if [ $CIRCLE_BRANCH = ‘release’ ]; then
docker build --build-arg branch=$CIRCLE_BRANCH --no-cache=true -t release .
docker push release
fi

1 Like