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.
|
|
|
|
|
|
|
|
package pipeline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/common"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/internal"
|
|
|
|
)
|
|
|
|
|
|
|
|
var pipelineStartCmd = &cli.Command{
|
|
|
|
Name: "start",
|
|
|
|
Usage: "start a pipeline",
|
2023-06-12 23:07:52 +00:00
|
|
|
ArgsUsage: "<repo-id|repo-full-name> [pipeline]",
|
2022-10-18 01:24:12 +00:00
|
|
|
Action: pipelineStart,
|
|
|
|
Flags: append(common.GlobalFlags,
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "param",
|
|
|
|
Aliases: []string{"p"},
|
2022-10-28 15:38:53 +00:00
|
|
|
Usage: "custom parameters to be injected into the step environment. Format: KEY=value",
|
2022-10-18 01:24:12 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
func pipelineStart(c *cli.Context) (err error) {
|
2023-06-12 23:07:52 +00:00
|
|
|
repoIDOrFullName := c.Args().First()
|
|
|
|
client, err := internal.NewClient(c)
|
2022-10-18 01:24:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-12 23:07:52 +00:00
|
|
|
repoID, err := internal.ParseRepo(client, repoIDOrFullName)
|
2022-10-18 01:24:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pipelineArg := c.Args().Get(1)
|
|
|
|
var number int
|
|
|
|
if pipelineArg == "last" {
|
|
|
|
// Fetch the pipeline number from the last pipeline
|
2023-06-12 23:07:52 +00:00
|
|
|
pipeline, err := client.PipelineLast(repoID, "")
|
2022-10-18 01:24:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
number = pipeline.Number
|
|
|
|
} else {
|
|
|
|
if len(pipelineArg) == 0 {
|
2022-10-28 15:38:53 +00:00
|
|
|
return errors.New("missing step number")
|
2022-10-18 01:24:12 +00:00
|
|
|
}
|
|
|
|
number, err = strconv.Atoi(pipelineArg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
params := internal.ParseKeyPair(c.StringSlice("param"))
|
|
|
|
|
2023-06-12 23:07:52 +00:00
|
|
|
pipeline, err := client.PipelineStart(repoID, number, params)
|
2022-10-18 01:24:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-12 23:07:52 +00:00
|
|
|
fmt.Printf("Starting pipeline %s#%d\n", repoIDOrFullName, pipeline.Number)
|
2022-10-18 01:24:12 +00:00
|
|
|
return nil
|
|
|
|
}
|