python 调用函数_封装一个python-gitlab的调用函数

1ef3970e2bb797ca6304859549a209c0.png

在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

c75bc252504ac7a561d8c5b602b65566.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值