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