Skip to content

Commit

Permalink
Add support for subcommand run/runfailed jobs and run/cancel stages o…
Browse files Browse the repository at this point in the history
…f GoCD pipeline
  • Loading branch information
nikhilsbhat committed Jul 17, 2023
1 parent ff8c9a6 commit 855ae28
Show file tree
Hide file tree
Showing 17 changed files with 398 additions and 23 deletions.
1 change: 1 addition & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func getGoCDCliCommands() *cobra.Command {
command.commands = append(command.commands, registerPipelinesCommand())
command.commands = append(command.commands, registerMaintenanceCommand())
command.commands = append(command.commands, registerJobsCommand())
command.commands = append(command.commands, registerStageCommand())
command.commands = append(command.commands, registerArtifactCommand())
command.commands = append(command.commands, registerAuthConfigCommand())
command.commands = append(command.commands, registerMaterialsCommand())
Expand Down
13 changes: 13 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,16 @@ func registerAgentsFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&agentName, "name", "", "",
"name of the agent on whom the action is to be performed")
}

func registerJobsNStageFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringVarP(&stageConfig.Pipeline, "pipeline", "", "",
"pipeline name from which the jobs/stage to be triggered")
cmd.PersistentFlags().StringVarP(&stageConfig.Name, "stage", "", "",
"stage name that should to be operated")
cmd.PersistentFlags().StringVarP(&stageConfig.PipelineInstance, "pipeline-counter", "", "",
"instance of the pipeline that should be considered")
cmd.PersistentFlags().StringVarP(&stageConfig.StageCounter, "stage-counter", "", "",
"instance of the stage that should be considered")
cmd.PersistentFlags().StringSliceVarP(&stageConfig.Jobs, "job", "", nil,
"list of jobs that should be triggered")
}
51 changes: 49 additions & 2 deletions cmd/jobs.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package cmd

import (
"github.com/nikhilsbhat/gocd-sdk-go"
"github.com/spf13/cobra"
)

var stageConfig gocd.Stage

func registerJobsCommand() *cobra.Command {
jobsCommand := &cobra.Command{
Use: "job",
Short: "Command to operate on jobs present in GoCD",
Long: `Command leverages GoCD job apis'
[https://api.gocd.org/current/#scheduled-jobs] to
GET/SCHEDULE jobs of specific pipelines present GoCD`,
SCHEDULE/RUN/RUN-FAILED jobs of specific pipeline present GoCD`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.Usage(); err != nil {
return err
Expand All @@ -23,6 +26,8 @@ GET/SCHEDULE jobs of specific pipelines present GoCD`,
jobsCommand.SetUsageTemplate(getUsageTemplate())

jobsCommand.AddCommand(getScheduledJobsCommand())
jobsCommand.AddCommand(getRunFailedJobsCommand())
jobsCommand.AddCommand(getRunJobsCommand())

for _, command := range jobsCommand.Commands() {
command.SilenceUsage = true
Expand All @@ -48,7 +53,49 @@ func getScheduledJobsCommand() *cobra.Command {
},
}

registerPipelineFlags(getScheduledJobsCmd)
return getScheduledJobsCmd
}

func getRunFailedJobsCommand() *cobra.Command {
getScheduledJobsCmd := &cobra.Command{
Use: "run-failed",
Short: "Command to run failed jobs of specific pipelines in GoCD [https://api.gocd.org/current/#run-failed-jobs]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli job run --pipeline myPipeline --pipeline-counter 2 --stage myStage --stage-counter 3`,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.RunFailedJobs(stageConfig)
if err != nil {
return err
}

return cliRenderer.Render(response)
},
}

registerJobsNStageFlags(getScheduledJobsCmd)

return getScheduledJobsCmd
}

func getRunJobsCommand() *cobra.Command {
getScheduledJobsCmd := &cobra.Command{
Use: "run",
Short: "Command to run list of jobs those are part of selected pipeline in GoCD [https://api.gocd.org/current/#run-selected-jobs]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli job run --pipeline myPipeline --pipeline-counter 2 --stage myStage --stage-counter 3 --job job1 --job job2`,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.RunJobs(stageConfig)
if err != nil {
return err
}

return cliRenderer.Render(response)
},
}

registerJobsNStageFlags(getScheduledJobsCmd)

