uploadSecretToRepo uploads a secret to the repository if it doesn't already exist
(secretName, secretValue, repoSlug string, verbose bool)
| 458 | |
| 459 | // uploadSecretToRepo uploads a secret to the repository if it doesn't already exist |
| 460 | func uploadSecretToRepo(secretName, secretValue, repoSlug string, verbose bool) error { |
| 461 | engineSecretsLog.Printf("Uploading secret %s to %s", secretName, repoSlug) |
| 462 | |
| 463 | // Check if secret already exists |
| 464 | output, err := workflow.RunGHCombined("Checking secrets...", "secret", "list", "--repo", repoSlug) |
| 465 | if err == nil && stringContainsSecretName(string(output), secretName) { |
| 466 | if verbose { |
| 467 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Secret %s already exists, skipping upload", secretName))) |
| 468 | } |
| 469 | return nil |
| 470 | } |
| 471 | |
| 472 | // Upload the secret |
| 473 | if verbose { |
| 474 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Uploading %s secret to repository", secretName))) |
| 475 | } |
| 476 | |
| 477 | output, err = workflow.RunGHCombined("Setting secret...", "secret", "set", secretName, "--repo", repoSlug, "--body", secretValue) |
| 478 | if err != nil { |
| 479 | return fmt.Errorf("failed to set %s secret: %w (output: %s)", secretName, err, string(output)) |
| 480 | } |
| 481 | |
| 482 | fmt.Fprintln(os.Stderr, console.FormatSuccessMessage(fmt.Sprintf("Uploaded %s secret to repository", secretName))) |
| 483 | return nil |
| 484 | } |
| 485 | |
| 486 | // stringContainsSecretName checks if the gh secret list output contains a secret name |
| 487 | func stringContainsSecretName(output, secretName string) bool { |
no test coverage detected