在管道2.0 groovy脚本中执行git fetch [英] Perform a git fetch in pipeline 2.0 groovy script

查看:427
本文介绍了在管道2.0 groovy脚本中执行git fetch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jenkins 2.0管道脚本中有一个涉及git中包含区域的打开错误,因此这意味着要进行大型单仓库,因为在我的情况下,每次检入都将导致启动多个管道,这是不希望的行为. 因此,可视化:

There is an open bug on jenkins 2.0 pipeline scripts relating to included regions in git, so it means for a large mono-repo as in my case each checkin to master will cause multiple pipelines to be kicked off which is not the desired behavior. So to visualize:

顶级:
->应用程序文件夹1
->应用程序文件夹2

top-level:
->application folder 1
->application folder 2

我想做的是先执行git fetch,然后再执行git diff来查看特定文件夹中的任何内容是否已更改,以及是否已经运行了该特定文件夹的管道,并且在不执行任何操作的情况下没什么改变

What I want to do is to do a git fetch first so then I can do a git diff to see if anything in the particular folder has changed and if it has then run the pipeline for that particular folder and not do anything if nothing changed

我的代码如下:

node{
git credentialsId: 'cred id', url:    'ssh://git@git-repo:1234/app/mono-repo.git'
ret = sh(script: 'git fetch; git diff origin/master remotes/origin/master | grep "folder-name"', returnStatus: true)
    if(ret == 0){
        doSomething()
   }else{
        doNothing()
   }
}

我遇到的问题是,由于权限错误而导致git fetch失败,我可以使用checkout,但是我无法事先获得diff,这不是事实.有没有一种使用凭据收集git fetch的方法?

The issue I have that the git fetch fails due a permissions error, I can use a the checkout but then I cannot get the diff before hand which is not what. Is there a way of using the u tiling the git fetch using the credentias?

推荐答案

简单地获取对标记的引用可能会有所帮助.注意,我相信这不等同于git fetch --tags.参见例如包括"git fetch"?.

It might help to simply get the references to tags. Note, I believe this is not equivalent to git fetch --tags. See Does "git fetch --tags" include "git fetch"? for example.

git([branches: [
        [name: '*/master'], 
        [name: 'refs/tags/*:refs/tags/*']], 
     credentialsId: CREDENTIALS_ID_GIT,
     url: REPO])

我注意到,在Jenkin的控制台上,Git插件正在执行git fetch --tags,因此默认情况下,Git插件可能已经提供了此功能.请检查一下.

I noticed that on the Jenkin's console the Git plugin is performing a git fetch --tags, so by default the Git Plugin may already provide this functionality. Please check on this.

我还要添加此解决方案:

I'd like to also add this solution:

withCredentials(
    [usernamePassword(
        credentialsId: CREDENTIALS_ID_GIT, 
        passwordVariable: 'GIT_PASSWORD', 
        usernameVariable: 'GIT_USERNAME')]) {
        sh("git fetch --tags https://${GIT_USERNAME}:${GIT_PASSWORD}@${REPO}")
    }

这篇关于在管道2.0 groovy脚本中执行git fetch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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