2021-09-27 22:37:18 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import gitlab
|
|
|
|
|
2021-10-05 15:20:32 +00:00
|
|
|
CERBERO_PROJECT = 'gstreamer/cerbero'
|
|
|
|
|
2021-09-27 22:37:18 +00:00
|
|
|
|
|
|
|
class Status:
|
|
|
|
FAILED = 'failed'
|
|
|
|
MANUAL = 'manual'
|
|
|
|
CANCELED = 'canceled'
|
|
|
|
SUCCESS = 'success'
|
|
|
|
SKIPPED = 'skipped'
|
|
|
|
CREATED = 'created'
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def is_finished(cls, state):
|
|
|
|
return state in [
|
|
|
|
cls.FAILED,
|
|
|
|
cls.MANUAL,
|
|
|
|
cls.CANCELED,
|
|
|
|
cls.SUCCESS,
|
|
|
|
cls.SKIPPED,
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def fprint(msg):
|
|
|
|
print(msg, end="")
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2021-10-05 00:24:58 +00:00
|
|
|
|
2021-09-27 22:37:18 +00:00
|
|
|
if __name__ == "__main__":
|
2021-10-05 15:20:32 +00:00
|
|
|
server = os.environ['CI_SERVER_URL']
|
|
|
|
gl = gitlab.Gitlab(server,
|
2022-04-21 14:14:00 +00:00
|
|
|
private_token=os.environ.get('GITLAB_API_TOKEN'),
|
|
|
|
job_token=os.environ.get('CI_JOB_TOKEN'))
|
|
|
|
|
2022-10-25 14:27:49 +00:00
|
|
|
def get_matching_user_project(project, branch):
|
|
|
|
cerbero = gl.projects.get(project)
|
|
|
|
# Search for matching branches, return only if the branch name matches
|
|
|
|
# exactly
|
|
|
|
for b in cerbero.branches.list(search=cerbero_branch, iterator=True):
|
|
|
|
if branch == b.name:
|
|
|
|
return cerbero
|
|
|
|
return None
|
|
|
|
|
2022-10-26 09:17:52 +00:00
|
|
|
cerbero = None
|
2022-04-21 14:14:00 +00:00
|
|
|
# We do not want to run on (often out of date) user upstream branch
|
|
|
|
if os.environ["CI_COMMIT_REF_NAME"] != os.environ['GST_UPSTREAM_BRANCH']:
|
|
|
|
try:
|
2022-07-16 05:32:51 +00:00
|
|
|
cerbero_name = f'{os.environ["CI_PROJECT_NAMESPACE"]}/cerbero'
|
2022-10-25 14:27:49 +00:00
|
|
|
cerbero_branch = os.environ["CI_COMMIT_REF_NAME"]
|
|
|
|
cerbero = get_matching_user_project(cerbero_name, cerbero_branch)
|
2022-04-21 14:14:00 +00:00
|
|
|
except gitlab.exceptions.GitlabGetError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if cerbero is None:
|
2022-07-16 05:32:51 +00:00
|
|
|
cerbero_name = CERBERO_PROJECT
|
2022-04-21 14:14:00 +00:00
|
|
|
cerbero_branch = os.environ["GST_UPSTREAM_BRANCH"]
|
2022-10-25 14:27:49 +00:00
|
|
|
cerbero = gl.projects.get(cerbero_name)
|
2021-09-27 22:37:18 +00:00
|
|
|
|
2022-07-16 05:32:51 +00:00
|
|
|
fprint(f"-> Triggering on branch {cerbero_branch} in {cerbero_name}\n")
|
|
|
|
|
|
|
|
# CI_PROJECT_URL is not necessarily the project where the branch we need to
|
|
|
|
# build resides, for instance merge request pipelines can be run on
|
|
|
|
# 'gstreamer' namespace. Fetch the branch name in the same way, just in
|
|
|
|
# case it breaks in the future.
|
|
|
|
if 'CI_MERGE_REQUEST_SOURCE_PROJECT_URL' in os.environ:
|
|
|
|
project_url = os.environ['CI_MERGE_REQUEST_SOURCE_PROJECT_URL']
|
|
|
|
project_branch = os.environ['CI_MERGE_REQUEST_SOURCE_BRANCH_NAME']
|
|
|
|
else:
|
|
|
|
project_url = os.environ['CI_PROJECT_URL']
|
|
|
|
project_branch = os.environ['CI_COMMIT_REF_NAME']
|
|
|
|
|
2021-09-27 22:37:18 +00:00
|
|
|
pipe = cerbero.trigger_pipeline(
|
|
|
|
token=os.environ['CI_JOB_TOKEN'],
|
2022-04-21 14:14:00 +00:00
|
|
|
ref=cerbero_branch,
|
2021-09-27 22:37:18 +00:00
|
|
|
variables={
|
2022-07-16 05:32:51 +00:00
|
|
|
"CI_GSTREAMER_URL": project_url,
|
|
|
|
"CI_GSTREAMER_REF_NAME": project_branch,
|
2022-02-05 03:46:49 +00:00
|
|
|
# This tells cerbero CI that this is a pipeline started via the
|
2022-02-04 14:49:57 +00:00
|
|
|
# trigger API, which means it can use a deps cache instead of
|
|
|
|
# building from scratch.
|
2022-02-05 03:46:49 +00:00
|
|
|
"CI_GSTREAMER_TRIGGERED": "true",
|
2021-09-27 22:37:18 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
fprint(f'Cerbero pipeline running at {pipe.web_url} ')
|
|
|
|
while True:
|
|
|
|
time.sleep(15)
|
|
|
|
pipe.refresh()
|
|
|
|
if Status.is_finished(pipe.status):
|
|
|
|
fprint(f": {pipe.status}\n")
|
|
|
|
sys.exit(0 if pipe.status == Status.SUCCESS else 1)
|
|
|
|
else:
|
|
|
|
fprint(".")
|