用于定制CI运行程序的GitLab runner客户端的非正式实现
gitlab-runner-api的Python项目详细描述
用于创建定制GitLab CI运行程序的API的非官方Python实现。在
这个包提供了注册Runner的基本功能。 然后可以使用该对象请求Job,并将日志、状态和工件通信回GitLab。 没有提供任何功能来执行.gitlab-ci.yml中定义的有效负载。在
这个包最初是为了支持LHCb’s Analysis Productions,它提供了一个可以向lhcdirac提交作业的CI运行程序。 更多细节可以在TODO中找到。在
注册跑步者
注册新的Runner的最简单方法是使用包含的命令行工具:
$ register-runner --help usage: register-runner [-h] [--locked] [--maximum-timeout MAXIMUM_TIMEOUT] api_url token output_fn positional arguments: api_url token output_fn optional arguments: -h, --help show this help message and exit --locked Lock the runner the to the current project --maximum-timeout MAXIMUM_TIMEOUT Maximum timeout set when this Runner will handle the job (in seconds)
例如
^{pr2}$其中参数可以通过导航到所需存储库设置的“CI/CD”页找到。在
找工作
注册运行程序后,可以从.json文件加载它并用于请求作业:
fromgitlab_runner_apiimportRunnerrunner=Runner.load("my-runner-data.json")runner.check_auth()ifjob:=runner.request_job():print("Received a new job, starting executor")my_job_executor(job)else:print("No jobs are currently available")
正在执行作业
最小CI执行器的运行方式如下:
fromgitlab_runner_apiimportfailure_reasonsjob.log+=f"Starting job with id {job.id} for branch {job.ref}\n"do_clone_and_checkout(job.repo_url,job.commit_sha)success=run_tests(job)ifsuccess:job.log+="All tests ran successfully\n"job.set_success()else:# ANSI formatting codes can be used to enhance the CI logsjob.log+="\u001b[31mJob failed!!!\u001b[0m\n"job.set_failed(failure_reasons.ScriptFailure())
有关可用属性的完整列表,请参阅参考文档Job。在
持续工作
对于长时间运行的作业,可能需要在调用之间持久化作业对象。 这可以使用类似于Python标准库中的pickle模块的接口来完成:
job_data=job.dumps()fromgitlab_runner_apiimportJobjob=Job.loads(job_data)
注意:作业日志包含在持久化数据中,因此在不丢失日志消息的情况下,Job对象不能被持久化一次并加载多次。在
- 项目
标签: