如何使用带有git命令的travis合并到另一个分支?

时间:2015-12-21 22:00:32

标签: git github continuous-integration travis-ci

我正在尝试向我的devstack添加一项功能,以便在travis测试通过名为 travis 的分支时添加自动部署。在此测试通过后,我想将此travis分支合并到主分支并推送到主分支。

到目前为止,当我推送到 travis 分支时,travis运行测试并且一切都成功但我的travis.yml文件中的after_success中的git命令出现问题。 / p>

travis.yml

- "npm i -g jasmine-node"
-after_success: 
  - "git fetch"
  - "git checkout master"
  - "git merge travis"
  - "git push origin master"
 branches:
   only:
     - travis

这是travis控制台上的输出:

error: pathspec 'master' did not match any file(s) known to git.
fatal: 'travis' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

非常感谢你!

1 个答案:

答案 0 :(得分:8)

TLDR

它不起作用,因为Travis克隆存储库的方式, 分支机构在本地不存在。你需要先拉它们。

在我的travis构建脚本中,我调用这个函数,允许我拉出所有 分支机构。根据您的需要进行调整。

function create_all_branches()
{
    # Keep track of where Travis put us.
    # We are on a detached head, and we need to be able to go back to it.
    local build_head=$(git rev-parse HEAD)

    # Fetch all the remote branches. Travis clones with `--depth`, which
    # implies `--single-branch`, so we need to overwrite remote.origin.fetch to
    # do that.
    git config --replace-all remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
    git fetch
    # optionally, we can also fetch the tags
    git fetch --tags

    # create the tacking branches
    for branch in $(git branch -r|grep -v HEAD) ; do
        git checkout -qf ${branch#origin/}
    done

    # finally, go back to where we were at the beginning
    git checkout ${build_head}
}

解释

Travis如何克隆存储库

我们可以在Travis日志中看到哪些命令在克隆存储库时运行。对于常规分支和拉取请求,它略有不同。

对于拉取请求:

# Clone the repository (note the --depth option) in ./user/repo
git clone --depth=50 https://github.com/user/repo.git user/repo

# Go the repository
cd user/repo

# Fetch the reference to the pull request 
git fetch origin +refs/pull/22/merge:

# Checkout the HEAD of the reference we just fetched. In other words,
# checkout the last commit of the PR. For details about FETCH_HEAD see
# https://stackoverflow.com/a/9237511/1836144
git checkout -qf FETCH_HEAD

对于常规分支(在此示例中称为mybranch):

# Clone the repository (note the --depth option) in ./user/repo
# This time, we also have the --branch option
git clone --depth=50 branch=mybranch https://github.com/user/repo.git user/repo

# Go the repository
cd user/repo

# Checkout  the HEAD of the branch we just fetched
git checkout -qf 7f15290cc343249217a9b3669975705a3dc5bd44

在这两种情况下,克隆存储库时都会使用--depth选项,这意味着--single-branch。以下是git关于--single-branch的说法:

  

仅克隆导致单个分支的提示的历史记录,由--branch选项或主分支远程的HEAD指向。进一步提取到生成的存储库只会更新分支的远程跟踪分支,此选项用于初始克隆。如果--single-branch clone创建时,远程的HEAD没有指向任何分支,则不会创建远程跟踪分支。

换句话说,只获取了一个远程分支。更糟糕的是,git fetch不会 甚至取其他分支。

如何拉动所有远程分支

This answer解释了如何让git fetch再次发挥作用:

git config --replace-all remote.origin.fetch +refs/heads/*:refs/remotes/origin/*

现在,git fetch应该获取所有远程分支,但我们仍然没有完成:我们希望创建远程跟踪分支。为此,我们可以为刚刚提取的每个分支执行git checkout

for branch in $(git branch -r|grep -v HEAD) ; do
    git checkout ${branch#origin/}
done
相关问题