github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/build/script/script.go (about) 1 package script 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "strings" 8 9 "launchpad.net/goyaml" 10 11 "github.com/drone/drone/pkg/build/buildfile" 12 "github.com/drone/drone/pkg/build/git" 13 "github.com/drone/drone/pkg/build/repo" 14 "github.com/drone/drone/pkg/plugin/deploy" 15 "github.com/drone/drone/pkg/plugin/notify" 16 "github.com/drone/drone/pkg/plugin/publish" 17 ) 18 19 func ParseBuild(data []byte, params map[string]string) (*Build, error) { 20 build := Build{} 21 22 // parse the build configuration file 23 err := goyaml.Unmarshal(injectParams(data, params), &build) 24 return &build, err 25 } 26 27 func ParseBuildFile(filename string) (*Build, error) { 28 data, err := ioutil.ReadFile(filename) 29 if err != nil { 30 return nil, err 31 } 32 33 return ParseBuild(data, nil) 34 } 35 36 // injectParams injects params into data. 37 func injectParams(data []byte, params map[string]string) []byte { 38 for k, v := range params { 39 data = bytes.Replace(data, []byte(fmt.Sprintf("{{%s}}", k)), []byte(v), -1) 40 } 41 return data 42 } 43 44 // Build stores the configuration details for 45 // building, testing and deploying code. 46 type Build struct { 47 // Image specifies the Docker Image that will be 48 // used to virtualize the Build process. 49 Image string 50 51 // Name specifies a user-defined label used 52 // to identify the build. 53 Name string 54 55 // Script specifies the build and test commands. 56 Script []string 57 58 // Env specifies the environment of the build. 59 Env []string 60 61 // Hosts specifies the custom IP address and 62 // hostname mappings. 63 Hosts []string 64 65 // Cache lists a set of directories that should 66 // persisted between builds. 67 Cache []string 68 69 // Services specifies external services, such as 70 // database or messaging queues, that should be 71 // linked to the build environment. 72 Services []string 73 74 Deploy *deploy.Deploy `yaml:"deploy,omitempty"` 75 Publish *publish.Publish `yaml:"publish,omitempty"` 76 Notifications *notify.Notification `yaml:"notify,omitempty"` 77 78 // Git specified git-specific parameters, such as 79 // the clone depth and path 80 Git *git.Git `yaml:"git,omitempty"` 81 } 82 83 // Write adds all the steps to the build script, including 84 // build commands, deploy and publish commands. 85 func (b *Build) Write(f *buildfile.Buildfile, r *repo.Repo) { 86 // append build commands 87 b.WriteBuild(f) 88 89 // write publish commands 90 if b.Publish != nil { 91 b.Publish.Write(f, r) 92 } 93 94 // write deployment commands 95 if b.Deploy != nil { 96 b.Deploy.Write(f) 97 } 98 99 // write exit value 100 f.WriteCmd("exit 0") 101 } 102 103 // WriteBuild adds only the build steps to the build script, 104 // omitting publish and deploy steps. This is important for 105 // pull requests, where deployment would be undesirable. 106 func (b *Build) WriteBuild(f *buildfile.Buildfile) { 107 // append environment variables 108 for _, env := range b.Env { 109 parts := strings.Split(env, "=") 110 if len(parts) != 2 { 111 continue 112 } 113 f.WriteEnv(parts[0], parts[1]) 114 } 115 116 // append build commands 117 for _, cmd := range b.Script { 118 f.WriteCmd(cmd) 119 } 120 } 121 122 type Publish interface { 123 Write(f *buildfile.Buildfile) 124 } 125 126 type Deployment interface { 127 Write(f *buildfile.Buildfile) 128 } 129 130 type Notification interface { 131 Set(c Context) 132 } 133 134 type Context interface { 135 Host() string 136 Owner() string 137 Name() string 138 139 Branch() string 140 Hash() string 141 Status() string 142 Message() string 143 Author() string 144 Gravatar() string 145 146 Duration() int64 147 HumanDuration() string 148 149 //Settings 150 }