bifangback

python-gitlab库操作gitlab的API

2021-01-06  本文已影响0人  万州客

看看不能不能直接操作gitlab的CI/CD功能。也就是说,pipeline功能开始,但不触发。使用trigger触发pipeline功能,完美~

一,安装python-gitlab

pip install --upgrade python-gitlab

二,连接认证

import gitlab

git_url = 'http://gitlab.demo.com.cn'
git_private_token = 'xxxxxxxxx-xxx
gl = gitlab.Gitlab(git_url, private_token=git_private_token)

三,获取一个project的所有信息

project_id = 1234
project = gl.projects.get(project_id)

print(project)
print("project.attributes['id'] ", project.attributes['id'])
print("project.attributes['id'] ", project.id)
print("project.attributes['name'] ", project.attributes['name'])
print("project.attributes['name'] ", project.name)
print("project.attributes['description'] ", project.attributes['description'])
print("project.attributes['name_with_namespace'] ", project.name_with_namespace)
print("project.attributes['path'] ", project.path)
print("project.attributes['path_with_namespace'] ", project.path_with_namespace)
print("project.attributes['created_at'] ", project.created_at)
print("project.attributes['default_branch'] ", project.default_branch)
print("project.attributes['tag_list'] ", project.tag_list)
print("project.attributes['ssh_url_to_repo'] ", project.ssh_url_to_repo)
print("project.attributes['http_url_to_repo'] ", project.http_url_to_repo)
print("project.attributes['web_url'] ", project.web_url)
print("project.attributes['readme_url'] ", project.readme_url)
print("project.attributes['avatar_url'] ", project.avatar_url)
print("project.attributes['star_count'] ", project.star_count)
print("project.attributes['forks_count'] ", project.forks_count)
print("project.attributes['last_activity_at'] ", project.last_activity_at)
print("project.attributes['namespace'] ", project.namespace)
print("project.attributes['_links'] ", project._links)
print("project.attributes['issues'] ", project.issues)
print("project.attributes['labels'] ", project.labels)
print("project.attributes['events'] ", project.events)
print("project.attributes['members'] ", project.members)
print("project.attributes['empty_repo'] ", project.empty_repo)
print("project.attributes['archived'] ", project.archived)
print("project.attributes['visibility'] ", project.visibility)
print("project.attributes['resolve_outdated_diff_discussions'] ", project.resolve_outdated_diff_discussions)
print("project.attributes['container_registry_enabled'] ", project.container_registry_enabled)
print("project.attributes['container_expiration_policy'] ", project.container_expiration_policy)
print("project.attributes['issues_enabled'] ", project.issues_enabled)
print("project.attributes['merge_requests_enabled'] ", project.merge_requests_enabled)
print("project.attributes['wiki_enabled'] ", project.wiki_enabled)
print("project.attributes['jobs_enabled'] ", project.jobs_enabled)
print("project.attributes['snippets_enabled'] ", project.snippets_enabled)
print("project.attributes['can_create_merge_request_in'] ", project.can_create_merge_request_in)
print("project.attributes['issues_access_level'] ", project.issues_access_level)
print("project.attributes['repository_access_level'] ", project.repository_access_level)
print("project.attributes['merge_requests_access_level'] ", project.merge_requests_access_level)
print("project.attributes['wiki_access_level'] ", project.wiki_access_level)
print("project.attributes['builds_access_level'] ", project.builds_access_level)
print("project.attributes['snippets_access_level'] ", project.snippets_access_level)
print("project.attributes['pages_access_level'] ", project.pages_access_level)
print("project.attributes['emails_disabled'] ", project.emails_disabled)
print("project.attributes['shared_runners_enabled'] ", project.shared_runners_enabled)
print("project.attributes['lfs_enabled'] ", project.lfs_enabled)
print("project.attributes['creator_id'] ", project.creator_id)
print("project.attributes['import_status'] ", project.import_status)
print("project.attributes['import_error'] ", project.import_error)
print("project.attributes['open_issues_count'] ", project.open_issues_count)
print("project.attributes['runners_token'] ", project.runners_token)
print("project.attributes['ci_default_git_depth'] ", project.ci_default_git_depth)
print("project.attributes['public_jobs'] ", project.public_jobs)
print("project.attributes['build_git_strategy'] ", project.build_git_strategy)
print("project.attributes['build_timeout'] ", project.build_timeout)
print("project.attributes['auto_cancel_pending_pipelines'] ", project.auto_cancel_pending_pipelines)
print("project.attributes['build_coverage_regex'] ", project.build_coverage_regex)
print("project.attributes['ci_config_path'] ", project.ci_config_path)
print("project.attributes['shared_with_groups'] ", project.shared_with_groups)
print("project.attributes['only_allow_merge_if_pipeline_succeeds'] ", project.only_allow_merge_if_pipeline_succeeds)
print("project.attributes['request_access_enabled'] ", project.request_access_enabled)
print("project.attributes['only_allow_merge_if_all_discussions_are_resolved'] ", project.only_allow_merge_if_all_discussions_are_resolved)
print("project.attributes['remove_source_branch_after_merge'] ", project.remove_source_branch_after_merge)
print("project.attributes['project'] ", project.printing_merge_request_link_enabled)
print("project.attributes['merge_method'] ", project.merge_method)
print("project.attributes['suggestion_commit_message'] ", project.suggestion_commit_message)
print("project.attributes['auto_devops_enabled'] ", project.auto_devops_enabled)
print("project.attributes['auto_devops_deploy_strategy'] ", project.auto_devops_deploy_strategy)
print("project.attributes['autoclose_referenced_issues'] ", project.autoclose_referenced_issues)
print("project.attributes['permissions'] ", project.permissions)

四,可能操作pipine trigger的api

def get_or_create_trigger(project):
    trigger_decription = 'my_trigger_id'
    for t in project.triggers.list():
        if t.description == trigger_decription:
            return t
    return project.triggers.create({'description': trigger_decription})

trigger = get_or_create_trigger(project)
pipeline = project.trigger_pipeline('master', trigger.token, variables={"DEPLOY_ZONE": "us-west1"})
while pipeline.finished_at is None:
    pipeline.refresh()
    time.sleep(1)

五,第三方库的官网文档

https://python-gitlab.readthedocs.io/en/stable/index.html

上一篇下一篇

猜你喜欢

热点阅读