Exemplo n.º 1
0
// writeBuildScript is a helper function that
// will generate the build script file in the builder's
// temp directory to be added to the Image.
func (b *Builder) writeBuildScript(dir string) error {
	f := buildfile.New()

	// add environment variables for user env
	f.WriteEnv("LANG", "en_US.UTF-8")
	f.WriteEnv("LANGUAGE", "en_US:en")
	f.WriteEnv("TERM", "xterm")
	f.WriteEnv("GOPATH", "/var/cache/drone")
	f.WriteEnv("SHELL", "/bin/bash")

	// add environment variables about the build
	f.WriteEnv("CI", "true")
	f.WriteEnv("DRONE", "true")
	f.WriteEnv("DRONE_REMOTE", b.Repo.Path)
	f.WriteEnv("DRONE_BRANCH", b.Repo.Branch)
	f.WriteEnv("DRONE_COMMIT", b.Repo.Commit)
	f.WriteEnv("DRONE_PR", b.Repo.PR)
	f.WriteEnv("DRONE_BUILD_DIR", b.Repo.Dir)

	// add environment variables for code coverage
	// systems, like coveralls.
	f.WriteEnv("CI_NAME", "DRONE")
	f.WriteEnv("CI_BUILD_NUMBER", b.Repo.Commit)
	f.WriteEnv("CI_BUILD_URL", "")
	f.WriteEnv("CI_REMOTE", b.Repo.Path)
	f.WriteEnv("CI_BRANCH", b.Repo.Branch)
	f.WriteEnv("CI_PULL_REQUEST", b.Repo.PR)

	// add /etc/hosts entries
	for _, mapping := range b.Build.Hosts {
		f.WriteHost(mapping)
	}

	if len(b.Key) != 0 {
		f.WriteFile("$HOME/.ssh/id_rsa", b.Key, 600)
	}

	// if the repository is remote then we should
	// add the commands to the build script to
	// clone the repository
	if b.Repo.IsRemote() {
		for _, cmd := range b.Repo.Commands() {
			f.WriteCmd(cmd)
		}
	}

	// if the commit is for merging a pull request
	// we should only execute the build commands,
	// and omit the deploy and publish commands.
	if len(b.Repo.PR) == 0 {
		b.Build.Write(f, b.Repo)
	} else {
		// only write the build commands
		b.Build.WriteBuild(f)
	}

	scriptfilePath := filepath.Join(dir, "drone")
	return ioutil.WriteFile(scriptfilePath, f.Bytes(), 0700)
}
Exemplo n.º 2
0
func setUpWithBash(input string) (string, error) {
	var buildStruct buildWithBash
	err := yaml.Unmarshal([]byte(input), &buildStruct)
	if err != nil {
		return "", err
	}
	bf := buildfile.New()
	buildStruct.Deploy.Write(bf, nil)
	return bf.String(), err
}
Exemplo n.º 3
0
func setUpWithDrone(input string) (string, error) {
	var buildStruct PublishToDrone
	err := yaml.Unmarshal([]byte(input), &buildStruct)
	if err != nil {
		return "", err
	}
	bf := buildfile.New()
	buildStruct.Publish.Write(bf, &repo.Repo{Name: "name"})
	return bf.String(), err
}
Exemplo n.º 4
0
func setUpWithCatapult(input string) (string, error) {
	var buildStruct PublishToCatapult
	err := yaml.Unmarshal([]byte(input), &buildStruct)
	if err != nil {
		return "", err
	}
	bf := buildfile.New()
	buildStruct.Publish.Write(bf, &repo.Repo{Name: "github.com/Clever/repo-name", Branch: "master", Commit: "1234566789abcdef"})
	return bf.String(), err
}
Exemplo n.º 5
0
func TestWriteBuildScript(t *testing.T) {
	// temporary directory to store file
	dir, _ := ioutil.TempDir("", "drone-test-")
	defer os.RemoveAll(dir)

	b := Builder{}
	b.Build = &script.Build{
		Hosts: []string{"127.0.0.1"}}
	b.Key = []byte("ssh-rsa AAA...")
	b.Repo = &repo.Repo{
		Path:   "git://github.com/drone/drone.git",
		Branch: "master",
		Commit: "e7e046b35",
		PR:     "123",
		Dir:    "/var/cache/drone/github.com/drone/drone"}
	b.writeBuildScript(dir)

	// persist a dummy build script to disk
	script, err := ioutil.ReadFile(filepath.Join(dir, "drone"))
	if err != nil {
		t.Errorf("Expected id_rsa file saved to disk")
	}

	f := buildfile.New()
	f.WriteEnv("LANG", "en_US.UTF-8")
	f.WriteEnv("LANGUAGE", "en_US:en")
	f.WriteEnv("TERM", "xterm")
	f.WriteEnv("GOPATH", "/var/cache/drone")
	f.WriteEnv("SHELL", "/bin/bash")
	f.WriteEnv("CI", "true")
	f.WriteEnv("DRONE", "true")
	f.WriteEnv("DRONE_REMOTE", "git://github.com/drone/drone.git")
	f.WriteEnv("DRONE_BRANCH", "master")
	f.WriteEnv("DRONE_COMMIT", "e7e046b35")
	f.WriteEnv("DRONE_PR", "123")
	f.WriteEnv("DRONE_BUILD_DIR", "/var/cache/drone/github.com/drone/drone")
	f.WriteEnv("CI_NAME", "DRONE")
	f.WriteEnv("CI_BUILD_NUMBER", "e7e046b35")
	f.WriteEnv("CI_BUILD_URL", "")
	f.WriteEnv("CI_REMOTE", "git://github.com/drone/drone.git")
	f.WriteEnv("CI_BRANCH", "master")
	f.WriteEnv("CI_PULL_REQUEST", "123")
	f.WriteHost("127.0.0.1")
	f.WriteFile("$HOME/.ssh/id_rsa", []byte("ssh-rsa AAA..."), 600)
	f.WriteCmd("git clone --depth=0 --recursive git://github.com/drone/drone.git /var/cache/drone/github.com/drone/drone")
	f.WriteCmd("git fetch origin +refs/pull/123/head:refs/remotes/origin/pr/123")
	f.WriteCmd("git checkout -qf -b pr/123 origin/pr/123")

	if string(script) != f.String() {
		t.Errorf("Expected build script value saved as %s, got %s", f.String(), script)
	}
}