Convert GitHub Action to Bitbucket

User
name: Deploy to EC2

on:
push:
branches:
– master # Trigger on push to master branch

jobs:
deploy:
runs-on: ubuntu-latest
steps:
– name: Checkout code
uses: actions/checkout@v2

– name: Set up SSH
env:
PEM_FILE: ${{ secrets.PEM_FILE }} # Store your PEM file content in GitHub secrets
run: |
echo "$PEM_FILE" > key.pem
chmod 600 key.pem

– name: Ensure proper permissions on EC2
env:
EC2_USER: ${{ secrets.EC2_USER }} # Store your EC2 username in GitHub secrets
EC2_HOST: ${{ secrets.EC2_HOST }} # Store your EC2 host in GitHub secrets
run: |
# Ensure that the correct permissions are set on the EC2 instance
ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "sudo chown -R $EC2_USER:$EC2_USER /var/www/shareyourthoughts && sudo chmod -R 775 /var/www/shareyourthoughts"

– name: Delete public/build folder from EC2
env:
EC2_USER: ${{ secrets.EC2_USER }} # Store your EC2 username in GitHub secrets
EC2_HOST: ${{ secrets.EC2_HOST }} # Store your EC2 host in GitHub secrets
run: |
ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "rm -rf /var/www/shareyourthoughts/public/build"

– name: Deploy to EC2
env:
EC2_USER: ${{ secrets.EC2_USER }} # Store your EC2 username in GitHub secrets
EC2_HOST: ${{ secrets.EC2_HOST }} # Store your EC2 host in GitHub secrets
run: |
# Run rsync with updated timestamps only for changed files, excluding storage if needed
rsync -avz –exclude 'storage/*' -e "ssh -i key.pem -o StrictHostKeyChecking=no" ./ $EC2_USER@$EC2_HOST:/var/www/shareyourthoughts

– name: Install Composer dependencies
env:
EC2_USER: ${{ secrets.EC2_USER }}
EC2_HOST: ${{ secrets.EC2_HOST }}
run: |
ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "
cd /var/www/shareyourthoughts &&
composer update –no-dev –optimize-autoloader"

– name: Run DB Migrations
env:
EC2_USER: ${{ secrets.EC2_USER }}
EC2_HOST: ${{ secrets.EC2_HOST }}
run: |
ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "
cd /var/www/shareyourthoughts &&
php artisan migrate"

– name: Change ownership to www-data
env:
EC2_USER: ${{ secrets.EC2_USER }} # Store your EC2 username in GitHub secrets
EC2_HOST: ${{ secrets.EC2_HOST }} # Store your EC2 host in GitHub secrets
run: |
# Change ownership to www-data:www-data
ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "sudo chown -R www-data:www-data /var/www/shareyourthoughts"
rm key.pem # Clean up the PEM file after the final deployment
convert above to bitbucket pipeline

