2019-04-06 19:32:14 +00:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/common"
|
2021-09-21 14:36:41 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/internal"
|
2019-04-06 19:32:14 +00:00
|
|
|
)
|
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
var buildStartCmd = &cli.Command{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "start",
|
|
|
|
Usage: "start a build",
|
|
|
|
ArgsUsage: "<repo/name> [build]",
|
|
|
|
Action: buildStart,
|
2021-10-27 19:03:14 +00:00
|
|
|
Flags: append(common.GlobalFlags,
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "param",
|
|
|
|
Aliases: []string{"p"},
|
|
|
|
Usage: "custom parameters to be injected into the job environment. Format: KEY=value",
|
2019-04-06 19:32:14 +00:00
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
),
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildStart(c *cli.Context) (err error) {
|
|
|
|
repo := c.Args().First()
|
|
|
|
owner, name, err := internal.ParseRepo(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := internal.NewClient(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buildArg := c.Args().Get(1)
|
|
|
|
var number int
|
|
|
|
if buildArg == "last" {
|
|
|
|
// Fetch the build number from the last build
|
|
|
|
build, err := client.BuildLast(owner, name, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
number = build.Number
|
|
|
|
} else {
|
|
|
|
if len(buildArg) == 0 {
|
|
|
|
return errors.New("missing job number")
|
|
|
|
}
|
|
|
|
number, err = strconv.Atoi(buildArg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
params := internal.ParseKeyPair(c.StringSlice("param"))
|
|
|
|
|
|
|
|
build, err := client.BuildStart(owner, name, number, params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Starting build %s/%s#%d\n", owner, name, build.Number)
|
|
|
|
return nil
|
|
|
|
}
|