I tried using the API of Unity Cloud Build from Python. I think it can be used when uploading a file built in cooperation with other systems.
The point that it can be built from the source code of BitBucket (former: Stash) was also high. The setting of the initial build was about 5 minutes with a button, so the explanation is omitted.
The first time is supposed to be built manually and the second and subsequent times are used via API. All the details of the API used this time are described in the official document.
If the build fails, HTTP Status: 202 will be returned. The URL of the completed file is a separate file
u.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import requests
API_TOKEN = "****"
ORGID = "****"
PROJECTS = "****"
BUILD_TARGETS = "****"
def get_url(func_name, extend=None):
auth_base = "orgs/{orgid}/projects/{projectid}/buildtargets/{buildtargetid}".format(orgid=ORGID,
projectid=PROJECTS,
buildtargetid=BUILD_TARGETS)
if extend:
url_base = "https://build-api.cloud.unity3d.com/api/v1/{auth}/{func}{extend}"
return url_base.format(auth=auth_base, func=func_name, extend=extend)
url_base = "https://build-api.cloud.unity3d.com/api/v1/{auth}/{func}"
return url_base.format(auth=auth_base, func=func_name)
def get_headers():
return {
"Content-Type": "application/json",
"Authorization": "Basic {token}".format(token=API_TOKEN),
}
def execute_build():
"""
Build execution
#Semi-normal system Already building
[{"buildtargetid":"****","buildTargetName":"****","error":"Cannot start build - already a build pending."}] 202
"""
response = requests.post(get_url("builds"), headers=get_headers(), data={})
print(response.text), response.status_code
return
#Build execution
execute_build()
The build status is as follows {queued, sentToBuilder, started, restarted, success, failure, canceled, unknown} If there is no charge, the queued time is long. If the build is successful, it will be success
u.py
def get_builds():
"""
You can get the latest 25 build information.
If you want to get older data, per_Modify page and page parameters
"""
response = requests.get(get_url("builds"), headers=get_headers())
print(response.text), response.status_code
return
def get_build(number):
"""
You can get the specified build information
#Semi-normal system
404 if it does not exist: {"error":"The requested object was not found."}
:param number: int
"""
response = requests.get(get_url("builds", extend="/" + str(number)), headers=get_headers())
print(response.text), response.status_code
return
def get_latest_build():
"""
You can get the latest 1 build
"""
build_start_result = requests.get(get_url("builds", extend="?per_page=1&page=1"), headers=get_headers())
print(build_start_result.text), build_start_result.status_code
return
#Get a specific build
get_build(6)
#Get the latest build
get_latest_build()
#Get all builds
get_build_status()
Is there anything you want to cancel from the API after implementing it? I thought. It is a specification to cancel with an HTML DELETE query. DELETE query is used for the first time this year. The character of the engineer who made the API faithfully to the REST principle was somehow conveyed.
u.py
def cancel_all():
"""
Cancel all builds
"""
response = requests.delete(get_url("builds"), headers=get_headers())
print(response.text), response.status_code
def cancel(number):
"""
Cancel a specific build
#Successful cancellation of normal system
204
#Semi-normal system does not exist
{"error":"The requested object was not found."} 404
#When you specify a build that has completed a quasi-normal build
204
:param number: int
"""
response = requests.delete(get_url("builds", extend="/" + str(number)), headers=get_headers())
print(response.text), response.status_code
#Specific build cancellation
cancel(7)
#Cancel all builds
cancel_all()
If the build is successful, the file will be uploaded to S3. A function to get the URL to download.
u.py
def get_latest_build_download_urls():
"""
Download URL for the latest successful build
:return: str
"""
response = requests.get(get_url("builds", extend="?buildStatus=success&per_page=1&page=1"),
headers=get_headers())
data = ujson.loads(response.text)
assert len(data) > 0
return data[0].get('links').get('download_primary').get('href')
#Download URL for the latest successful build
print get_latest_build_download_urls()
u.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import requests
import ujson
API_TOKEN = "****"
ORGID = "****"
PROJECTS = "****"
BUILD_TARGETS = "****"
def get_url(func_name, extend=None):
auth_base = "orgs/{orgid}/projects/{projectid}/buildtargets/{buildtargetid}".format(orgid=ORGID,
projectid=PROJECTS,
buildtargetid=BUILD_TARGETS)
if extend:
url_base = "https://build-api.cloud.unity3d.com/api/v1/{auth}/{func}{extend}"
return url_base.format(auth=auth_base, func=func_name, extend=extend)
url_base = "https://build-api.cloud.unity3d.com/api/v1/{auth}/{func}"
return url_base.format(auth=auth_base, func=func_name)
def get_headers():
return {
"Content-Type": "application/json",
"Authorization": "Basic {token}".format(token=API_TOKEN),
}
def execute_build():
"""
Build execution
#Semi-normal system Already building
[{"buildtargetid":"****","buildTargetName":"****","error":"Cannot start build - already a build pending."}] 202
"""
response = requests.post(get_url("builds"), headers=get_headers(), data={})
print(response.text), response.status_code
return
def get_builds():
"""
You can get the latest 25 build information.
If you want to get older data, per_Modify page and page parameters
"""
response = requests.get(get_url("builds"), headers=get_headers())
print(response.text), response.status_code
return
def get_build(number):
"""
You can get the specified build information
#Semi-normal system
404 if it does not exist: {"error":"The requested object was not found."}
:param number: int
"""
response = requests.get(get_url("builds", extend="/" + str(number)), headers=get_headers())
print(response.text), response.status_code
return
def get_latest_build():
"""
You can get the latest 1 build
"""
build_start_result = requests.get(get_url("builds", extend="?per_page=1&page=1"), headers=get_headers())
print(build_start_result.text), build_start_result.status_code
return
def get_latest_build_download_urls():
"""
Download URL for the latest successful build
:return: str
"""
response = requests.get(get_url("builds", extend="?buildStatus=success&per_page=1&page=1"),
headers=get_headers())
data = ujson.loads(response.text)
assert len(data) > 0
return data[0].get('links').get('download_primary').get('href')
def cancel_all():
"""
Cancel all builds
"""
response = requests.delete(get_url("builds"), headers=get_headers())
print(response.text), response.status_code
def cancel(number):
"""
Cancel a specific build
#Successful cancellation of normal system
204
#Semi-normal system does not exist
{"error":"The requested object was not found."} 404
#When you specify a build that has completed a quasi-normal build
204
:param number: int
"""
response = requests.delete(get_url("builds", extend="/" + str(number)), headers=get_headers())
print(response.text), response.status_code
#Specific build cancellation
# cancel(7)
#Cancel all builds
# cancel_all()
#Get a specific build
# get_build(6)
#Get the latest build
# get_latest_build()
#Get all builds
# get_build_status()
#Build execution
# execute_build()
#Download URL for the latest successful build
print get_latest_build_download_urls()
Recommended Posts