Auto Deploy Bitbucket to EC2

User
automatic deploy bitbucket push to aws ec2
Assistant
To set up automatic deployment from a Bitbucket push to an AWS EC2 instance, you can follow these general steps:

### 1. **Prepare the EC2 Instance**
– Launch an EC2 instance with SSH access enabled.
– Ensure that you have a security group with ports 22 (SSH) and 80 (HTTP) or 443 (HTTPS) open for inbound connections.
– SSH into your EC2 instance and install necessary software like Git, Node.js, Composer, PHP, and any other dependencies your project might need.

For example, for a PHP Laravel app:

sudo apt update
   sudo apt install git curl php php-cli php-mbstring php-xml unzip

### 2. **Set Up SSH Keys for EC2 and Bitbucket**
– **Generate an SSH key pair** on your local machine if you don’t have one already:

ssh-keygen -t rsa -b 4096 -C "your-email@example.com"

– **Add the SSH public key** (`~/.ssh/id_rsa.pub`) to your EC2 instance for passwordless authentication.

– **Add the SSH public key to Bitbucket**:
– Go to **Bitbucket settings > SSH keys** and add the EC2 public key.

– **Test SSH access** from the EC2 instance to Bitbucket:

ssh -T git@bitbucket.org

### 3. **Set Up a Git Repository on EC2**
– SSH into the EC2 instance and navigate to the directory where your app should reside.
– Clone the Bitbucket repository:

git clone git@bitbucket.org:your-username/your-repository.git

– Make sure that your repository contains any necessary files like `.env` for environment variables and deployment scripts.

### 4. **Set Up a Webhook in Bitbucket**
– Go to **Bitbucket repository settings** and navigate to **Webhooks**.
– Add a new webhook with the URL pointing to your EC2 instance, for example:

http://your-ec2-public-ip/deploy-hook

– Choose the trigger events (typically, push events).

### 5. **Set Up a Webhook Listener Script on EC2**
– On the EC2 instance, you can write a simple PHP or Node.js script to listen for the webhook and trigger the deployment when a push occurs. Here’s an example in PHP (`deploy.php`):

<?php
   // Simple webhook listener for Bitbucket push events
   $payload = file_get_contents('php://input');
   $data = json_decode($payload);

   // Optional: Verify that the request comes from Bitbucket
   // e.g., using a secret key or a signature

   // Change to the directory of your app
   chdir('/var/www/your-app');

   // Pull the latest changes from the repository
   shell_exec('git pull origin master');

   // Run any additional deployment steps (e.g., composer install, migrations)
   shell_exec('composer install');
   shell_exec('php artisan migrate');
   ?>

– Save this file and make sure the script is executable.
– You can place this script in the document root of your server or use a separate server (such as a Node.js application) to listen for the Bitbucket webhook.

### 6. **Configure a Web Server (Optional)**
– If you want to make the webhook listener accessible over HTTP, configure your web server (Apache, Nginx) to serve the script.
– For example, with Nginx, add a configuration to serve your `deploy.php` or `deploy.js`.

### 7. **Test the Deployment**
– Push changes to your Bitbucket repository. This should trigger the webhook, and the EC2 instance will automatically pull the latest changes, install dependencies, and run any necessary tasks (like database migrations).

### 8. **Security Considerations**
– Use a secret key or signature to verify that the incoming webhook request is from Bitbucket.
– Set proper file and directory permissions on your EC2 instance to avoid exposing sensitive files.
– Limit SSH access to trusted IPs or use other security measures to protect your instance.

### Additional Notes:
– **CI/CD Tools**: For more advanced deployment strategies, you might consider integrating CI/CD tools like Jenkins, Bitbucket Pipelines, or GitLab CI to automate the build and deployment process further.
– **Automation with AWS Services**: You could use AWS services like CodeDeploy or ECS (Elastic Container Service) to further automate the process.

Let me know if you need more detailed steps for any of these!

Leave a Comment

Your email address will not be published. Required fields are marked *