How can I push code to a remote server using bamboo?
Asked by davidsons:

I'm running an on prem Bitbucket repo with an on prem Bamboo server.

I'd like to checkout code on commit and then push the changes to a different server automatically.

I'm at a loss for how to get things to function. Here is what I've done so far:

1. Created repositories and everything is properly pushing and pulling
2. Connected Bitbucket and Bamboo successfully
3. Installed git on all of the servers and it works like a charm from command line and gui interfaces
4. Created a build plan that has 1 job... it checks out the code from the repo

This is where I get stuck. I created a script task that uses a git push command but for some reason, bamboo is not allowing the git command to work. I tried the following:

* writing the command inline
* placing it in a batch file
* I tried including the $ before git and not
* I tried powershell, shell with a bash shebang, and cmd.exe to no avail

I think I'm missing a crucial component but don't know what I don't know. Any help would be greatly appreciated.

Screens below show the log and how one instance of the command is setup.![2018-09-13 08_08_39-Gallery of Guns - GOG Development - Push to dev_ Edit job configuration - Atlass.png](/t5/image/serverpage/image-id/32196iB28E7E652656D5DB/image-size/large?v=v2&px=999 "2018-09-13 08_08_39-Gallery of Guns - GOG Development - Push to dev_ Edit job configuration - Atlass.png")![2018-09-13 08_09_48-Gallery of Guns - GOG Development - Push to dev 11_ Build log - Atlassian Bamboo.png](/t5/image/serverpage/image-id/32195iFD7E2813141552FC/image-size/large?v=v2&px=999 "2018-09-13 08_09_48-Gallery of Guns - GOG Development - Push to dev 11_ Build log - Atlassian Bamboo.png")


Answers:
author: Rajath Veeramanchala
body: Hi [@davidsons](/t5/user/viewprofilepage/user-id/1453868) ,

The ideal way to deploy your code to a server is using a deployment project in bamboo with scp tasks to copy your source code to the server.

When you use Source code checkout task, that will bring all your source code in to the bamboo build directory. Now you can use a SCP task after that to copy instead of using git push. It is not required to install git on your destination server.

More ideally , you should create an artifact of your source code in the bamboo plan, then create a deployment project which will pick this artifact and deploy to your destination server using SCP tasks. That way you can use this artifact to deploy to multiple environments configured under deployment project and it\`s also preferred method as it is easy to trace back your releases compared to doing SCP with in the bamboo plan. You also get a rollback option by using deployment projects

Hope this helps

Regards,

Rajath.

comments: author: davidsons
body: Hi [@Rajath Veeramanchala](/t5/user/viewprofilepage/user-id/1143588),

I originally wanted to go this route but I'm running into an issue where it deploys the entire source code directory instead of the changed files only.

Could you provide an explanation for how to only package up changed files and deploy those specifically?

-or-

If that's not possible, should I be packaging the whole source as an artifact and then inside of the deployment project, telling it to only deploy the changed files while dumping the rest?

Thanks,

Dominic


author: Rajath Veeramanchala
body: Hi [@davidsons](/t5/user/viewprofilepage/user-id/1453868),

You can actually package only changed code with few tweaks in bitbucket but this requirement changes based on the project. If your source code is small , then it is ideal to package it as a whole artifact and deploy so as to make it easy for rollbacks. But if your source code is large , then you can use the below method to create an artifact of only changed code :

Use this option to see only the files changed in the previous commit :

```
git diff --name-only HEAD HEAD^1
```

and you can create and archive of changed files using the below command :

```
git archive --output=latest_artifact.zip HEAD $(git diff --name-only HEAD HEAD^1) 
```

To make sure this runs correctly, You have to do the following :

1. Make sure your merge strategy is Merge commit - Usually makes a merge commit every time resulting in your latest commit having changes of all the commits from source branch to target branch.For example : 3 commits from feature to dev will add 4 commits to dev with the 4th commit being merge commit and having all changes of the first 3 commits.

2. Disable Automatic merging under Branching model - Merging master to dev automatically or release/\* branches to dev

3. Run the command in script task after your source code checkout task

The only problem with the merge strategy being merge commit is that it will create a large commit history and with Automatic merging it can further mess up the commit history. Also you can\`t have any .zip files in your repo and in bamboo you can create an artifact of the \*.zip file and then use deployment project / SCP task to copy the zip file to server.

Hope this helps. The version is 5.12 when I\`m writing this answer. I have tested multiple scenarios using this method and it worked as expected. Let me know if you see any issues.

Regards,

Rajath


author: davidsons
body: Hi [@Rajath Veeramanchala](/t5/user/viewprofilepage/user-id/1143588),

Thanks for the helpful information. I've been able to fix and am moving right along.

Cheers,

Dominic


author: andylorenz
body: sounds fine for new and modified files, but what about deleted files in the repo ? i.e, cleaning up files deleted from the repo on the target server?


author: Edwin Kyalangalilwa
body: Hi [@davidsons](/t5/user/viewprofilepage/user-id/1453868),

The issue is that Git is not in the path. Please confirm that you can run the git command manually on the server. If not, then check your path.

comments: author: davidsons
body: Hi [@Edwin Kyalangalilwa](/t5/user/viewprofilepage/user-id/255918)

git is in the path already and can be run manually on the server


Tags: bitbucket

bitbucket-server

git

server