Bitbucket Pipeline EC2 Deploy
November 16, 2024 · AI Conversations
### 1. **Configure SSH Access**
Ensure your EC2 instance can securely receive files from Bitbucket through SSH.
– **Generate an SSH key pair on your local machine (or Bitbucket Pipeline container)** if you haven't already:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This will generate two files: a private key (`id_rsa`) and a public key (`id_rsa.pub`).
– **Add the public key to your EC2 instance**:
Copy the contents of `id_rsa.pub` and add it to the `~/.ssh/authorized_keys` file on your EC2 instance. You can do this manually or use the following command on your EC2 instance:
echo "your-public-key" >> ~/.ssh/authorized_keys
– **Add the private key to Bitbucket Pipeline**:
In Bitbucket, go to your repository settings → **Repository Settings** → **Pipelines** → **SSH Keys**. Add the private key (`id_rsa`) as an environment variable, and ensure it's added securely to the pipeline.
### 2. **Create Bitbucket Pipeline Configuration File**
Create a `.bitbucket-pipelines.yml` file in the root of your repository. This file defines the steps for the pipeline.
Here is a sample `.bitbucket-pipelines.yml` configuration for deploying a Laravel and Vue.js app to AWS EC2:
image: node:21 # Use Node.js 21 Docker image for building the frontend
pipelines:
branches:
master:
- step:
name: Build and Deploy to EC2
deployment: production
caches:
- node
- composer
script:
- apt-get update && apt-get install -y ssh # Install SSH client to connect to EC2
- npm install # Install frontend dependencies
- npm run build # Build your Vue.js app
- cd /path/to/laravel && composer install --no-interaction # Install backend dependencies
- ssh -o StrictHostKeyChecking=no ec2-user@your-ec2-ip "cd /path/to/your/laravel && git pull origin master && npm install && php artisan migrate && npm run production && php artisan config:cache"
### Explanation of the above configuration:
– **`image: node:21`**: Uses a Node.js 21 Docker image for building the frontend (you can adjust based on your requirements).
– **`pipelines`**: Defines pipeline steps for the `master` branch.
– **`caches`**: Caches `node_modules` and Composer dependencies to speed up builds.
– **`script`**: The actual deployment steps, including:
– Installing SSH client on the build container.
– Installing and building frontend dependencies (Vue.js).
– Pulling the latest code on your EC2 instance using SSH and running necessary Laravel and Vue.js commands like migrations, build, and cache clearing.
### 3. **Setting up Environment Variables**
Make sure you add your EC2 IP and other sensitive credentials (like private SSH key, AWS access credentials, etc.) as environment variables in the Bitbucket repository settings:
– Go to **Repository Settings** → **Pipelines** → **Environment Variables**.
– Add environment variables for your EC2 details, such as:
– `EC2_USER` (e.g., `ec2-user`)
– `EC2_HOST` (your EC2 public IP)
– `EC2_KEY` (your private SSH key, stored securely in Bitbucket)
You can reference these variables in the pipeline configuration like so:
- ssh -o StrictHostKeyChecking=no -i $EC2_KEY $EC2_USER@$EC2_HOST "cd /path/to/your/laravel && git pull origin master && npm install && php artisan migrate && npm run production && php artisan config:cache"
### 4. **Test the Pipeline**
After setting up the `.bitbucket-pipelines.yml` file and pushing your changes to the Bitbucket repository, the pipeline should automatically trigger on any push to the `master` branch. You can check the Bitbucket pipeline logs for debugging if anything goes wrong.
### 5. **Additional Setup (Optional)**
– **Environment-specific configurations**: If you have different environments (e.g., staging and production), you can add separate steps in the pipeline for each environment and use deployment variables to adjust the deployment process.
– **Handling secrets securely**: If you need to handle sensitive data such as `.env` files, you can securely store these in Bitbucket Pipeline environment variables or use a tool like AWS Secrets Manager.
This setup should automate the process of deploying your Laravel and Vue.js app to your EC2 instance directly from Bitbucket pushes.
– npm install
Not needed
### Updated `.bitbucket-pipelines.yml`
image: node:21 # Use Node.js 21 Docker image for building the frontend
pipelines:
branches:
master:
- step:
name: Build and Deploy to EC2
deployment: production
caches:
- node
- composer
script:
- npm run build # Build your Vue.js app
- cd /path/to/laravel && composer install --no-interaction # Install backend dependencies
- ssh -o StrictHostKeyChecking=no -i $EC2_KEY $EC2_USER@$EC2_HOST "cd /path/to/your/laravel && git pull origin master && npm install && php artisan migrate && npm run production && php artisan config:cache"
### Explanation of Changes:
– Removed `apt-get update && apt-get install -y ssh` because you mentioned SSH installation is not needed. (Ensure the build container already has SSH installed, or you can set it up in your custom pipeline image if necessary).
– Removed `npm install` in the pipeline since you don’t need to reinstall dependencies during the pipeline execution. The pipeline will only build the frontend (`npm run build`).
This version assumes that you are managing dependencies separately on your EC2 instance (through `composer install`, `npm install`, etc.) and only pulling the latest changes and rebuilding necessary parts on the EC2 instance.