我正在尝试向我的devstack添加一个功能,以便在travis测试通过名为travis的分支时添加自动部署.在此测试通过后,我想将此travis分支合并到主分支并推送到主分支.
到目前为止,当我推送travis分支时,travis运行测试并且一切都成功但我after_success
在我的travis.yml文件中遇到了我的git命令问题.
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.
非常感谢!
它不起作用,因为由于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日志中看到哪些命令在克隆存储库时运行.对于常规分支和拉取请求,它略有不同.
对于拉动请求:
# 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
# /sf/ask/17360801/
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指向.进一步提取到生成的存储库只会更新分支的远程跟踪分支,此选项用于初始克隆.如果在创建单个分支克隆时远程处的HEAD未指向任何分支,则不会创建远程跟踪分支.
换句话说,只获取了一个远程分支.更糟糕的是,git fetch
甚至不会取其他分支机构.
这个答案解释了如何重新开始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