-
Notifications
You must be signed in to change notification settings - Fork 55
Adapt bootstrap and autodeploy commands to GitHub Actions #979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
d2033c4
56ea2d5
01aedba
82479da
9dca072
58d204d
c999388
a160626
63fca69
101810d
b1dabd9
7e02312
3226020
b4381ad
324e272
55e04ac
221167e
0f95669
751eea3
39de8fc
ac9f8a8
e61a861
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| namespace RoboComponents; | ||
|
|
||
| use Robo\Symfony\ConsoleIO; | ||
| use Symfony\Component\Yaml\Yaml; | ||
|
|
||
| /** | ||
|
|
@@ -693,24 +694,21 @@ public function deployPantheonInstallEnv(string $env = 'qa', ?string $pantheon_n | |
| } | ||
|
|
||
| /** | ||
| * Prepares the repository to perform automatic deployment to Pantheon. | ||
| * Configures automatic deployment to Pantheon using GitHub Actions. | ||
| * | ||
| * This command generates an SSH key pair and provides instructions for | ||
| * setting up the necessary GitHub Secrets and Variables for automatic | ||
| * deployment. | ||
| * | ||
| * @param string $token | ||
| * Terminus machine token: https://pantheon.io/docs/machine-tokens. | ||
| * @param string $github_token | ||
| * Personal GitHub token: | ||
| * https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token. | ||
| * @param string $github_deploy_branch | ||
| * The branch that should be pushed automatically to Pantheon. By default, | ||
| * it's 'main', the default GitHub branch for any new project. | ||
| * @param string $pantheon_deploy_branch | ||
| * The branch at the artifact repo that should be the target of the | ||
| * deployment. As we typically deploy to QA, the default value here is 'qa', | ||
| * that multi-dev environment should be created by hand beforehand. | ||
| * | ||
| * @throws \Exception | ||
| */ | ||
| public function deployConfigAutodeploy(string $token, string $github_token, string $github_deploy_branch = 'main', string $pantheon_deploy_branch = 'qa'): void { | ||
| public function deployConfigAutodeploy(string $token, string $github_token): void { | ||
| $pantheon_info = $this->getPantheonNameAndEnv(); | ||
| $project_name = $pantheon_info['name']; | ||
|
|
||
|
|
@@ -720,63 +718,106 @@ public function deployConfigAutodeploy(string $token, string $github_token, stri | |
| throw new \Exception('The key generation failed.'); | ||
| } | ||
|
|
||
| // Encrypt the SSH key for use in GitHub Actions. | ||
| $result = $this->taskExec('openssl rand -hex 32')->printOutput(FALSE)->run(); | ||
| if ($result->getExitCode() !== 0) { | ||
| throw new \Exception('Failed to generate encryption key.'); | ||
| } | ||
| $encryption_key = trim($result->getMessage()); | ||
|
|
||
| $result = $this->taskExec('openssl rand -hex 16')->printOutput(FALSE)->run(); | ||
| if ($result->getExitCode() !== 0) { | ||
| throw new \Exception('Failed to generate encryption IV.'); | ||
| } | ||
| $encryption_iv = trim($result->getMessage()); | ||
|
|
||
| $result = $this->taskExec("openssl aes-256-cbc -K $encryption_key -iv $encryption_iv -in pantheon-key -out pantheon-key.enc")->run(); | ||
| if ($result->getExitCode() !== 0) { | ||
| throw new \Exception('The encryption of the private key failed.'); | ||
| } | ||
|
|
||
| $result = $this->taskExec("terminus connection:info $project_name.dev --fields='Git Command' --format=string | awk '{print $3}'") | ||
| ->printOutput(FALSE) | ||
| ->run(); | ||
| $pantheon_git_url = trim($result->getMessage()); | ||
|
|
||
| // Update GitHub Actions workflows if they exist. | ||
| if (file_exists('.github/workflows/lint.template.yml')) { | ||
| $this->_exec("cp .github/workflows/lint.template.yml .github/workflows/lint.yml"); | ||
| $this->taskReplaceInFile('.github/workflows/lint.yml') | ||
| ->from('{{ GITHUB_DEPLOY_BRANCH }}') | ||
| ->to($github_deploy_branch) | ||
| ->run(); | ||
| } | ||
| $this->say("The project was prepared for automatic deployment to Pantheon using GitHub Actions"); | ||
| $this->say(""); | ||
|
|
||
| $result = $this->taskExec('git add pantheon-key.enc')->run(); | ||
| if ($result->getExitCode() !== 0) { | ||
| throw new \Exception("git add failed."); | ||
| // Check if gh CLI is available. | ||
| $gh_available = $this->taskExec('which gh') | ||
| ->printOutput(FALSE) | ||
| ->run() | ||
| ->wasSuccessful(); | ||
|
|
||
| if ($gh_available) { | ||
| $this->say("Detected gh CLI - GitHub Secrets and Variables can be set automatically."); | ||
| $io = new ConsoleIO($this->input(), $this->output()); | ||
| $automate = $io->confirm('Would you like to automatically set up GitHub Secrets and Variables now?', TRUE); | ||
|
|
||
| if ($automate) { | ||
| $this->say(""); | ||
| $this->say("Setting up GitHub Secrets and Variables..."); | ||
|
|
||
| // Set secrets. | ||
| $result = $this->taskExec("gh secret set TERMINUS_TOKEN --body '$token' --repo " . self::$githubProject)->run(); | ||
| if ($result->wasSuccessful()) { | ||
| $this->say("✓ Set TERMINUS_TOKEN secret"); | ||
| } | ||
|
|
||
| $result = $this->taskExec("gh secret set PANTHEON_DEPLOY_KEY < pantheon-key --repo " . self::$githubProject)->run(); | ||
| if ($result->wasSuccessful()) { | ||
| $this->say("✓ Set PANTHEON_DEPLOY_KEY secret"); | ||
| } | ||
|
|
||
| $result = $this->taskExec("gh secret set GH_TOKEN --body '$github_token' --repo " . self::$githubProject)->run(); | ||
| if ($result->wasSuccessful()) { | ||
| $this->say("✓ Set GH_TOKEN secret"); | ||
| } | ||
|
|
||
| // Set variables. | ||
| $result = $this->taskExec("gh variable set PANTHEON_GIT_URL --body '$pantheon_git_url' --repo " . self::$githubProject)->run(); | ||
| if ($result->wasSuccessful()) { | ||
| $this->say("✓ Set PANTHEON_GIT_URL variable"); | ||
| } | ||
|
|
||
| $this->say(""); | ||
| $this->say("GitHub Secrets and Variables have been configured!"); | ||
| $this->say(""); | ||
| $this->say("Optional: You can also set ROLLBAR_SERVER_TOKEN and DEPLOY_EXCLUDE_WARNING variables if needed:"); | ||
| $this->say(" gh variable set ROLLBAR_SERVER_TOKEN --body 'your-token' --repo " . self::$githubProject); | ||
| $this->say(" gh variable set DEPLOY_EXCLUDE_WARNING --body 'warning1|warning2' --repo " . self::$githubProject); | ||
| } | ||
| else { | ||
| $this->printManualInstructions($token, $github_token, $pantheon_git_url); | ||
| } | ||
| } | ||
| else { | ||
| $this->say("Note: Install gh CLI (https://cli.github.com/) to automate secret/variable setup."); | ||
| $this->say(""); | ||
| $this->printManualInstructions($token, $github_token, $pantheon_git_url); | ||
| } | ||
|
|
||
| $this->say("The project was prepared for automatic deployment to Pantheon using GitHub Actions"); | ||
| $this->say(""); | ||
| $this->say("Please complete the following steps:"); | ||
| $this->say(""); | ||
| $this->say("1. Add the following secrets to your GitHub repository:"); | ||
| $this->say(" - Go to: Settings → Secrets and variables → Actions → New repository secret"); | ||
| $this->say(" - PANTHEON_GIT_URL: " . $pantheon_git_url); | ||
| $this->say(" - TERMINUS_TOKEN: " . $token); | ||
| $this->say(" - ENCRYPTED_KEY: " . $encryption_key); | ||
| $this->say(" - ENCRYPTED_IV: " . $encryption_iv); | ||
| $this->say(" - GITHUB_TOKEN: (use the automatically provided token or your personal token)"); | ||
| $this->say(" - ROLLBAR_SERVER_TOKEN: (your Rollbar token if applicable)"); | ||
| $this->say(""); | ||
| $this->say("2. Add the SSH public key to the Pantheon account:"); | ||
| $this->say("Remaining steps:"); | ||
| $this->say("1. Add the SSH public key to the Pantheon account:"); | ||
| $this->say(" - Key location: pantheon-key.pub"); | ||
| $this->say(" - Instructions: https://pantheon.io/docs/ssh-keys"); | ||
| $this->say(""); | ||
| $this->say("3. Review and commit the encrypted key file (pantheon-key.enc)"); | ||
| $this->say("2. IMPORTANT: Keep the pantheon-key file secure and DO NOT commit it to the repository."); | ||
| $this->say(" After adding the public key to Pantheon, you can delete the pantheon-key files locally."); | ||
| $this->say(""); | ||
| $this->say("4. Ensure nested docroot is configured: https://pantheon.io/docs/nested-docroot"); | ||
| $this->say("3. Ensure nested docroot is configured: https://pantheon.io/docs/nested-docroot"); | ||
| $this->say(""); | ||
| $this->say("For more details, see the 'Automatic Deployment to Pantheon' section in README.md"); | ||
| } | ||
|
|
||
| /** | ||
| * Prints manual instructions for setting up GitHub Secrets and Variables. | ||
| * | ||
| * @param string $token | ||
| * The Terminus token. | ||
| * @param string $github_token | ||
| * The GitHub token. | ||
| * @param string $pantheon_git_url | ||
| * The Pantheon Git URL. | ||
| */ | ||
| protected function printManualInstructions(string $token, string $github_token, string $pantheon_git_url): void { | ||
| $this->say("Please complete the following steps manually:"); | ||
| $this->say(""); | ||
| $this->say("1. Set up the following GitHub Secrets:"); | ||
| $this->say(" - Go to: Settings → Secrets and variables → Actions → Secrets → New repository secret"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot Can we automate it with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! Added gh CLI automation in commit 82479da. The command now:
The gh CLI can be installed on-demand in DDEV, and users will be prompted to confirm before any secrets are set. |
||
| $this->say(" - TERMINUS_TOKEN: " . $token); | ||
| $this->say(" - PANTHEON_DEPLOY_KEY: (paste the contents of pantheon-key file)"); | ||
| $this->say(" - GH_TOKEN: " . $github_token); | ||
| $this->say(""); | ||
| $this->say("2. Set up the following GitHub Variables:"); | ||
| $this->say(" - Go to: Settings → Secrets and variables → Actions → Variables → New repository variable"); | ||
| $this->say(" - PANTHEON_GIT_URL: " . $pantheon_git_url); | ||
| $this->say(" - ROLLBAR_SERVER_TOKEN: (your Rollbar token, optional)"); | ||
| $this->say(" - DEPLOY_EXCLUDE_WARNING: (warnings to exclude, optional)"); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot This is not enough. We are inside DDEV, we can sudo and install
ghif it's missing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 9dca072. The command now automatically installs gh CLI using sudo if it's not found in the DDEV environment. Installation uses apt with quiet flags to minimize output noise. If installation fails, it falls back to manual instructions.