在bifang项目中,用得着的。就是参数有点多,7个。后期再想办法优化。
一,函数
import gitlab
import time
def gitlab_trigger(git_url, git_access_token,
project_id, app_name, release,
git_branch, git_trigger_token):
git_url = git_url
git_access_token = git_access_token
gl = gitlab.Gitlab(git_url, private_token=git_access_token)
project = gl.projects.get(project_id)
pipeline = project.trigger_pipeline(git_branch,
git_trigger_token,
variables={"RELEASE": release,
'APP_NAME': app_name})
while pipeline.finished_at is None:
pipeline.refresh()
time.sleep(1)
return pipeline
- 直到调用完成之前,每隔1秒获取一下结果
二,调用
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bifangback.settings")
django.setup()
from cmdb.models import App
from utils.gitlab import gitlab_trigger
app = App.objects.get(name='go-demo')
git_url = app.git.git_url
git_access_token = app.git.git_token
git_trigger_token = app.git_trigger_token
project_id = app.git_app_id
app_name = app.name
release = '202101090245XF'
git_branch = 'master'
job_pipeline = gitlab_trigger(git_url, git_access_token,
project_id, app_name, release,
git_branch, git_trigger_token)
print(job_pipeline)
print(job_pipeline.id)
print(job_pipeline.status)
print(job_pipeline.ref)
print(job_pipeline.web_url)
print(job_pipeline.duration)
- 根据下面的输出,使用点(.)获取更具体的信息
三,输出
<class 'gitlab.v4.objects.ProjectPipeline'> =>
{
'id': 20,
'sha': 'c76966f59b74e6f22f83130dab408284d360906d',
'ref': 'master',
'status': 'success',
'created_at': '2021-01-11T14:33:12.360Z',
'updated_at': '2021-01-11T14:33:43.337Z',
'web_url': 'http://192.168.1.211:8180/demo/go-demo/-/pipelines/20',
'before_sha': '0000000000000000000000000000000000000000',
'tag': False,
'yaml_errors': None,
'user': {'id': 1, 'name': 'Administrator', 'username': 'root', 'state': 'active', 'avatar_url': 'https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon', 'web_url': 'http://192.168.1.211:8180/root'},
'started_at': '2021-01-11T14:33:19.940Z',
'finished_at': '2021-01-11T14:33:43.322Z',
'committed_at': None,
'duration': 23,
'coverage': None,
'detailed_status': {'icon': 'status_success', 'text': 'passed', 'label': 'passed', 'group': 'success', 'tooltip': 'passed', 'has_details': False, 'details_path': '/demo/go-demo/-/pipelines/20', 'illustration': None, 'favicon': '/assets/ci_favicons/favicon_status_success-8451333011eee8ce9f2ab25dc487fe24a8758c694827a582f17f42b0a90446a2.png'}
}
20
success
master
http://192.168.1.211:8180/demo/go-demo/-/pipelines/20
23