Git Repository - Remote trigger
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
Purpose
The following will provide you with a better understanding on setting up Remote repository triggering in Bamboo when using Git repositories.
Please, refer to the following documentation for further information:
As per links above, you are able to delegate your Git repository the task to inform Bamboo when a build should run:
Requirements
- add Git repository to a Project / Plan in Bamboo
- add post-receive to your Git repository
- add a Remote trigger (Repository triggers the build when changes are committed)
- add Trigger IP Addresses (IP address from where the request will originate)
- check Plan configuration >> Permissions tab
Plan configuration >> Permissions
- When Anonymous users is enabled
- When Anonymous users is disabled
- When Anonymous users is disabled but providing user's authentication
- Notes
1. When Anonymous users is enabled
When running 'post-receive', Bamboo will run builds.
2. When Anonymous users is disabled
When running 'post-receive', Bamboo will not run builds and the following will be thrown in <bamboo-home>/logs/atlassian-bamboo.log
2016-11-17 17:19:21,753 INFO [http-bio-8085-exec-2] [AccessLogFilter] 127.0.0.1 GET http://localhost:8085/api/rest/updateAndBuild.action?buildKey=PROJ-PLAN 56080kb
2016-11-17 17:19:21,779 WARN [http-bio-8085-exec-2] [AuthorizationLoggerListener] Authorization failed: org.acegisecurity.AccessDeniedException: Access is denied; authenticated principal: org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken@9055e4a6: Username: anonymousUser; Password: [PROTECTED]; Authenticated: true; Details: org.acegisecurity.ui.WebAuthenticationDetails@957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS; secure object: ReflectiveMethodInvocation: public abstract java.util.List com.atlassian.bamboo.plan.cache.CachedPlanManager.getBranchesForChain(com.atlassian.bamboo.plan.PlanIdentifier); target is of class [com.atlassian.bamboo.plan.cache.CachedPlanManagerImpl]; configuration attributes: [ACL_BUILD_READ]
3. When Anonymous users is disabled but providing user's authentication
As mentioned previously, removing Anonymous users permission will prevent builds from running, unless you edit the 'post-receive' and run the GET method authenticating against Bamboo.
Please, find below examples on how to accomplish that:
#!/usr/bin/python
#
# ./post-receive http://localhost:8085 PROJ-PLAN
import sys
import urllib2
import base64
baseUrl = sys.argv[1]
buildKey = sys.argv[2]
# the user provided MUST have access on 'Plan configuration >> Permissions' tab
username = 'admin'
password = 'admin'
remoteCall = baseUrl + "/api/rest/updateAndBuild.action?buildKey=" + buildKey
request = urllib2.Request(remoteCall)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)
result.close()
#!/usr/bin/python
### ========== REQUIREMENTS
# install 'requests' as per http://docs.python-requests.org/en/master/user/install/
### ========== REQUIREMENTS : end
import requests
import json
import sys
import re
def post_receive(from_commit, to_commit, branch_name):
# the user provided MUST have access on 'Plan configuration >> Permissions' tab
username = 'admin'
password = 'admin'
planKey = 'PROJ-PLAN'
url = 'http://localhost:8085/rest/api/latest'
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
r = requests.get(url + '/plan/' + planKey + '/branch', auth=(username, password), headers=headers)
if (r.status_code == 200):
branches = json.loads(r.text)
for branch in branches["branches"]["branch"]:
if (branch["enabled"] == True):
if (re.sub("[^0-9a-zA-Z]+","-",branch_name).lower() == branch["shortName"].lower()):
planKey = branch["key"]
elif (r.status_code == 400):
print(r.text)
url = 'http://localhost:8085/rest/api'
headers = {'Content-Type': 'application/json'}
r = requests.post(url + '/latest/queue/' + planKey, auth=(username, password), headers=headers)
if (r.status_code == 200):
print "Success"
elif (r.status_code == 400):
print(r.text)
if __name__ == '__main__':
# get values from STDIN
fc,tc,bn = sys.stdin.read().split()
post_receive(fc, tc, bn)
Please, remember the 'post-receive' above is a Python script, so you can edit as you were writing a Python script, extending its functionality based on your use case:
- evaluate the committer so only a particular user and/or set of users can trigger builds in Bamboo
- only send event after X new commits
- allow triggering after 5PM
- allow triggering on Thursdays
4. Notes
It is important to notice the action call (/api/rest/updateAndBuild.action?buildKey=) from 'post-receive' above will check whether there are changes in the repository associated with the planKey provided as parameter. If so, Bamboo will:
- trigger a build against the buildKey provided
- will check if there are any other changes in the repository, including branches. If so, will trigger a build against branches.
In other words, when you have the following scenario and commit something against branch feature/bar (buildKey=PROJ-PLAN1), Bamboo will attempt on running a build against all branches (PROJ-PLAN, PROJ-PLAN0, PROJ-PLAN1) if repository changes were found.
Repository | Branch | PlanKey |
---|---|---|
http://my.git.server/proj/repository.git | master | PROJ-PLAN |
http://my.git.server/proj/repository.git | feature/foo | PROJ-PLAN0 |
http://my.git.server/proj/repository.git | feature/bar | PROJ-PLAN1 |
And you should find something similar to the following in <bamboo-home>/logs/atlassian-bamboo.log
2016-11-17 18:09:15,732 INFO [9-BAM::PlanExec:pool-16-thread-3] [ChangeDetectionListenerAction] No changes found for 'PROJ-PLAN0'
2016-11-17 18:09:15,732 INFO [9-BAM::PlanExec:pool-16-thread-4] [ChangeDetectionListenerAction] No changes found for 'PROJ-PLAN'
So if you intend on only trigger a particular build in Bamboo, then you should use post-receive, specific branch example instead.