Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: use subcommand to invoke action ci check (#15964)

p0ny3ed928aee610
diff --git a/action/README.md b/action/README.md
index bb964611a3..52ecec852b 100644
--- a/action/README.md
+++ b/action/README.md
@@ -2,25 +2,45 @@
`bytebase-action` helps to do common chores in database CI/CD with Bytebase.
-## Inputs
+## Commands
-This action requires the following inputs, configured as environment variables:
+### check
-### Environment Variables
+`bytebase-action check` checks the migration files. This is typically done in the CI phase.
-- **`BYTEBASE_URL`**: The Bytebase instance URL.
+## Configuration
-- **`BYTEBASE_SERVICE_ACCOUNT`**: The service account email.
+This action is configured via command-line flags.
-- **`BYTEBASE_SERVICE_ACCOUNT_SECRET`**: The service account password.
+### Global Flags
-- **`BYTEBASE_PROJECT`**: The target Bytebase project name.
- - Format: `projects/{project}`
+These flags apply to the main `bytebase-action` command and its subcommands.
-- **`BYTEBASE_TARGETS`**: A comma-separated string listing the target databases or database groups.
- - Can specify a database group or individual databases.
- - Formats:
- - Database: `instances/{instance}/databases/{database}`
- - Database Group: `projects/{project}/databaseGroups/{databaseGroup}`
+- **`--url`**: The Bytebase instance URL.
+ - Default: `https://demo.bytebase.com`
-- **`FILE_PATTERN`**: A pattern used to glob SQL migration files.
+- **`--service-account`**: The service account email.
+ - Default: `[email protected]`
+
+- **`--service-account-secret`**: The service account password.
+ - Default: Reads from the `BYTEBASE_SERVICE_ACCOUNT_SECRET` environment variable. You can override this by providing the flag directly.
+ - *Note: Setting the environment variable `BYTEBASE_SERVICE_ACCOUNT_SECRET` is the recommended way to handle the secret.*
+
+- **`--project`**: The target Bytebase project name.
+ - Format: `projects/{project}`
+ - Default: `projects/project-sample`
+
+- **`--targets`**: A comma-separated list or multiple uses of the flag specifying the target databases or database groups.
+ - Can specify a database group or individual databases.
+ - Formats:
+ - Database: `instances/{instance}/databases/{database}`
+ - Database Group: `projects/{project}/databaseGroups/{databaseGroup}`
+ - Default: `instances/test-sample-instance/databases/hr_test,instances/prod-sample-instance/databases/hr_prod`
+
+### `check` Command Flags
+
+These flags are specific to the `check` subcommand (`bytebase-action check`).
+
+- **`--file-pattern`**: A glob pattern used to find SQL migration files to check.
+ - Default: `""` (empty string)
+ - *Required for the `check` command to find files.*
diff --git a/action/main.go b/action/main.go
index 5c39a05994..a163829100 100644
--- a/action/main.go
+++ b/action/main.go
@@ -1,51 +1,71 @@
package main
import (
- "fmt"
"os"
- "strings"
- "github.com/pkg/errors"
+ "github.com/spf13/cobra"
"github.com/bytebase/bytebase/action/github"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
-func run(platform JobPlatform) error {
- url, serviceAccount, serviceAccountSecret := os.Getenv("BYTEBASE_URL"), os.Getenv("BYTEBASE_SERVICE_ACCOUNT"), os.Getenv("BYTEBASE_SERVICE_ACCOUNT_SECRET")
- if url == "" {
- return errors.Errorf("environment BYTEBASE_URL is not set")
- }
- if serviceAccount == "" {
- return errors.Errorf("environment BYTEBASE_SERVICE_ACCOUNT is not set")
- }
- if serviceAccountSecret == "" {
- return errors.Errorf("environment BYTEBASE_SERVICE_ACCOUNT_SECRET is not set")
- }
- project, bytebaseTargets, filePattern := os.Getenv("BYTEBASE_PROJECT"), os.Getenv("BYTEBASE_TARGETS"), os.Getenv("FILE_PATTERN")
- if project == "" {
- return errors.Errorf("environment BYTEBASE_PROJECT is not set")
+var (
+ Config struct {
+ // bytebase-action flags
+ URL string
+ ServiceAccount string
+ ServiceAccountSecret string
+ Project string
+ Targets []string
+
+ // bytebase-action check flags
+ FilePattern string
}
- if bytebaseTargets == "" {
- return errors.Errorf("environment BYTEBASE_TARGETS is not set")
+ cmd = &cobra.Command{
+ Use: "bytebase-action",
+ Short: "Bytebase action",
}
- if filePattern == "" {
- return errors.Errorf("environment FILE_PATTERN is not set")
+)
+
+func init() {
+ // bytebase-action flags
+ cmd.PersistentFlags().StringVar(&Config.URL, "url", "https://demo.bytebase.com", "Bytebase URL")
+ cmd.PersistentFlags().StringVar(&Config.ServiceAccount, "service-account", "[email protected]", "Bytebase Service account")
+ cmd.PersistentFlags().StringVar(&Config.ServiceAccountSecret, "service-account-secret", os.Getenv("BYTEBASE_SERVICE_ACCOUNT_SECRET"), "Bytebase Service account secret")
+ // 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")
+
+ // bytebase-action check flags
+ cmdCheck := &cobra.Command{
+ Use: "check",
+ Short: "Check the release files",
+ Args: cobra.NoArgs,
+ RunE: runCI,
}
- targets := strings.Split(bytebaseTargets, ",")
+ cmdCheck.PersistentFlags().StringVar(&Config.FilePattern, "file-pattern", "", "File pattern to glob migration files")
- client, err := NewClient(url, serviceAccount, serviceAccountSecret)
+ cmd.AddCommand(cmdCheck)
+}
+
+func Execute() error {
+ return cmd.Execute()
+}
+
+func runCI(*cobra.Command, []string) error {
+ platform := getJobPlatform()
+ client, err := NewClient(Config.URL, Config.ServiceAccount, Config.ServiceAccountSecret)
if err != nil {
return err
}
- releaseFiles, err := getReleaseFiles(filePattern)
+ releaseFiles, err := getReleaseFiles(Config.FilePattern)
if err != nil {
return err
}
- checkReleaseResponse, err := client.checkRelease(project, &v1pb.CheckReleaseRequest{
+ checkReleaseResponse, err := client.checkRelease(Config.Project, &v1pb.CheckReleaseRequest{
Release: &v1pb.Release{Files: releaseFiles},
- Targets: targets,
+ Targets: Config.Targets,
})
if err != nil {
return err
@@ -72,9 +92,5 @@ func run(platform JobPlatform) error {
}
func main() {
- platform := getJobPlatform()
- fmt.Printf("Hello, World - %s!\n", platform.String())
- if err := run(platform); err != nil {
- panic(err)
- }
+ cmd.Execute()
}