-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
buildkite-agent step cancel --step "key"
subcommand
- Loading branch information
Showing
6 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package clicommand | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/buildkite/agent/v3/api" | ||
"github.com/buildkite/agent/v3/logger" | ||
"github.com/buildkite/roko" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
const stepCancelHelpDescription = `Usage: | ||
buildkite-agent step cancel <attribute> <value> [options...] | ||
Description: | ||
Cancel all unfinished jobs for a step | ||
Example: | ||
$ buildkite-agent step cancel --step "key" | ||
$ buildkite-agent step cancel --step "key" --force` | ||
|
||
type StepCancelConfig struct { | ||
StepOrKey string `cli:"step" validate:"required"` | ||
Force bool `cli:"force"` | ||
|
||
// Global flags | ||
Debug bool `cli:"debug"` | ||
LogLevel string `cli:"log-level"` | ||
NoColor bool `cli:"no-color"` | ||
Experiments []string `cli:"experiment" normalize:"list"` | ||
Profile string `cli:"profile"` | ||
|
||
// API config | ||
DebugHTTP bool `cli:"debug-http"` | ||
AgentAccessToken string `cli:"agent-access-token" validate:"required"` | ||
Endpoint string `cli:"endpoint" validate:"required"` | ||
NoHTTP2 bool `cli:"no-http2"` | ||
} | ||
|
||
var StepCancelCommand = cli.Command{ | ||
Name: "cancel", | ||
Usage: "Cancel all unfinished jobs for a step", | ||
Description: stepCancelHelpDescription, | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "step", | ||
Value: "", | ||
Usage: "The step to cancel. Can be either its ID (BUILDKITE_STEP_ID) or key (BUILDKITE_STEP_KEY)", | ||
EnvVar: "BUILDKITE_STEP_ID", | ||
}, | ||
cli.BoolFlag{ | ||
Name: "force", | ||
Usage: "Don't wait for the agent to finish before cancelling the jobs", | ||
EnvVar: "BUILDKITE_STEP_CANCEL_FORCE", | ||
}, | ||
|
||
// API Flags | ||
AgentAccessTokenFlag, | ||
EndpointFlag, | ||
NoHTTP2Flag, | ||
DebugHTTPFlag, | ||
|
||
// Global flags | ||
NoColorFlag, | ||
DebugFlag, | ||
LogLevelFlag, | ||
ExperimentsFlag, | ||
ProfileFlag, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
ctx, cfg, l, _, done := setupLoggerAndConfig[StepCancelConfig](context.Background(), c) | ||
defer done() | ||
|
||
return cancelStep(ctx, cfg, l) | ||
}, | ||
} | ||
|
||
func cancelStep(ctx context.Context, cfg StepCancelConfig, l logger.Logger) error { | ||
// Create the API client | ||
client := api.NewClient(l, loadAPIClientConfig(cfg, "AgentAccessToken")) | ||
|
||
// Create the value to cancel | ||
cancel := &api.StepCancel{ | ||
Force: cfg.Force, | ||
} | ||
|
||
// Post the change | ||
if err := roko.NewRetrier( | ||
roko.WithMaxAttempts(10), | ||
roko.WithStrategy(roko.Constant(5*time.Second)), | ||
).DoWithContext(ctx, func(r *roko.Retrier) error { | ||
stepCancelResponse, resp, err := client.StepCancel(ctx, cfg.StepOrKey, cancel) | ||
if resp != nil && (resp.StatusCode == 400 || resp.StatusCode == 401 || resp.StatusCode == 404) { | ||
r.Break() | ||
} | ||
if err != nil { | ||
l.Warn("%s (%s)", err, r) | ||
return err | ||
} | ||
|
||
l.Info("Successfully cancelled step: %s", stepCancelResponse.UUID) | ||
return nil | ||
}); err != nil { | ||
return fmt.Errorf("Failed to cancel step: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package clicommand | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/buildkite/agent/v3/logger" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStepCancel(t *testing.T) { | ||
t.Parallel() | ||
ctx := context.Background() | ||
|
||
t.Run("success", func(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
rw.WriteHeader(http.StatusOK) | ||
rw.Write([]byte(`{"uuid": "b0db1550-e68c-428f-9b4d-edf5599b2cff"}`)) | ||
})) | ||
|
||
cfg := StepCancelConfig{ | ||
Force: true, | ||
StepOrKey: "some-random-key", | ||
AgentAccessToken: "agentaccesstoken", | ||
Endpoint: server.URL, | ||
} | ||
|
||
l := logger.NewBuffer() | ||
err := cancelStep(ctx, cfg, l) | ||
assert.Nil(t, err) | ||
assert.Contains(t, l.Messages, "[info] Successfully cancelled step: b0db1550-e68c-428f-9b4d-edf5599b2cff") | ||
}) | ||
|
||
t.Run("failed", func(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
rw.WriteHeader(http.StatusInternalServerError) | ||
})) | ||
|
||
cfg := StepCancelConfig{ | ||
Force: true, | ||
StepOrKey: "some-random-key", | ||
AgentAccessToken: "agentaccesstoken", | ||
Endpoint: server.URL, | ||
} | ||
|
||
l := logger.NewBuffer() | ||
err := cancelStep(ctx, cfg, l) | ||
assert.Contains(t, err.Error(), "Failed to cancel step") | ||
}) | ||
} |