2014-02-07 10:10:01 +00:00
|
|
|
package script
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
|
2014-06-12 00:42:49 +00:00
|
|
|
"gopkg.in/yaml.v1"
|
2014-02-07 10:10:01 +00:00
|
|
|
|
2014-06-12 21:46:55 +00:00
|
|
|
"github.com/drone/drone/plugin/deploy"
|
|
|
|
"github.com/drone/drone/plugin/notify"
|
|
|
|
"github.com/drone/drone/plugin/publish"
|
2014-06-04 21:25:38 +00:00
|
|
|
"github.com/drone/drone/shared/build/buildfile"
|
|
|
|
"github.com/drone/drone/shared/build/git"
|
|
|
|
"github.com/drone/drone/shared/build/repo"
|
2014-02-07 10:10:01 +00:00
|
|
|
)
|
|
|
|
|
2014-10-12 20:02:53 +00:00
|
|
|
func ParseBuild(data string) (*Build, error) {
|
2014-02-07 10:10:01 +00:00
|
|
|
build := Build{}
|
|
|
|
|
|
|
|
// parse the build configuration file
|
2014-10-12 20:02:53 +00:00
|
|
|
err := yaml.Unmarshal([]byte(data), &build)
|
2014-02-07 10:10:01 +00:00
|
|
|
return &build, err
|
|
|
|
}
|
|
|
|
|
2014-12-07 16:26:43 +00:00
|
|
|
func ParseBuildFile(filename string, params map[string]string) (*Build, error) {
|
2014-02-07 10:10:01 +00:00
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-07 16:26:43 +00:00
|
|
|
return ParseBuild(Inject(string(data), params))
|
2014-02-07 10:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build stores the configuration details for
|
|
|
|
// building, testing and deploying code.
|
|
|
|
type Build struct {
|
|
|
|
// Image specifies the Docker Image that will be
|
|
|
|
// used to virtualize the Build process.
|
|
|
|
Image string
|
|
|
|
|
|
|
|
// Name specifies a user-defined label used
|
|
|
|
// to identify the build.
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Script specifies the build and test commands.
|
|
|
|
Script []string
|
|
|
|
|
|
|
|
// Env specifies the environment of the build.
|
|
|
|
Env []string
|
|
|
|
|
2014-03-03 02:55:28 +00:00
|
|
|
// Hosts specifies the custom IP address and
|
|
|
|
// hostname mappings.
|
|
|
|
Hosts []string
|
|
|
|
|
2014-03-03 06:35:01 +00:00
|
|
|
// Cache lists a set of directories that should
|
|
|
|
// persisted between builds.
|
|
|
|
Cache []string
|
|
|
|
|
2014-02-07 10:10:01 +00:00
|
|
|
// Services specifies external services, such as
|
|
|
|
// database or messaging queues, that should be
|
|
|
|
// linked to the build environment.
|
|
|
|
Services []string
|
|
|
|
|
2014-07-18 05:49:11 +00:00
|
|
|
// White-list of Branches that are built.
|
|
|
|
Branches []string
|
|
|
|
|
2014-02-11 22:17:35 +00:00
|
|
|
Deploy *deploy.Deploy `yaml:"deploy,omitempty"`
|
|
|
|
Publish *publish.Publish `yaml:"publish,omitempty"`
|
2014-08-14 04:37:56 +00:00
|
|
|
Notifications *notify.Notification `yaml:"notify,omitempty"`
|
2014-03-03 02:55:28 +00:00
|
|
|
|
|
|
|
// Git specified git-specific parameters, such as
|
|
|
|
// the clone depth and path
|
|
|
|
Git *git.Git `yaml:"git,omitempty"`
|
2014-10-15 09:24:20 +00:00
|
|
|
|
|
|
|
// Docker container parameters, such as
|
|
|
|
// NetworkMode and UserName
|
|
|
|
Docker *Docker `yaml:"docker,omitempty"`
|
2014-02-07 10:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write adds all the steps to the build script, including
|
|
|
|
// build commands, deploy and publish commands.
|
2014-04-29 02:28:07 +00:00
|
|
|
func (b *Build) Write(f *buildfile.Buildfile, r *repo.Repo) {
|
2014-02-07 10:10:01 +00:00
|
|
|
// append build commands
|
|
|
|
b.WriteBuild(f)
|
|
|
|
|
|
|
|
// write publish commands
|
|
|
|
if b.Publish != nil {
|
2014-04-29 02:28:07 +00:00
|
|
|
b.Publish.Write(f, r)
|
2014-02-07 10:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write deployment commands
|
|
|
|
if b.Deploy != nil {
|
2014-09-08 06:08:35 +00:00
|
|
|
b.Deploy.Write(f, r)
|
2014-02-07 10:10:01 +00:00
|
|
|
}
|
2014-02-26 21:24:48 +00:00
|
|
|
|
|
|
|
// write exit value
|
|
|
|
f.WriteCmd("exit 0")
|
2014-02-07 10:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteBuild adds only the build steps to the build script,
|
|
|
|
// omitting publish and deploy steps. This is important for
|
|
|
|
// pull requests, where deployment would be undesirable.
|
|
|
|
func (b *Build) WriteBuild(f *buildfile.Buildfile) {
|
|
|
|
// append environment variables
|
|
|
|
for _, env := range b.Env {
|
2014-11-01 18:28:59 +00:00
|
|
|
parts := strings.SplitN(env, "=", 2)
|
2014-02-07 10:10:01 +00:00
|
|
|
if len(parts) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
f.WriteEnv(parts[0], parts[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// append build commands
|
|
|
|
for _, cmd := range b.Script {
|
|
|
|
f.WriteCmd(cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 05:49:11 +00:00
|
|
|
func (b *Build) MatchBranch(branch string) bool {
|
|
|
|
if len(b.Branches) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, item := range b.Branches {
|
|
|
|
if item == branch {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-02-07 10:10:01 +00:00
|
|
|
type Publish interface {
|
|
|
|
Write(f *buildfile.Buildfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Deployment interface {
|
|
|
|
Write(f *buildfile.Buildfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Notification interface {
|
|
|
|
Set(c Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Context interface {
|
|
|
|
Host() string
|
|
|
|
Owner() string
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
Branch() string
|
|
|
|
Hash() string
|
|
|
|
Status() string
|
|
|
|
Message() string
|
|
|
|
Author() string
|
|
|
|
Gravatar() string
|
|
|
|
|
|
|
|
Duration() int64
|
|
|
|
HumanDuration() string
|
|
|
|
|
|
|
|
//Settings
|
|
|
|
}
|