Deploy an application from a GitHub repository to an EC2 instance. Args: instance_id: The ID of the EC2 instance to deploy to. repository: The GitHub repository (format: 'owner/repo'). branch: The branch to deploy from. deploy
(
self,
instance_id: str,
repository: str,
branch: str = 'main',
deploy_path: str = '/var/www/html',
setup_script: Optional[str] = None,
github_token: Optional[str] = None
)
| 683 | |
| 684 | @aws_operation("deploy_from_github") |
| 685 | def deploy_from_github( |
| 686 | self, |
| 687 | instance_id: str, |
| 688 | repository: str, |
| 689 | branch: str = 'main', |
| 690 | deploy_path: str = '/var/www/html', |
| 691 | setup_script: Optional[str] = None, |
| 692 | github_token: Optional[str] = None |
| 693 | ) -> Dict[str, Any]: |
| 694 | """ |
| 695 | Deploy an application from a GitHub repository to an EC2 instance. |
| 696 | |
| 697 | Args: |
| 698 | instance_id: The ID of the EC2 instance to deploy to. |
| 699 | repository: The GitHub repository (format: 'owner/repo'). |
| 700 | branch: The branch to deploy from. |
| 701 | deploy_path: The path on the instance to deploy to. |
| 702 | setup_script: Optional path to a setup script to run after deployment. |
| 703 | github_token: Optional GitHub token for private repositories. |
| 704 | |
| 705 | Returns: |
| 706 | Deployment status and details. |
| 707 | """ |
| 708 | from ..github import GitHubService |
| 709 | from ..core.credentials import get_credential_manager |
| 710 | |
| 711 | # Get instance details |
| 712 | instance = self.get_instance(instance_id) |
| 713 | |
| 714 | # Create GitHub service client |
| 715 | if github_token: |
| 716 | github = GitHubService(token=github_token) |
| 717 | else: |
| 718 | cred_manager = get_credential_manager() |
| 719 | github_credentials = cred_manager.get_github_credentials() |
| 720 | github = GitHubService(token=github_credentials.token) |
| 721 | |
| 722 | # Generate the deployment script |
| 723 | deploy_cmd = f"""#!/bin/bash |
| 724 | set -e |
| 725 | |
| 726 | # Install git if not already installed |
| 727 | if ! command -v git &> /dev/null; then |
| 728 | if command -v apt-get &> /dev/null; then |
| 729 | apt-get update |
| 730 | apt-get install -y git |
| 731 | elif command -v yum &> /dev/null; then |
| 732 | yum install -y git |
| 733 | fi |
| 734 | fi |
| 735 | |
| 736 | # Create deployment directory |
| 737 | mkdir -p {deploy_path} |
| 738 | |
| 739 | # Clone or update the repository |
| 740 | if [ -d "{deploy_path}/.git" ]; then |
| 741 | cd {deploy_path} |
| 742 | git fetch |
no test coverage detected