How to commit and push to a Git repo during a build

Still need help?

The Atlassian Community is here for you.

Ask the community

Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

To commit local changes (performed during the build in the build directory) to a git repository and then push the commits to a git repository as part of the build. 

Solution

Bamboo version 6.7 and above

tip/resting Created with Sketch.

Bamboo source control tasks are recommended over script tasks as not only do they reduce the need for manually writing a script task but they also allow you to utilize the credentials stored against the repository in Bamboo rather than relying on authentication methods embedded into the script or available in the environment. 

The recommended way to make changes to git repositories during the build is to use a couple of Source Control tasks as explained in: https://confluence.atlassian.com/bamboo/configuring-a-source-control-task-956167494.html

A common set of actions would be to perform local changes in the build directory followed by a commit to the repository and a push to the repository. To achieve this use a Script task that performs the changes, followed by a Repository Commit task and a Repository Push task.

Bamboo before version 6.7

The changes to repositories during the build can be performed by the user by using Script tasks that are manually configured. An example of a task configuration to achieve this is presented below:

Even if a repository has been defined in a build plan and a source-code checkout task proceeds this script task, it is still necessary to set a new remote like in this example as the origin remote will point to Bamboo's internal git cache, not the external Git repository.

  1. Add a Script task to a Job in your build plan.

  2. Insert the following code as an inline script. Rather than changing the origin remote url, this example opts to add an additional remote called central to push to instead (since the origin remote points to Bamboo's internal git cache when repository caching is enabled):

    touch file.txt
    
    git remote add central https://username:${bamboo_bitbucket_password}@bitbucket.org/path/to/reponame.git
    git config --global user.email "user@example.org"
    git config --global user.name "username"
    git add file.txt
    git commit -m 'adding a file'
    git push central master
    1. Alternatively, you could use git remote origin set-url instead if you prefer to use origin: https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories#changing-a-remote-repositorys-url
  3. Replace 'username' with your git repository user name.
  4. Replace 'bitbucket.org' with the URL of your repository.
  5. Replace 'path/to/reponame.git' with the path to your repository.
  6. Replace 'user@example.org' with your e-mail address.

  7. The example above is adding an empty 'file.txt' to the repository but one can replace 'file.txt' with whatever one may want to commit.

  8. Change 'adding a file' to change the commit message.

  9. Finally, ${bamboo_bitbucket_password} is a custom variable that you'll need to first define with the password to your git repository.

Last modified on Sep 30, 2021

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.