如何在 Gitlab 中成功的管道结束时创建合并请求? [英] How to create a merge request at the end of a successful pipeline in Gitlab?

查看:52
本文介绍了如何在 Gitlab 中成功的管道结束时创建合并请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 gitlab 和 gitlab CI 非常陌生,我已经建立了一个成功完成的管道.我的主分支和开发分支受到保护,因此需要合并请求,以便组中的另一个开发人员可以在合并之前查看代码和评论.我想知道是否可以在此管道的末尾生成此合并请求.gitlab 存储库中是否有此设置,或者我是否必须创建一个脚本才能实现此目的?
旁注:
就在发布此之前,我遇到了 gitlab 文档的这一部分
我在 ubuntu 18.04 上使用 gitlab-runner 11.0.0

I'm very new to gitlab and gitlab CI and I have put a pipeline in place that is successfully completing. My master and development branches are protected so a merge request is required so that another dev in the group can review the code and comment before merging. I was wondering if it is possible to generate this merge request at the end of this pipeline. Is there a setting for this in the gitlab repository or do I have to create a script to achieve this?
Side note :
Just before posting this I came across this section of the gitlab docs
I'm using gitlab-runner 11.0.0 on ubuntu 18.04

推荐答案

简短回答: 当然 - 一切皆有可能.GitLab 有一个很棒的 AP​​I(包括创建一个 MR).但我认为走那条路是不好的形式.您应该按照设计使用 GitLab.您开始合并请求太晚了.在您开始任何工作之前启动它,您的合并请求将在您的分支的整个持续时间中保持打开状态.

Short Answer: Sure - anything's possible. GitLab has a great API (including creating an MR). But I think going that route is bad form. You should utilize GitLab as it's designed. You're starting your Merge Request too late. Start it before you begin any work and your Merge Request will remain open the entire duration of your branch.

长答案:这是理想的 GitLab 工作流程:

Long Answer: This is the ideal GitLab workflow:

  1. 有人针对存储库创建了ISSUE.也许是一个功能请求,也许是一个实际问题,无论什么 - 有人想要改变一些东西,所以这是一个问题"
  2. 开发者打开问题并点击CREATE MERGE REQUEST
    • 生成一个合并请求(MR),一个匹配的分支,并将其与问题联系起来
  1. Someone creates an ISSUE against a repository. Maybe a feature request, maybe an actual problem, whatever - someone wants something changed, so it's an 'issue'
  2. Developer opens the issue and clicks CREATE MERGE REQUEST
    • This generates an Merge Request (MR), a matching branch, and ties it back to the issue

这从根本上落后于 GitHub 的工作方式(我来自该方式),你没有告诉人们你在做什么.

This is fundamentally backward from the way GitHub works (which I came from) where you don't have to tell people what you're working on.

  • GitHub 上的Pull Requests 是在工作完成 并且您想要合并到 master 时创建的.
  • GitLab 上的
  • 合并请求是在工作开始并且您想告诉全世界您即将开始工作时创建的在一个特征上.这允许人们在不需要时快速关闭,或防止多个开发人员重复工作.
  • Pull Requests on GitHub are created when the work is finished and you want to merge into master.
  • Merge Requests on GitLab are created when the work is beginning and you want to tell the world you're about to embark on working on a feature. This allows for people to do a quick shutdown if it's not needed or prevents multiple devs from duplicating effort.

听起来您对利用 API 很感兴趣.有一个名为python-gitlab"的python包实际上工作得很好http://python-gitlab.readthedocs.io/en/stable/gl_objects/mrs.html

It sounds like you're interested in leveraging the API. There's a python package called 'python-gitlab' actually that works decently http://python-gitlab.readthedocs.io/en/stable/gl_objects/mrs.html

import gitlab
import os

origin = "https://gitlab.example.com"
# Private token is set as an env var
gl = gitlab.Gitlab(origin, private_token, api_version='4')
gl.auth()

def create_merge_request(origin, private_token):
    mr = project.mergerequests.create({'source_branch': 'cool_feature',
                               'target_branch': 'master',
                               'title': 'merge cool feature',
                               'labels': ['label1', 'label2']})
    mr.assignee_id = gl.users.get(2).id # Assign it to coworker

def lookup_last_pipeline(origin, private_token):
    current_pipeline_id = os.environ['CI_PIPELINE_ID']
    pipelines = gl.projects.get(os.environ['CI_PROJECT_ID']).pipelines.list()
    for pipeline in pipelines:
        if pipeline.status == 'success' and pipeline.id == current_pipeline_id:
            create_merge_request()

这当然是一个示例,您必须根据自己的具体需求对其进行调整.

This is of course an example, you'll have to adapt it to your precise needs.

这篇关于如何在 Gitlab 中成功的管道结束时创建合并请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