return getScheduledJobsCmd
}
75 changes: 75 additions & 0 deletions cmd/stage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cmd

import "github.com/spf13/cobra"

func registerStageCommand() *cobra.Command {
jobsCommand := &cobra.Command{
Use: "stage",
Short: "Command to operate on stages of a pipeline present in GoCD",
Long: `Command leverages GoCD job apis'
[https://api.gocd.org/current/#stage-instancess] to
CANCEL/RUN stage of specific pipeline present GoCD`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.Usage(); err != nil {
return err
}

return nil
},
}

jobsCommand.SetUsageTemplate(getUsageTemplate())

jobsCommand.AddCommand(getCancelStageCommand())
jobsCommand.AddCommand(getRunStageCommand())

for _, command := range jobsCommand.Commands() {
command.SilenceUsage = true
}

return jobsCommand
}

func getCancelStageCommand() *cobra.Command {
getCancelStageCmd := &cobra.Command{
Use: "cancel",
Short: "Command to cancel specific stage of a pipeline present in GoCD [https://api.gocd.org/current/#run-failed-jobs]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli stage cancel --pipeline myPipeline --pipeline-counter 2 --stage myStage --stage-counter 3`,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.CancelStage(stageConfig)
if err != nil {
return err
}

return cliRenderer.Render(response)
},
}

registerJobsNStageFlags(getCancelStageCmd)

return getCancelStageCmd
}

