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

お困りですか?

アトラシアン コミュニティをご利用ください。

コミュニティに質問

プラットフォームについて: Server および Data Center のみ。この記事は、Server および Data Center プラットフォームのアトラシアン製品にのみ適用されます。

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.

*Fisheye および Crucible は除く

要約

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. 

ソリューション

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.

最終更新日 2021 年 9 月 30 日

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.