Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the params injection function to use mustanche and drone-cli to use this function. #73

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fixed the params injections function to use mustanche and drone-cli to
use this function.
  • Loading branch information
yosssi committed Feb 14, 2014
commit 8c6ab0053adb8420b7147b2c14131efda4b472cd
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ git:
depth: 1
```

### Params Injection
### Parameters Injection

You can inject params into .drone.yml.
You can inject parameters into .drone.yml.

```
notify:
Expand All @@ -233,8 +233,16 @@ notify:
on_failure: true
```

You can set the parameters via Drone web app:

![params-injection](https://f.cloud.github.com/assets/1583973/2161187/2905077e-94c3-11e3-8499-a3844682c8af.png)

You can also do via Drone cli:

```sh
$ drone -param hipchatRoom=support -param hipchatToken=3028700e5466d375 build
```

### Docs

Coming Soon to [drone.readthedocs.org](http:https://drone.readthedocs.org/)
Expand Down
71 changes: 56 additions & 15 deletions cmd/drone/drone.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"

Expand All @@ -18,6 +19,34 @@ import (
"launchpad.net/goyaml"
)

// A buildParams represents build parameters which are
// injected into the yaml configuration file.
type buildParams []string

// String returns the string value of the buildParams.
func (p *buildParams) String() string {
return fmt.Sprint(*p)
}

// Set sets the value into the buildParams.
func (p *buildParams) Set(value string) error {
for _, v := range strings.Split(value, ",") {
*p = append(*p, v)
}
return nil
}

// Map returns a map of the buildParams.
func (p *buildParams) Map() map[string]string {
m := map[string]string{}
for _, prm := range *p {
if kv := strings.SplitN(prm, "=", 2); len(kv) == 2 {
m[kv[0]] = kv[1]
}
}
return m
}

var (
// identity file (id_rsa) that will be injected
// into the container if specified
Expand All @@ -35,6 +64,9 @@ var (

// displays the help / usage if True
help = flag.Bool("h", false, "")

// build parameters
params buildParams
)

func init() {
Expand All @@ -43,6 +75,8 @@ func init() {
log.SetSuffix("\033[0m\n")
log.SetOutput(os.Stdout)
log.SetPriority(log.LOG_NOTICE)

flag.Var(&params, "param", "")
}

func main() {
Expand Down Expand Up @@ -72,7 +106,7 @@ func main() {
case args[0] == "build" && len(args) == 1:
path, _ := os.Getwd()
path = filepath.Join(path, ".drone.yml")
run(path)
run(path, params)

// run drone build where the path to the
// source directory is provided
Expand All @@ -81,7 +115,7 @@ func main() {
path = filepath.Clean(path)
path, _ = filepath.Abs(path)
path = filepath.Join(path, ".drone.yml")
run(path)
run(path, params)

// run drone vet where the path to the
// source directory is provided
Expand All @@ -90,14 +124,14 @@ func main() {
path = filepath.Clean(path)
path, _ = filepath.Abs(path)
path = filepath.Join(path, ".drone.yml")
vet(path)
vet(path, params)

// run drone vet assuming the current
// working directory contains the drone.yml
case args[0] == "vet" && len(args) == 1:
path, _ := os.Getwd()
path = filepath.Join(path, ".drone.yml")
vet(path)
vet(path, params)

// print the help message
case args[0] == "help" && len(args) == 1:
Expand All @@ -107,9 +141,9 @@ func main() {
os.Exit(0)
}

func vet(path string) {
func vet(path string, params buildParams) {
// parse the Drone yml file
script, err := script.ParseBuildFile(path)
script, err := script.ParseBuildFile(path, params.Map())
if err != nil {
log.Err(err.Error())
os.Exit(1)
Expand All @@ -121,15 +155,21 @@ func vet(path string) {
log.Noticef("parsed yaml:\n%s", string(out))
}

func run(path string) {
func run(path string, params buildParams) {
paramsMap := params.Map()
// parse the Drone yml file
s, err := script.ParseBuildFile(path)
s, err := script.ParseBuildFile(path, paramsMap)
if err != nil {
log.Err(err.Error())
os.Exit(1)
return
}

// set environment variables
for k, v := range paramsMap {
s.Env = append(s.Env, k+"="+v)
}

// get the repository root directory
dir := filepath.Dir(path)
code := repo.Repo{Path: dir}
Expand Down Expand Up @@ -270,14 +310,15 @@ Usage:

The commands are:

build build and test the repository
version print the version number
vet validate the yaml configuration file
build build and test the repository
version print the version number
vet validate the yaml configuration file

-v runs drone with verbose output
-h display this help and exit
--parallel runs drone build tasks in parallel
--timeout=300ms timeout build after 300 milliseconds
-v runs drone with verbose output
-h display this help and exit
--parallel runs drone build tasks in parallel
--timeout=300ms timeout build after 300 milliseconds
-param 'key=value' Parameter for the yaml configuration file, can be used multiple times.

Examples:
drone build builds the source in the pwd
Expand Down
19 changes: 4 additions & 15 deletions pkg/build/script/script.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package script

import (
"bytes"
"fmt"
"io/ioutil"
"strings"

Expand All @@ -13,31 +11,22 @@ import (
"github.com/drone/drone/pkg/plugin/deploy"
"github.com/drone/drone/pkg/plugin/notify"
"github.com/drone/drone/pkg/plugin/publish"
"github.com/hoisie/mustache"
)

func ParseBuild(data []byte, params map[string]string) (*Build, error) {
build := Build{}

// parse the build configuration file
err := goyaml.Unmarshal(injectParams(data, params), &build)
err := goyaml.Unmarshal([]byte(mustache.Render(string(data), params)), &build)
return &build, err
}

func ParseBuildFile(filename string) (*Build, error) {
func ParseBuildFile(filename string, params map[string]string) (*Build, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}

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
return ParseBuild(data, params)
}

// Build stores the configuration details for
Expand Down