Assistant
Here's the equivalent Bitbucket Pipelines configuration for deploying your Laravel project to an EC2 instance:
Assistant
{"name": "bitbucket_pipeline_ec2", "type": "code/yaml", "content": "image: ubuntu:latestnndefinitions:n services:n docker:n memory: 2048nnpipelines:n branches:n master:n – step:n name: Deploy to EC2n script:n – apt-get update && apt-get install -y openssh-client rsyncn – echo "$PEM_FILE" > key.pemn – chmod 600 key.pemn – ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "sudo chown -R $EC2_USER:$EC2_USER /var/www/shareyourthoughts && sudo chmod -R 775 /var/www/shareyourthoughts"n – ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "rm -rf /var/www/shareyourthoughts/public/build"n – rsync -avz –exclude 'storage/*' -e "ssh -i key.pem -o StrictHostKeyChecking=no" ./ $EC2_USER@$EC2_HOST:/var/www/shareyourthoughtsn – ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "cd /var/www/shareyourthoughts && composer update –no-dev –optimize-autoloader"n – ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "cd /var/www/shareyourthoughts && php artisan migrate"n – ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "sudo chown -R www-data:www-data /var/www/shareyourthoughts"n – rm key.pemn services:n – dockern caches:n – composern artifacts:n – key.pemnnenvironment:n PEM_FILE: $PEM_FILEn EC2_USER: $EC2_USERn EC2_HOST: $EC2_HOST"}
Assistant
This Bitbucket Pipeline will execute the deployment process when changes are pushed to the `master` branch. Make sure you add `PEM_FILE`, `EC2_USER`, and `EC2_HOST` as environment variables in your Bitbucket repository settings. Let me know if you need any modifications! ๐Ÿš€
User
what its file name
Assistant
The file name for this Bitbucket Pipelines configuration should be **`bitbucket-pipelines.yml`** and it should be placed in the root of your repository. Let me know if you need any adjustments! ๐Ÿš€
User
remove services docker
caches composer
Assistant
{"updates":[{"pattern":".*","replacement":"image: ubuntu:latestnnpipelines:n branches:n master:n – step:n name: Deploy to EC2n script:n – apt-get update && apt-get install -y openssh-client rsyncn – echo "$PEM_FILE" > key.pemn – chmod 600 key.pemn – ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "sudo chown -R $EC2_USER:$EC2_USER /var/www/shareyourthoughts && sudo chmod -R 775 /var/www/shareyourthoughts"n – ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "rm -rf /var/www/shareyourthoughts/public/build"n – rsync -avz –exclude 'storage/*' -e "ssh -i key.pem -o StrictHostKeyChecking=no" ./ $EC2_USER@$EC2_HOST:/var/www/shareyourthoughtsn – ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "cd /var/www/shareyourthoughts && composer update –no-dev –optimize-autoloader"n – ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "cd /var/www/shareyourthoughts && php artisan migrate"n – ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "sudo chown -R www-data:www-data /var/www/shareyourthoughts"n – rm key.pemnnenvironment:n PEM_FILE: $PEM_FILEn EC2_USER: $EC2_USERn EC2_HOST: $EC2_HOST"}]}
Assistant
I've removed the `services: docker` and `caches: composer` sections from your Bitbucket Pipelines configuration. Let me know if you need any further adjustments! ๐Ÿš€
User
ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "sudo chown -R $EC2_USER:$EC2_USER /var/www/vuefront && sudo chmod -R 775 /var/www/vuefront"
1s
+ ssh -i key.pem -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST "sudo chown -R $EC2_USER:$EC2_USER /var/www/vuefront && sudo chmod -R 775 /var/www/vuefront"
Warning: Permanently added '$EC2_HOST' (ED25519) to the list of known hosts.
Load key "key.pem": error in libcrypto
$EC2_USER@$EC2_HOST: Permission denied (publickey).
Assistant
The error `Load key "key.pem": error in libcrypto` indicates that the private key (`PEM_FILE`) might be corrupted, improperly formatted, or not correctly set as a secret in Bitbucket.

### Steps to Fix:

1. **Ensure the Private Key is Correct**
– Verify that the private key (`.pem` file) is in the correct format. The private key should start with:

-----BEGIN OPENSSH PRIVATE KEY-----

or

-----BEGIN RSA PRIVATE KEY-----

2. **Re-upload the Key in Bitbucket Secrets**
– Go to **Repository Settings โ†’ Repository variables**.
– Remove the existing `PEM_FILE` secret.
– Re-upload the private key with proper formatting.
– Wrap the private key in **double quotes (`""`)** when setting the secret.

3. **Modify the Pipeline to Handle Multi-line Secrets Correctly**
– Update your `bitbucket-pipelines.yml` to properly handle multiline secrets:

- echo "$PEM_FILE" | tr -d 'r' > key.pem
     - chmod 600 key.pem

4. **Check SSH Permissions**
– Ensure your EC2 instance allows the correct user for SSH access.
– Run this command from your local machine to verify:

ssh -i key.pem ubuntu@your-ec2-host

– If it works locally but not in Bitbucket, the issue is likely with how the key is stored in Bitbucket variables.

Try these steps and let me know if the issue persists! ๐Ÿš€

Leave a Comment

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