Expose the commit branch to the environment as BRANCH #20

wants to merge 5 commits into

6 participants


This is a simple change to add BRANCH=#{commit.branch} to the build environment so that a build script (e.g. rake) can do things conditionally based on the branch.


Pure curiosity: what kind of things are you going to do conditionally depending on the branch under testing?


All travis-specific ENV variables are prefixed with TRAVIS_, for example, TRAVIS_RUBY_VERSION. I recommend keeping it that way.


@KL-7 This is primarily because I'd like to use travis to build and publish static websites via awestruct. And I don't want every push to cause a change to the production site. Only do that if the branch is 'production', otherwise just update the staging site.

Though I think it makes sense in other circumstances as well. Commits on master push to a testing server, for example.

@michaelklishin My mistake. I wasn't aware - will fix.

Travis CI member

have you run the features? I think they will fail with this change.

@simi simi commented on an outdated diff May 2, 2012

Argh - @joshk you are right. Stay tuned.

Travis CI member
rkh commented May 2, 2012

Uh, oh, isn't that the same as running git rev-parse --symbolic-full-name --abbrev-ref HEAD?


@rkh are you suggesting that I could just use git in the build script to get this info vs. from an env export in travis?

Doesn't that break if the commit hash is on two separate branches? I thought that travis was checking out the repo based on the commit hash alone, so the current working directory won't be associated with a branch, will it?

Travis CI member
rkh commented May 2, 2012

I think it should work. name-rev tries to find the best fitting name, which would be the branch. It might break if someone pushes to master after triggering the build but before the git clone ran, though.


Hmm - maybe I'm missing something. This is what I get with a dry run.

$ git clone git://github.com/awestruct/awestruct.git
Cloning into awestruct...
remote: Counting objects: 1809, done.
remote: Compressing objects: 100% (731/731), done.
remote: Total 1809 (delta 1009), reused 1777 (delta 984)
Receiving objects: 100% (1809/1809), 224.94 KiB, done.
Resolving deltas: 100% (1009/1009), done.

$ cd awestruct/

$ git checkout a727e6fc6bdeea7750d5820a4cc1e9c84e6279fd
Note: checking out 'a727e6fc6bdeea7750d5820a4cc1e9c84e6279fd'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at a727e6f... Bump version

$ git rev-parse --symbolic-full-name --abbrev-ref HEAD

Travis CI member
rkh commented May 2, 2012

Uh, oh, I gave you the wrong command, sorry, try this one: git name-rev --name-only HEAD.


Actually, it does work, if I fetch the ref first, as is being done here. https://github.com/travis-ci/travis-build/blob/master/lib/travis/build/scm/git.rb#L47

But still, are you suggesting that anyone who wants branch information has to shell out, execute a git command, slurp the results of that, and continue? Isn't it advantageous to add this information to the environment somehow?

Travis CI member
rkh commented May 2, 2012

No, I'm just throwing this in as an option to brainstorm. Travis might not necessarily be running on a branch.


@rkh You're right, I don't think Travis is really running on a branch - it's checking out the repo using the commit sha, which may be associated with multiple branches. But we do know, bassed on the commit ref what the branch associated with the commit is. And that's really the information I'm after.

Yes, I can do this inspection in the build script myself - and thanks for pointing that out. But it's more fun to submit pull requests to open source projects. :) And I'd be surprised if I'm the only person who would appreciate having the information available in some way at build time. In any case, it's been fun learning about how Travis works and poking around in the code. Yay open source!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment