Convert GitHub Action to Bitbucket
March 9, 2025 ยท AI Conversations
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
caches composer
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).
### 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! ๐