2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-04-06 19:32:14 +00:00
|
|
|
package deploy
|
|
|
|
|
|
|
|
import (
|
2024-07-17 23:26:35 +00:00
|
|
|
"context"
|
2019-04-06 19:32:14 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
2024-07-17 23:26:35 +00:00
|
|
|
"github.com/urfave/cli/v3"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/cli/common"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/cli/internal"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/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",
|
2024-07-03 13:58:51 +00:00
|
|
|
Usage: "trigger a pipeline with the 'deployment' event",
|
2023-06-12 23:07:52 +00:00
|
|
|
ArgsUsage: "<repo-id|repo-full-name> <pipeline> <environment>",
|
2019-04-06 19:32:14 +00:00
|
|
|
Action: deploy,
|
2023-12-08 08:36:53 +00:00
|
|
|
Flags: []cli.Flag{
|
2021-10-27 19:03:14 +00:00
|
|
|
common.FormatFlag(tmplDeployInfo),
|
|
|
|
&cli.StringFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "branch",
|
|
|
|
Usage: "branch filter",
|
|
|
|
},
|
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"},
|
2024-11-30 14:23:44 +00:00
|
|
|
Usage: "custom parameters to inject into the step environment. Format: KEY=value",
|
2019-04-06 19:32:14 +00:00
|
|
|
},
|
2023-12-08 08:36:53 +00:00
|
|
|
},
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 23:26:35 +00:00
|
|
|
func deploy(ctx context.Context, c *cli.Command) error {
|
|
|
|
client, err := internal.NewClient(ctx, c)
|
2019-04-06 19:32:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-12 23:07:52 +00:00
|
|
|
repo := c.Args().First()
|
|
|
|
repoID, err := internal.ParseRepo(client, repo)
|
2019-04-06 19:32:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
branch := c.String("branch")
|
|
|
|
event := c.String("event")
|
|
|
|
status := c.String("status")
|
|
|
|
|
2024-07-03 13:58:51 +00:00
|
|
|
if branch == "" {
|
|
|
|
repo, err := client.Repo(repoID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-30 00:47:17 +00:00
|
|
|
branch = repo.Branch
|
2024-07-03 13:58:51 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipelineArg := c.Args().Get(1)
|
2023-11-02 11:21:11 +00:00
|
|
|
var number int64
|
2022-10-18 01:24:12 +00:00
|
|
|
if pipelineArg == "last" {
|
|
|
|
// Fetch the pipeline number from the last pipeline
|
2024-11-26 10:50:48 +00:00
|
|
|
pipelines, err := client.PipelineList(repoID, woodpecker.PipelineListOptions{})
|
2024-05-24 20:35:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
for _, pipeline := range pipelines {
|
|
|
|
if branch != "" && pipeline.Branch != branch {
|
2019-04-06 19:32:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if event != "" && pipeline.Event != event {
|
2019-04-06 19:32:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if status != "" && pipeline.Status != status {
|
2019-04-06 19:32:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
if pipeline.Number > number {
|
|
|
|
number = pipeline.Number
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if number == 0 {
|
2024-01-10 21:56:42 +00:00
|
|
|
return fmt.Errorf("cannot deploy failure pipeline")
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-11-02 11:21:11 +00:00
|
|
|
number, err = strconv.ParseInt(pipelineArg, 10, 64)
|
2019-04-06 19:32:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 17:00:25 +00:00
|
|
|
envArgIndex := 2
|
|
|
|
env := c.Args().Get(envArgIndex)
|
2019-04-06 19:32:14 +00:00
|
|
|
if env == "" {
|
2024-01-10 21:56:42 +00:00
|
|
|
return fmt.Errorf("please specify the target environment (i.e. production)")
|
2019-04-06 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
2024-11-26 10:50:48 +00:00
|
|
|
opt := woodpecker.DeployOptions{
|
|
|
|
DeployTo: env,
|
|
|
|
Params: internal.ParseKeyPair(c.StringSlice("param")),
|
|
|
|
}
|
2019-04-06 19:32:14 +00:00
|
|
|
|
2024-11-26 10:50:48 +00:00
|
|
|
deploy, err := client.Deploy(repoID, number, opt)
|
2019-04-06 19:32:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.New("_").Parse(c.String("format"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tmpl.Execute(os.Stdout, deploy)
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// Template for deployment information.
|
2019-04-06 19:32:14 +00:00
|
|
|
var tmplDeployInfo = `Number: {{ .Number }}
|
|
|
|
Status: {{ .Status }}
|
|
|
|
Commit: {{ .Commit }}
|
|
|
|
Branch: {{ .Branch }}
|
|
|
|
Ref: {{ .Ref }}
|
|
|
|
Message: {{ .Message }}
|
|
|
|
Author: {{ .Author }}
|
|
|
|
Target: {{ .Deploy }}
|
|
|
|
`
|