diff --git a/check_pipeline_status.py b/check_pipeline_status.py index e627495429a265058db69d51447a3e5937909d52..71eb0cd7cb9f5b2a6537da71d2be3cb298a5cbd2 100755 --- a/check_pipeline_status.py +++ b/check_pipeline_status.py @@ -2,12 +2,14 @@ import common import argparse +import os import sys import time -from gitlab import Gitlab +from gitlab import Gitlab, GitlabGetError +from gitlab.v4.objects import Project -def check_pipeline_status(project, commit): +def check_pipeline_status(project: Project, commit): """Get latest pipeline status for a given commit""" # Find pipeline for commit @@ -25,7 +27,7 @@ def check_pipeline_status(project, commit): time.sleep(1) pipeline = project.pipelines.get(pipeline.id, retry_transient_errors=True) print("") - print("Result: %s" % pipeline.status) + print("Result: %s" % pipeline.status, flush=True) return pipeline.status @@ -64,9 +66,27 @@ def main(): status = check_pipeline_status(project, args.commit) - if status != "success": + # If we are running in a job environment and the upstream status is canceled, + # explicitly cancel this job as well + job_project_path = os.environ.get("CI_PROJECT_PATH") + job_id = os.environ.get("CI_JOB_ID") + if job_project_path and job_id and status == "canceled": + try: + job_project = common.get_project(gitlab, job_project_path) + job = job_project.jobs.get(id=job_id, retry_transient_errors=True) + # Wait some time until Gitlab has flushed the job log, otherwise it will be + # truncated before all lines are visible + time.sleep(5) + job.cancel() + except GitlabGetError: + print("WARNING: job %s not found in project %s" % (args.job, args.project)) + + # Else just reflect job status in return value + elif status != "success": sys.exit(1) + sys.exit(0) + if __name__ == "__main__": main()