16
votes

Can TeamCity push successful builds to a git repository?

I cannot see a specific build step in TeamCity to do this.
I use the version 7.1.1 of TeamCity

Thanks, Henrik

UPDATE:

Ok thanks for your answer, I find it a bit complicated. I found out that I can simply push back tags on successful builds to my global repository from which TeamCity fetches data for the build. I can pull changes from it and see whether the last commits were successful.

I would be happy if TeamCity provided a simple option for this kind of workflow!

It would be awesome if every developer could just pull from a repo that is only updated when the build is successful, or am I wrong here?

5

5 Answers

6
votes

You can have TeamCity execute a shell script that subsequently calls git push (with appropriate arguments, e.g. git push <repository> to push to a different repository). Do make sure that git doesn't need interactive authentication for the push operation.

A related example (deploy to Heroku using a git push) can be found here: http://blog.carbonfive.com/2010/08/06/deploying-to-heroku-from-teamcity/.

5
votes

If you upgrade to 8 or newer you can just make one or more "Automatic Merge" build features. This will push to remote repo. I didn't find it at first either because of the confusing naming but it makes sense that they have to support many different VCS with different naming.

5
votes

i finally made it!

You have to add a build parameter in your teamcity project:

name= env.PATH
value= C:\Program Files (x86)\Git\cmd

and then you add a new commandline build step with custom script:

call git push "C:\Gruene Git Repos\TeamCityApp" master

the "call" word is important!

Thanks for the help! henrik

2
votes

Easy Answer

For a while TeamCity has supported VCS Labeling, this allows your VCS Root user (if it has write permissions) to tag the commit hash that was just built with the version or anything else TeamCity knows about (see entire list of parameter references in the TeamCity wiki).

An aside

As stated in another answer the Automatic Merge functionality available in TeamCity will automatically perform a merge into a requested branch from the specified list of branches (wildcard enabled) and it will monitor and build and only merge them if it succeeds.

The Automatic Merge functionality can be good, but if you don't have good test coverage it can also be dangerous as a developer could break something that doesn't have a test and that will cause issues in your code long down the road. One way to prevent this is to require +2 tests be created/run every time the project builds (configurable in TeamCity). These caveats are mentioned in the previously linked article announcing the Automatic Merge feature.

Related Resolution

We encountered a similar issue not related directly to merging, but having a requirement to push some changes from the job beyond the "lightweight tags" (for Git at least) that TeamCity adds if you use VCS Labeling (terrible name).

What we ended up doing was:

  1. Using a Parameter of the type "Environment Variable" (visible to the build agent, the other types are not) and setting the "Spec" to make the field of type "Password" which will prevent the entered text from showing in either the config UI or the Job log output.
  2. Entered the username and password as parameters on the job config
  3. Created a script that looked at the git remote URL of the "agent side" repository and added a new remote with the username and password inline in the url (http://gituser:[email protected]/path/to/repo.git) in order to push the changes on a new branch.
    • We then remove the remote at the end of the script so that anyone accessing the system can't pull out the credential. Of course the credential is also fairly tightly scoped to only access certain repositories, but the least privilege rule is always good to follow.
0
votes

My solution maybe is stupid, but simple. The steps is as follows:

  1. Cloning your heroku from heroku git repository it will create your heroku app folder.

    heroku git:clone -a {app-name}

  2. Copy the files you need to update to heroku

    xcopy client "{app-name}/client" /e/i/h/y

    xcopy server "{app-name}/server" /e/i/h/y

    xcopy imports "{app-name}/imports" /e/i/h/y

  3. git add.

    cd {app-name} && git add .

  4. git commit

    cd {app-name} && git commit --message 'ok'

  5. push to heroku

    cd {app-name} && git push heroku master