func getRunStageCommand() *cobra.Command {
getScheduledJobsCmd := &cobra.Command{
Use: "run",
Short: "Command to run a stage from a selected pipeline present in GoCD [https://api.gocd.org/current/#run-selected-jobs]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli stage run --pipeline myPipeline --pipeline-counter 2 --stage myStage --stage-counter 3 --job job1 --job job2`,
RunE: func(cmd *cobra.Command, args []string) error {
response, err := client.RunStage(stageConfig)
if err != nil {
return err
}

return cliRenderer.Render(response)
},
}

registerJobsNStageFlags(getScheduledJobsCmd)

return getScheduledJobsCmd
}
3 changes: 2 additions & 1 deletion docs/doc/gocd-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ gocd-cli [flags]
* [gocd-cli pipeline-group](gocd-cli_pipeline-group.md) - Command to operate on pipeline groups present in GoCD [https://api.gocd.org/current/#pipeline-group-config]
* [gocd-cli plugin](gocd-cli_plugin.md) - Command to operate on plugins present in GoCD
* [gocd-cli server](gocd-cli_server.md) - Command to operate on GoCD server health status
* [gocd-cli stage](gocd-cli_stage.md) - Command to operate on stages of a pipeline present in GoCD
* [gocd-cli user](gocd-cli_user.md) - Command to operate on users in GoCD [https://api.gocd.org/current/#users]
* [gocd-cli version](gocd-cli_version.md) - Command to fetch the version of gocd-cli installed
* [gocd-cli who-am-i](gocd-cli_who-am-i.md) - Command to check which user being used by GoCD Command line interface

###### Auto generated by spf13/cobra on 14-Jul-2023
###### Auto generated by spf13/cobra on 17-Jul-2023
4 changes: 2 additions & 2 deletions docs/doc/gocd-cli_configrepo.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ gocd-cli configrepo [flags]
* [gocd-cli configrepo get](gocd-cli_configrepo_get.md) - Command to GET the config-repo information with a specified ID present in GoCD [https://api.gocd.org/current/#get-a-config-repo]
* [gocd-cli configrepo get-all](gocd-cli_configrepo_get-all.md) - Command to GET all config-repo information present in GoCD [https://api.gocd.org/current/#get-all-config-repos]
* [gocd-cli configrepo get-definitions](gocd-cli_configrepo_get-definitions.md) - Command to GET config-repo definitions present in GoCD [https://api.gocd.org/current/#definitions-defined-in-config-repo]
* [gocd-cli configrepo get-failed](gocd-cli_configrepo_get-failed.md) - Command to GET all config-repo that have failed in GoCD [https://api.gocd.org/current/#get-all-config-repos]
* [gocd-cli configrepo get-failed](gocd-cli_configrepo_get-failed.md) - Command to GET all config repos present in GoCD that are in a failed state [https://api.gocd.org/current/#get-all-config-repos]
* [gocd-cli configrepo list](gocd-cli_configrepo_list.md) - Command to LIST all configuration repository present in GoCD [https://api.gocd.org/current/#get-all-config-repos]
* [gocd-cli configrepo preflight-check](gocd-cli_configrepo_preflight-check.md) - Command to PREFLIGHT check the config repo configurations [https://api.gocd.org/current/#preflight-check-of-config-repo-configurations]
* [gocd-cli configrepo status](gocd-cli_configrepo_status.md) - Command to GET the status of config-repo update operation [https://api.gocd.org/current/#status-of-config-repository-update]
* [gocd-cli configrepo trigger-update](gocd-cli_configrepo_trigger-update.md) - Command to TRIGGER the update for config-repo to get latest revisions [https://api.gocd.org/current/#trigger-update-of-config-repository]
* [gocd-cli configrepo update](gocd-cli_configrepo_update.md) - Command to UPDATE the config-repo present in GoCD [https://api.gocd.org/current/#update-config-repo]

###### Auto generated by spf13/cobra on 1-Jul-2023
###### Auto generated by spf13/cobra on 17-Jul-2023
2 changes: 1 addition & 1 deletion docs/doc/gocd-cli_configrepo_get-failed.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## gocd-cli configrepo get-failed

Command to GET all config-repo that have failed in GoCD [https://api.gocd.org/current/#get-all-config-repos]
Command to GET all config repos present in GoCD that are in a failed state [https://api.gocd.org/current/#get-all-config-repos]

```
gocd-cli configrepo get-failed [flags]
Expand Down
6 changes: 4 additions & 2 deletions docs/doc/gocd-cli_job.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Command to operate on jobs present in GoCD

Command leverages GoCD job apis'
[https://api.gocd.org/current/#scheduled-jobs] to
GET/SCHEDULE jobs of specific pipelines present GoCD
SCHEDULE/RUN/RUN-FAILED jobs of specific pipeline present GoCD

```
gocd-cli job [flags]
Expand Down Expand Up @@ -40,6 +40,8 @@ gocd-cli job [flags]
### SEE ALSO

* [gocd-cli](gocd-cli.md) - Command line interface for GoCD
* [gocd-cli job run](gocd-cli_job_run.md) - Command to run list of jobs those are part of selected pipeline in GoCD [https://api.gocd.org/current/#run-selected-jobs]
* [gocd-cli job run-failed](gocd-cli_job_run-failed.md) - Command to run failed jobs of specific pipelines in GoCD [https://api.gocd.org/current/#run-failed-jobs]
* [gocd-cli job scheduled](gocd-cli_job_scheduled.md) - Command to GET a list of scheduled jobs in GoCD [https://api.gocd.org/current/#scheduled-jobs]

###### Auto generated by spf13/cobra on 1-Jul-2023
###### Auto generated by spf13/cobra on 17-Jul-2023
49 changes: 49 additions & 0 deletions docs/doc/gocd-cli_job_run-failed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## gocd-cli job run-failed

Command to run failed jobs of specific pipelines in GoCD [https://api.gocd.org/current/#run-failed-jobs]

```
gocd-cli job run-failed [flags]
```

### Examples

```
gocd-cli job run --pipeline myPipeline --pipeline-counter 2 --stage myStage --stage-counter 3
```

### Options

```
-h, --help help for run-failed
--job strings list of jobs that should be triggered
--pipeline string pipeline name from which the jobs/stage to be triggered
--pipeline-counter string instance of the pipeline that should be considered
--stage string stage name that should to be operated
--stage-counter string instance of the stage that should be considered
```

### Options inherited from parent commands

```
-t, --auth-token string token to authenticate with GoCD server, should not be co-used with basic auth (username/password)
--ca-file-path string path to file containing CA cert used to authenticate GoCD server, if you have one
--from-file string file containing configurations of objects that needs to be created in GoCD, config-repo/pipeline-group/environment and etc.
--json enable this to Render output in JSON format
-l, --log-level string log level for gocd cli, log levels supported by [https://github.com/sirupsen/logrus] will work (default "info")
--no-color enable this to Render output in YAML format
-p, --password string password to authenticate with GoCD server
-q, --query string query to filter the results, ex: '.material.attributes.type | id eq git'. this uses library gojsonq beneath
more queries can be found here https://github.com/thedevsaddam/gojsonq/wiki/Queries
--server-url string GoCD server URL base path (default "http://localhost:8153/go")
--skip-cache-config if enabled locally save auth configs would not be used to authenticate GoCD server (path: $HOME/.gocd/auth_config.yaml)
--to-file string file to which the output needs to be written to
-u, --username string username to authenticate with GoCD server
--yaml enable this to Render output in YAML format
```

### SEE ALSO

* [gocd-cli job](gocd-cli_job.md) - Command to operate on jobs present in GoCD

###### Auto generated by spf13/cobra on 17-Jul-2023
49 changes: 49 additions & 0 deletions docs/doc/gocd-cli_job_run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## gocd-cli job run

Command to run list of jobs those are part of selected pipeline in GoCD [https://api.gocd.org/current/#run-selected-jobs]

```
gocd-cli job run [flags]
```

### Examples

```
gocd-cli job run --pipeline myPipeline --pipeline-counter 2 --stage myStage --stage-counter 3 --job job1 --job job2
```

### Options

```
-h, --help help for run
--job strings list of jobs that should be triggered
--pipeline string pipeline name from which the jobs/stage to be triggered
--pipeline-counter string instance of the pipeline that should be considered
--stage string stage name that should to be operated
--stage-counter string instance of the stage that should be considered
```

### Options inherited from parent commands

```
-t, --auth-token string token to authenticate with GoCD server, should not be co-used with basic auth (username/password)
--ca-file-path string path to file containing CA cert used to authenticate GoCD server, if you have one
--from-file string file containing configurations of objects that needs to be created in GoCD, config-repo/pipeline-group/environment and etc.
--json enable this to Render output in JSON format
-l, --log-level string log level for gocd cli, log levels supported by [https://github.com/sirupsen/logrus] will work (default "info")
--no-color enable this to Render output in YAML format
-p, --password string password to authenticate with GoCD server
-q, --query string query to filter the results, ex: '.material.attributes.type | id eq git'. this uses library gojsonq beneath
more queries can be found here https://github.com/thedevsaddam/gojsonq/wiki/Queries
--server-url string GoCD server URL base path (default "http://localhost:8153/go")
--skip-cache-config if enabled locally save auth configs would not be used to authenticate GoCD server (path: $HOME/.gocd/auth_config.yaml)
--to-file string file to which the output needs to be written to
-u, --username string username to authenticate with GoCD server
--yaml enable this to Render output in YAML format
```

### SEE ALSO

* [gocd-cli job](gocd-cli_job.md) - Command to operate on jobs present in GoCD

###### Auto generated by spf13/cobra on 17-Jul-2023
12 changes: 2 additions & 10 deletions docs/doc/gocd-cli_job_scheduled.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ gocd-cli job scheduled"
### Options

```
--etag string etag used to identify the pipeline config. If you don't have one get it by using pipeline get command
-h, --help help for scheduled
-i, --instance int instance number of a pipeline
-m, --message string message to be passed while pausing/unpausing or commenting on pipeline present in GoCD
-n, --name string name of the pipeline present in GoCD
--pause enable to pause a pipeline
--pause-at-start enabling this will create the pipeline in the paused state
--template-name string name of the template to which the pipeline has to be extracted
--un-pause disable to pause a pipeline
-h, --help help for scheduled
```

### Options inherited from parent commands
Expand All @@ -49,4 +41,4 @@ gocd-cli job scheduled"

* [gocd-cli job](gocd-cli_job.md) - Command to operate on jobs present in GoCD

###### Auto generated by spf13/cobra on 1-Jul-2023
###### Auto generated by spf13/cobra on 17-Jul-2023
6 changes: 4 additions & 2 deletions docs/doc/gocd-cli_pipeline_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ gocd-cli pipeline list
### Options

```
-h, --help help for list
--environment strings GoCD environments from which the pipelines needs to be fetched
-h, --help help for list
--pipeline-group strings pipeline group names from which the pipelines needs to be fetched
```

### Options inherited from parent commands
Expand All @@ -41,4 +43,4 @@ gocd-cli pipeline list

* [gocd-cli pipeline](gocd-cli_pipeline.md) - Command to operate on pipelines present in GoCD

###### Auto generated by spf13/cobra on 1-Jul-2023
###### Auto generated by spf13/cobra on 17-Jul-2023
Loading

0 comments on commit 855ae28

Please sign in to comment.