Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: bytebase-action rollout skeleton (#15976)
p0ny7353c3c8ed02
diff --git a/action/README.md b/action/README.mdindex 52ecec852b..7814cfc55c 100644--- a/action/README.md+++ b/action/README.md@@ -4,17 +4,27 @@## Commands-### check+This action provides several subcommands to interact with Bytebase.-`bytebase-action check` checks the migration files. This is typically done in the CI phase.+### `check`++Usage: `bytebase-action check [global flags]`++Checks the SQL migration files matching the `--file-pattern`. This is typically used for linting or pre-deployment validation within a CI pipeline. It utilizes global flags like `--url`, `--service-account`, `--service-account-secret`, and `--file-pattern`.++### `rollout`++Usage: `bytebase-action rollout [global flags] [rollout flags]`++Creates a new release and initiates a rollout issue in the specified Bytebase `--project` to apply the SQL migration files matching the `--file-pattern` to the defined `--targets`. It uses global flags for connection and file discovery, and specific flags like `--release-title` and `--rollout-title` to name the created resources in Bytebase.## Configuration-This action is configured via command-line flags.+This action is configured via command-line flags. Global flags apply to all commands, while some commands have specific flags.### Global Flags-These flags apply to the main `bytebase-action` command and its subcommands.+These flags apply to the main `bytebase-action` command and its subcommands (`check`, `rollout`).- **`--url`**: The Bytebase instance URL.- Default: `https://demo.bytebase.com`@@ -37,10 +47,16 @@ These flags apply to the main `bytebase-action` command and its subcommands.- Database Group: `projects/{project}/databaseGroups/{databaseGroup}`- Default: `instances/test-sample-instance/databases/hr_test,instances/prod-sample-instance/databases/hr_prod`-### `check` Command Flags+- **`--file-pattern`**: A glob pattern used to find SQL migration files.+ - Used by subcommands like `check` and `rollout` to locate relevant files.+ - Default: `""` (empty string)-These flags are specific to the `check` subcommand (`bytebase-action check`).+### `rollout` Command Specific Flags-- **`--file-pattern`**: A glob pattern used to find SQL migration files to check.- - Default: `""` (empty string)- - *Required for the `check` command to find files.*+These flags are specific to the `rollout` subcommand (`bytebase-action rollout`).++- **`--release-title`**: The title of the release created in Bytebase.+ - Default: The current timestamp in RFC3339 format (e.g., `2025-04-25T17:32:07+08:00`).++- **`--rollout-title`**: The title of the rollout issue created in Bytebase.+ - Default: The current timestamp in RFC3339 format (e.g., `2025-04-25T17:32:07+08:00`).\ No newline at end of filediff --git a/action/api.go b/action/api.goindex b111aadbf4..9d53eb718c 100644--- a/action/api.go+++ b/action/api.go@@ -5,6 +5,7 @@ import ("fmt""io""net/http"+ "net/url""time""google.golang.org/protobuf/encoding/protojson"@@ -114,3 +115,124 @@ func (c *Client) checkRelease(project string, r *v1pb.CheckReleaseRequest) (*v1preturn resp, nil}++func (c *Client) createRelease(project string, r *v1pb.Release) (*v1pb.Release, error) {+ rb, err := protojson.Marshal(r)+ if err != nil {+ return nil, err+ }++ req, err := http.NewRequest("POST", fmt.Sprintf("%s/v1/%s/releases", c.url, project), bytes.NewReader(rb))+ if err != nil {+ return nil, err+ }++ body, err := c.doRequest(req)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to create release")+ }++ resp := &v1pb.Release{}+ if err := protojsonUnmarshaler.Unmarshal(body, resp); err != nil {+ return nil, err+ }++ return resp, nil+}++func (c *Client) previewPlan(project string, r *v1pb.PreviewPlanRequest) (*v1pb.PreviewPlanResponse, error) {+ rb, err := protojson.Marshal(r)+ if err != nil {+ return nil, err+ }++ req, err := http.NewRequest("POST", fmt.Sprintf("%s/v1/%s:previewPlan", c.url, project), bytes.NewReader(rb))+ if err != nil {+ return nil, err+ }++ body, err := c.doRequest(req)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to preview plan")+ }++ resp := &v1pb.PreviewPlanResponse{}+ if err := protojsonUnmarshaler.Unmarshal(body, resp); err != nil {+ return nil, err+ }++ return resp, nil+}++func (c *Client) createPlan(project string, r *v1pb.Plan) (*v1pb.Plan, error) {+ rb, err := protojson.Marshal(r)+ if err != nil {+ return nil, err+ }++ req, err := http.NewRequest("POST", fmt.Sprintf("%s/v1/%s/plans", c.url, project), bytes.NewReader(rb))+ if err != nil {+ return nil, err+ }++ body, err := c.doRequest(req)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to create plan")+ }++ resp := &v1pb.Plan{}+ if err := protojsonUnmarshaler.Unmarshal(body, resp); err != nil {+ return nil, err+ }++ return resp, nil+}++func (c *Client) getRollout(project, rolloutID string) (*v1pb.Rollout, error) {+ req, err := http.NewRequest("GET", fmt.Sprintf("%s/v1/%s/rollouts/%s", c.url, project, rolloutID), nil)+ if err != nil {+ return nil, err+ }++ body, err := c.doRequest(req)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to get rollout")+ }++ resp := &v1pb.Rollout{}+ if err := protojsonUnmarshaler.Unmarshal(body, resp); err != nil {+ return nil, err+ }++ return resp, nil+}++func (c *Client) createRollout(project string, r *v1pb.CreateRolloutRequest) (*v1pb.Rollout, error) {+ rb, err := protojson.Marshal(r.Rollout)+ if err != nil {+ return nil, err+ }+ a := fmt.Sprintf("%s/v1/%s/rollouts", c.url, project)+ if r.Target != nil {+ query := url.Values{}+ query.Set("target", *r.Target)+ a = a + "?" + query.Encode()+ }++ req, err := http.NewRequest("POST", a, bytes.NewReader(rb))+ if err != nil {+ return nil, err+ }++ body, err := c.doRequest(req)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to create rollout")+ }++ resp := &v1pb.Rollout{}+ if err := protojsonUnmarshaler.Unmarshal(body, resp); err != nil {+ return nil, err+ }++ return resp, nil+}diff --git a/action/main.go b/action/main.goindex a85ad58ca2..3f4eac09d3 100644--- a/action/main.go+++ b/action/main.go@@ -3,7 +3,9 @@ package mainimport ("log/slog""os"+ "time"+ "github.com/pkg/errors""github.com/spf13/cobra""github.com/bytebase/bytebase/action/github"@@ -17,11 +19,13 @@ var (URL stringServiceAccount stringServiceAccountSecret string- Project string+ Project string // projects/{project}Targets []string+ FilePattern string- // bytebase-action check flags- FilePattern string+ // bytebase-action rollout flags+ ReleaseTitle string // The title of the release+ RolloutTitle string // The title of the rollout}cmd = &cobra.Command{Use: "bytebase-action",@@ -37,6 +41,7 @@ func init() {// cmd.MarkPersistentFlagRequired("service-account-secret")cmd.PersistentFlags().StringVar(&Config.Project, "project", "projects/project-sample", "Bytebase project")cmd.PersistentFlags().StringSliceVar(&Config.Targets, "targets", []string{"instances/test-sample-instance/databases/hr_test", "instances/prod-sample-instance/databases/hr_prod"}, "Bytebase targets")+ cmd.PersistentFlags().StringVar(&Config.FilePattern, "file-pattern", "", "File pattern to glob migration files")// bytebase-action check flagscmdCheck := &cobra.Command{@@ -45,9 +50,18 @@ func init() {Args: cobra.NoArgs,RunE: runCI,}- cmdCheck.PersistentFlags().StringVar(&Config.FilePattern, "file-pattern", "", "File pattern to glob migration files")-cmd.AddCommand(cmdCheck)++ cmdRollout := &cobra.Command{+ Use: "rollout",+ Short: "Rollout the migrate files",+ Args: cobra.NoArgs,+ RunE: runRollout,+ }+ defaultTitle := time.Now().Format(time.RFC3339)+ cmdRollout.Flags().StringVar(&Config.ReleaseTitle, "release-title", defaultTitle, "The title of the release")+ cmdRollout.Flags().StringVar(&Config.RolloutTitle, "rollout-title", defaultTitle, "The title of the rollout")+ cmd.AddCommand(cmdRollout)}func Execute() error {@@ -93,6 +107,53 @@ func runCI(*cobra.Command, []string) error {return nil}+func runRollout(*cobra.Command, []string) error {+ client, err := NewClient(Config.URL, Config.ServiceAccount, Config.ServiceAccountSecret)+ if err != nil {+ return errors.Wrapf(err, "failed to create client")+ }+ releaseFiles, err := getReleaseFiles(Config.FilePattern)+ if err != nil {+ return errors.Wrapf(err, "failed to get release files")+ }+ createReleaseResponse, err := client.createRelease(Config.Project, &v1pb.Release{+ Title: Config.ReleaseTitle,+ Files: releaseFiles,+ VcsSource: nil, // TODO(p0ny): impl+ })++ planPreview, err := client.previewPlan(Config.Project, &v1pb.PreviewPlanRequest{+ Release: createReleaseResponse.Name,+ Targets: Config.Targets,+ AllowOutOfOrder: true,+ })+ if err != nil {+ return errors.Wrapf(err, "failed to preview plan")+ }++ planCreated, err := client.createPlan(Config.Project, planPreview.Plan)+ if err != nil {+ return errors.Wrapf(err, "failed to create plan")+ }++ // TODO(p0ny): wait for plan checks optionally++ rolloutCreated, err := client.createRollout(Config.Project, &v1pb.CreateRolloutRequest{+ Rollout: &v1pb.Rollout{+ Plan: planCreated.Name,+ },+ Target: nil, // TODO(p0ny): impl+ })+ if err != nil {+ return errors.Wrapf(err, "failed to create rollout")+ }++ _ = rolloutCreated+ // TODO(p0ny): wait for rollout to complete the target stage++ return nil+}+func main() {if err := cmd.Execute(); err != nil {slog.Error("failed to execute command", log.BBError(err))