Check out a specific branch

Since we are just initializing a repository, fetch from a remote and
checking out a specific commit, git no longer knows anything about the
branch locally. Which causes CI job to fail if they are trying to push
the branch to another remote for example for heroku deploys.

closes gitlab-org/gitlab-runner#4097
parent 7ba2b026
......@@ -820,6 +820,42 @@ func TestBuildChangesBranchesWhenFetchingRepo(t *testing.T) {
})
}
func TestBranchAvailableLocally(t *testing.T) {
shellstest.OnEachShell(t, func(t *testing.T, shell string) {
successfulBuild, err := common.GetRemoteSuccessfulBuild()
assert.NoError(t, err)
build, cleanup := newBuild(t, successfulBuild, shell)
defer cleanup()
build.Variables = append(build.Variables, common.JobVariable{Key: "GIT_STRATEGY", Value: "fetch"})
out, err := runBuildReturningOutput(t, build)
assert.NoError(t, err)
assert.Contains(t, out, "Created fresh repository")
assert.Regexp(t, "Checking out [a-f0-9]+ as", out)
f, err := ioutil.ReadFile(filepath.Join(build.FullProjectDir(), ".git/refs/heads/master"))
require.NoError(t, err)
assert.Equal(t, build.GitInfo.Sha, strings.TrimSuffix(string(f), "\n"))
out, err = runBuildReturningOutput(t, build)
assert.NoError(t, err)
assert.Contains(t, out, "Fetching changes")
assert.Regexp(t, "Checking out [a-f0-9]+ as", out)
f, err = ioutil.ReadFile(filepath.Join(build.FullProjectDir(), ".git/refs/heads/master"))
require.NoError(t, err)
assert.Equal(t, build.GitInfo.Sha, strings.TrimSuffix(string(f), "\n"))
build.GitInfo = common.GetLFSGitInfo(build.GitInfo.RepoURL)
out, err = runBuildReturningOutput(t, build)
assert.NoError(t, err)
assert.Contains(t, out, "Fetching changes")
assert.Regexp(t, "Checking out [a-f0-9]+ as", out)
f, err = ioutil.ReadFile(filepath.Join(build.FullProjectDir(), ".git/refs/heads/add-lfs-object"))
require.NoError(t, err)
assert.Equal(t, build.GitInfo.Sha, strings.TrimSuffix(string(f), "\n"))
})
}
func TestInteractiveTerminal(t *testing.T) {
cases := []struct {
app string
......
......@@ -182,6 +182,13 @@ func (b *AbstractShell) writeRefspecFetchCmd(w ShellWriter, build *common.Build,
func (b *AbstractShell) writeCheckoutCmd(w ShellWriter, build *common.Build) {
w.Notice("Checking out %s as %s...", build.GitInfo.Sha[0:8], build.GitInfo.Ref)
w.Command("git", "checkout", "-f", "-q", build.GitInfo.Sha)
// Use `git fetch $REMOTE $BRANCH:$BRANCH` to create the branch locally. We
// need to have it available locally so users can push branches to other
// remotes.
if build.GitInfo.RefType == common.RefTypeBranch {
w.Command("git", "fetch", "origin", fmt.Sprintf("%s:%s", build.GitInfo.Ref, build.GitInfo.Ref))
}
}
func (b *AbstractShell) writeSubmoduleUpdateCmd(w ShellWriter, build *common.Build, recursive bool) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment