Merge pull request #68 from yosssi/params-injection

Inject params into .drone.yml.
This commit is contained in:
Brad Rydzewski 2014-02-13 18:50:15 -08:00
commit aac473d689
3 changed files with 31 additions and 5 deletions

View file

@ -221,6 +221,22 @@ git:
depth: 1 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 ### Docs
Coming Soon to [drone.readthedocs.org](http://drone.readthedocs.org/) Coming Soon to [drone.readthedocs.org](http://drone.readthedocs.org/)

View file

@ -1,6 +1,8 @@
package script package script
import ( import (
"bytes"
"fmt"
"io/ioutil" "io/ioutil"
"strings" "strings"
@ -13,11 +15,11 @@ import (
"github.com/drone/drone/pkg/plugin/publish" "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{} build := Build{}
// parse the build configuration file // parse the build configuration file
err := goyaml.Unmarshal(data, &build) err := goyaml.Unmarshal(injectParams(data, params), &build)
return &build, err return &build, err
} }
@ -27,7 +29,15 @@ func ParseBuildFile(filename string) (*Build, error) {
return nil, err 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 // 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 // parse the build script
buildscript, err := script.ParseBuild(raw) buildscript, err := script.ParseBuild(raw, repo.Params)
if err != nil { 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" 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 { if err := saveFailedBuild(commit, msg); err != nil {
@ -233,7 +233,7 @@ func PullRequestHook(w http.ResponseWriter, r *http.Request) {
} }
// parse the build script // parse the build script
buildscript, err := script.ParseBuild(raw) buildscript, err := script.ParseBuild(raw, repo.Params)
if err != nil { if err != nil {
// TODO if the YAML is invalid we should create a commit record // 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 // with an ERROR status so that the user knows why a build wasn't