Jenkinsfile:git rev-parse --abbrev-ref HEAD返回HEAD

时间:2017-11-03 14:39:55

标签: git jenkins jenkins-pipeline

我是jenkins / devops的新手;我关注this example。当我在当地做(从终端):

git rev-parse --abbrev-ref HEAD

我得到当前分支的名称。但是从Jenkinsfile中,在我得到的日志中:

HEAD

在线研究了一段时间,到目前为止找不到原因。这个结果的潜在原因是什么?

其他详细信息

在我的jenkins文件中,我试图获取当前git分支的名称(触发webhook的名称),然后将其传递到'git branch'命令中,因此代码如下:

pipeline {
agent {
    label 'ubuntu'
}
stages {
    stage('check') {
    steps {
            script {
               env.GIT_BRANCH_NAME=sh(returnStdout: true, script: "git rev-parse --abbrev-ref HEAD").trim()
            }

            sh 'echo BRANCH_NAME ${GIT_BRANCH_NAME}'
            git branch: GIT_BRANCH_NAME, credentialsId: '******', url: 'https://*****/*****/*****.git'
      }
....
}

在第

sh 'echo BRANCH_NAME ${GIT_BRANCH_NAME}'

提供 HEAD

我找到了解决方法,使用 git name-rev --name-only HEAD 并将脚本代码修改为:

script {
    env.GIT_BRANCH_PATH=sh(returnStdout: true, script: "git name-rev --name-only HEAD").trim()
    env.GIT_BRANCH_NAME=GIT_BRANCH_PATH.split('remotes/origin/')[1]
}

现在我得到了正确的分支名称和步骤,但我宁愿采用不太苛刻的方式做事。

使用最佳实践实现我想要实现的目标的最佳方法是什么?

PS 我没有使用多分支管道,并且要求不使用多分支。

4 个答案:

答案 0 :(得分:8)

我可能迟到了,但可能有一种更简单的方法。确保您选择"检查特定的本地分支"在您的Git配置中的其他行为下。这将确保git检查您正在跟踪的确切分支,以及您的原始命令" git rev-parse --abbrev-ref HEAD"会很好。enter image description here

答案 1 :(得分:1)

如果您使用的是multibranch管道,则分支名称应在环境变量中以env. BRANCH_NAME的形式提供。您可以在步骤中使用sh 'printenv'来打印所有可用的环境变量

答案 2 :(得分:0)

我为这种情况找到的解决方案是:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.data.mongodb.config.MongoCredentialPropertyEditor;

@SpringBootApplication
public class Application implements BeanFactoryPostProcessor {
    private static String xmlFile;
    public static void main(String[] args) throws Exception {
        if (args.length == 0 ) {
            System.out.println("Provide path for mongodb connection file");
            return;
        }
        xmlFile = args[0];
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        if (configurableListableBeanFactory instanceof BeanDefinitionRegistry) {
            XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry)configurableListableBeanFactory);

            Resource beanFile = new FileSystemResource(Application.xmlFile);
            xmlBeanDefinitionReader.loadBeanDefinitions(beanFile);
           try {
            configurableListableBeanFactory.registerCustomEditor(Class.forName("[Lcom.mongodb.MongoCredential;"), MongoCredentialPropertyEditor.class);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        }
    }
}

此处的密钥为checkout([$class: 'GitSCM', branches: [[name: '*/' + branch]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'LocalBranch', localBranch: "**"]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'cred', url: 'git@bitbucket.org:repofolder/repo.git']]]) 。它允许检出分支而不是修订。

其来源来自here

答案 3 :(得分:0)

问题:

detached HEAD所述,如果您签出了一次提交(相对于分支),则您处于“ --abbrev-ref”状态(HEAD detached at 123abc)

为什么?

Vijay Ramaswamy返回对象名称的明确名称。当前对象是您检出的提交,而不是分支。此外,git仍然无法确定您想要哪个分支,因为您签出的提交可以轻松地存在于多个分支中。

解决方案:

我会使用suggestionhttps://www.youtube.com/watch?v=aIJU68Phi1w,但是,您也可以在脚本中对分支名称进行硬编码:

env.GIT_BRANCH_NAME='my-branch-name'

或更简单地

git branch: 'my-branch-name', credentialsId: '******', url: 'https://*****/*****/*****.git'

相关问题