Inject params into .drone.yml.

This commit is contained in:
yosssi 2014-02-13 15:03:40 +00:00
parent bb90a492cf
commit 5895e364c1
3 changed files with 31 additions and 5 deletions

View file

@ -219,6 +219,22 @@ git:
depth: 1
```
### Params Injection
You can inject params into .drone.yml.
```
notify:
hipchat:
room: {{hipchatRoom}}
token: {{hipchatToken}}
on_started: true
on_success: true
on_failure: true
```
![params-injection](https://f.cloud.github.com/assets/1583973/2161187/2905077e-94c3-11e3-8499-a3844682c8af.png)
### Docs
Coming Soon to [drone.readthedocs.org](http://drone.readthedocs.org/)

View file

@ -1,6 +1,8 @@
package script
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
@ -13,11 +15,11 @@ import (
"github.com/drone/drone/pkg/plugin/publish"
)
func ParseBuild(data []byte) (*Build, error) {
func ParseBuild(data []byte, params map[string]string) (*Build, error) {
build := Build{}
// parse the build configuration file
err := goyaml.Unmarshal(data, &build)
err := goyaml.Unmarshal(injectParams(data, params), &build)
return &build, err
}
@ -27,7 +29,15 @@ func ParseBuildFile(filename string) (*Build, error) {
return nil, err
}
return ParseBuild(data)
return ParseBuild(data, nil)
}
// injectParams injects params into data.
func injectParams(data []byte, params map[string]string) []byte {
for k, v := range params {
data = bytes.Replace(data, []byte(fmt.Sprintf("{{%s}}", k)), []byte(v), -1)
}
return data
}
// Build stores the configuration details for

View file

@ -122,7 +122,7 @@ func Hook(w http.ResponseWriter, r *http.Request) error {
}
// parse the build script
buildscript, err := script.ParseBuild(raw)
buildscript, err := script.ParseBuild(raw, repo.Params)
if err != nil {
msg := "Could not parse your .drone.yml file. It needs to be a valid drone yaml file.\n\n" + err.Error() + "\n"
if err := saveFailedBuild(commit, msg); err != nil {
@ -233,7 +233,7 @@ func PullRequestHook(w http.ResponseWriter, r *http.Request) {
}
// parse the build script
buildscript, err := script.ParseBuild(raw)
buildscript, err := script.ParseBuild(raw, repo.Params)
if err != nil {
// TODO if the YAML is invalid we should create a commit record
// with an ERROR status so that the user knows why a build wasn't