2019-04-06 19:32:14 +00:00
|
|
|
package deploy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"os"
|
|
|
|
"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"
|
2021-10-02 22:27:43 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
|
2019-04-06 19:32:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Command exports the deploy command.
|
2021-10-27 19:03:14 +00:00
|
|
|
var Command = &cli.Command{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "deploy",
|
|
|
|
Usage: "deploy code",
|
|
|
|
ArgsUsage: "<repo/name> <build> <environment>",
|
|
|
|
Action: deploy,
|
2021-10-27 19:03:14 +00:00
|
|
|
Flags: append(common.GlobalFlags,
|
|
|
|
common.FormatFlag(tmplDeployInfo),
|
|
|
|
&cli.StringFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "branch",
|
|
|
|
Usage: "branch filter",
|
|
|
|
Value: "master",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.StringFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "event",
|
|
|
|
Usage: "event filter",
|
2021-10-02 22:27:43 +00:00
|
|
|
Value: woodpecker.EventPush,
|
2019-04-06 19:32:14 +00:00
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.StringFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "status",
|
|
|
|
Usage: "status filter",
|
2021-10-02 22:27:43 +00:00
|
|
|
Value: woodpecker.StatusSuccess,
|
2019-04-06 19:32:14 +00:00
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&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 deploy(c *cli.Context) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
branch := c.String("branch")
|
|
|
|
event := c.String("event")
|
|
|
|
status := c.String("status")
|
|
|
|
|
|
|
|
buildArg := c.Args().Get(1)
|
|
|
|
var number int
|
|
|
|
if buildArg == "last" {
|
|
|
|
// Fetch the build number from the last build
|
|
|
|
builds, berr := client.BuildList(owner, name)
|
|
|
|
if berr != nil {
|
|
|
|
return berr
|
|
|
|
}
|
|
|
|
for _, build := range builds {
|
|
|
|
if branch != "" && build.Branch != branch {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if event != "" && build.Event != event {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if status != "" && build.Status != status {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if build.Number > number {
|
|
|
|
number = build.Number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if number == 0 {
|
|
|
|
return fmt.Errorf("Cannot deploy failure build")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
number, err = strconv.Atoi(buildArg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
env := c.Args().Get(2)
|
|
|
|
if env == "" {
|
|
|
|
return fmt.Errorf("Please specify the target environment (ie production)")
|
|
|
|
}
|
|
|
|
|
|
|
|
params := internal.ParseKeyPair(c.StringSlice("param"))
|
|
|
|
|
|
|
|
deploy, err := client.Deploy(owner, name, number, env, params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.New("_").Parse(c.String("format"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tmpl.Execute(os.Stdout, deploy)
|
|
|
|
}
|
|
|
|
|
|
|
|
// template for deployment information
|
|
|
|
var tmplDeployInfo = `Number: {{ .Number }}
|
|
|
|
Status: {{ .Status }}
|
|
|
|
Commit: {{ .Commit }}
|
|
|
|
Branch: {{ .Branch }}
|
|
|
|
Ref: {{ .Ref }}
|
|
|
|
Message: {{ .Message }}
|
|
|
|
Author: {{ .Author }}
|
|
|
|
Target: {{ .Deploy }}
|
|
|
|
`
|