简体   繁体   English

使用引用git分支的Jenkins管道在openshift上构建

[英]Build on openshift with a Jenkins pipeline referring to a git branch

I have the following structure: 我有以下结构:

  • a git(gitlab) repository where I have 2 branch (dev and master) (more are there but I'm interested only in this two for the build) 一个git(gitlab)仓库,其中有2个分支(dev和master)(还有更多分支,但是我只对这两个分支感兴趣)
  • configurated gitLab that makes a webhook when push/merge action are maded on this project. 在该项目上执行推/合并操作时,配置了gitLab的webhook。 The webhook points on a pipeline Webhook指向管道
  • a openshift container that has a pod with jenkins installed 一个openshift容器,其中装有装有jenkins的吊舱
  • 2 pipeline defined in the openshift jenkins project, one to build the dev branch and one to build the master branch. 在openshift jenkins项目中定义了2条管道,一条用于构建dev分支,另一条用于构建master分支。 The pieline are written in groovy, and made some git, maven and oc commands Pieline是用groovy编写的,并做了一些git,maven和oc命令

Versions: 版本:

  • gitlab 10.1 gitlab 10.1
  • openshift 3.4 Openshift 3.4
  • Jenkins 2.19.3 詹金斯2.19.3

What I'm able to do: make a push on dev/master that start the 2 pipelines and builds the two branches. 我能做的是:推动dev / master来启动2个管道并建立两个分支。 There starts two build because I don't know how to filter out that a push/merge event is made on dev or master. 因为我不知道如何过滤在dev或master上进行的push / merge事件,所以开始两个构建。 I want to do this because: 我想要这样做是因为:

  • on the build there are some integration tests that use a DB and If the pipeline starts simultaneously then they make some disaster on the test-db 在构建中,有一些使用DB的集成测试,如果管道同时启动,那么它们会对test-db造成灾难
  • and there is no reason that the two pipelines starts, when the branch that they represent are not involved. 当它们所代表的分支不涉及时,没有理由启动两个管道。 Only the pipeline with the correct branch has to start 只有具有正确分支的管道才必须启动

I saw two option to resolve this. 我看到了两种解决方案。 On Gitlab there are no possibility to drive the webhook event on a defined branch. 在Gitlab上,不可能在已定义的分支上驱动webhook事件。 (or is there and I don't know how?) On the pipeline I could write some code to check on witch branch there is made a push/merge. (或者在那里,我不知道如何?)在管道上,我可以编写一些代码来检查女巫分支,是否进行了推/合并。 But I don't know how exactly make this check. 但是我不知道如何进行这项检查。

Are there other options? 还有其他选择吗? If possible I don't want to change the workflow, but I'm open to hear any good Idea. 如果可能的话,我不想更改工作流程,但是我愿意听到任何好的建议。

If I'm missing some relevant information please ask me. 如果我缺少一些相关信息,请询问我。 I don't publish any code because the context are very big, and principally I only need the right direction or some ideas how to solve this. 我不发布任何代码,因为上下文非常大,原则上我只需要正确的方向或一些解决方案。

So At the end I found a way by myself. 所以最后我自己找到了一种方法。

I think it's not elegant but at the moment it solves my problem. 我认为它并不优雅,但目前可以解决我的问题。

All I do is to check with git in the pipeline the hash of the last Merge and the last hash of the branch in this way: 我要做的就是以这种方式在管道中使用git检查最后一个Merge和分支的最后一个哈希:

def lastMerge = sh(returnStdout: true, script: 'git show :/^Merge --pretty=format:"%H"').trim()  
def hash = sh(returnStdout: true, script: 'git log -1 origin/master --pretty=format:"%H"').trim() 
if(lastMerge==hash){
    doTheJob()
}

If there are better solutions, I'm open to hear it. 如果有更好的解决方案,我很乐意听。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM