I was trying to deploy code to my ec2 instance from bitbucket
image: node:slim
pipelines:
branches:
dev:
- step:
name: Install dependency package and build
caches:
- node
script:
- yarn
- yarn build
artifacts:
- node_modules/**
- dist/**
- step:
name: Deploy build folder to server via Rsync
script:
- pipe: atlassian/rsync-deploy:0.13.0
variables:
USER: $USER
SERVER: $SERVER
SSH_KEY: $MY_SSH_KEY
DEBUG: 'true'
REMOTE_PATH: '/home/ubuntu/api/dist'
LOCAL_PATH: 'dist/*'
- step:
name: Deploy node_modules to server via Rsync
script:
- pipe: atlassian/rsync-deploy:0.13.0
variables:
USER: $USER
SERVER: $SERVER
SSH_KEY: $MY_SSH_KEY
DEBUG: 'true'
REMOTE_PATH: '/home/ubuntu/api/node_modules'
LOCAL_PATH: 'node_modules/*'
- step:
name: Set execute permissions
script:
- pipe: atlassian/ssh-run:0.8.1
variables:
SSH_USER: $USER
SERVER: $SERVER
SSH_KEY: $MY_SSH_KEY
COMMAND: 'chmod +x /home/ubuntu/api/deploy.sh'
- step:
name: Run deploy script
script:
- pipe: atlassian/ssh-run:0.8.1
variables:
SSH_USER: $USER
SERVER: $SERVER
SSH_KEY: $MY_SSH_KEY
COMMAND: 'cd /home/ubuntu/api/ && ls'
above code works fine. and i got following result in from the last step
Status: Downloaded newer image for bitbucketpipelines/ssh-run:0.8.1
INFO: Executing the pipe...
INFO: Using passed SSH_KEY
INFO: Executing command on $SERVER
ssh -A -tt -i /root/.ssh/pipelines_id -o StrictHostKeyChecking=no -p 22 $USER@$SERVER bash -c 'cd /home/$USER/api/ && ls'
Dockerfile ecosystem.config.js test
README.md nest-cli.json tsconfig.build.json
bitbucket-pipelines.yml node_modules tsconfig.build.tsbuildinfo
deploy.sh package-lock.json tsconfig.json
dist package.json yarn.lock
docker-compose.yml src
Connection to $SERVER closed.
but when i try to run my project in to the remote server(ec2 instance).
lets say i change the last step as following
- step:
name: Run deploy script
script:
- pipe: atlassian/ssh-run:0.8.1
variables:
SSH_USER: $USER
SERVER: $SERVER
SSH_KEY: $MY_SSH_KEY
COMMAND: 'cd /home/ubuntu/api/ && yarn && yarn buuld'
it shows error yarn not found (Though my remote server has everything i can run it manually.)
i tried with a deploy.sh script the problem remains same.
Following works fine:
image: node:slim
pipelines:
branches:
dev:
- step:
name: Start application using PM2
script:
- pipe: atlassian/ssh-run:0.8.1
variables:
SSH_USER: $USER
SERVER: $SERVER
COMMAND: 'cd /home/ubuntu/api && ls && echo $PATH && /bin/bash git pull origin dev'
- step:
name: Deploying build
variable:
USER: $USER
SERVER: $SERVER
script:
- yarn
- yarn build
- cd /home/ubuntu/api
- npm install pm2 -g
- pm2 start
It working and i can found that my ec2 instance has been updated with latest code from bitbucket but now still step 2 not working.
2
Answers
This is the full workable code
Here is my deploy.sh
and here is the pull.sh
To fix the issue where yarn is not found during deployment via Bitbucket Pipelines on your EC2 instance, follow these steps:
Use a login shell to load environment variables: Update your command to run in a login shell so it loads the necessary paths (e.g., for yarn):
name: Run deploy script
script:
– pipe: atlassian/ssh-run:0.8.1
variables:
SSH_USER: $USER
SERVER: $SERVER
SSH_KEY: $MY_SSH_KEY
COMMAND: ‘cd /home/ubuntu/api/ && bash -lc "yarn && yarn build"’
Alternatively, specify the full path to yarn: Find the full path of yarn using which yarn, and use that path in the command:
COMMAND: ‘cd /home/ubuntu/api/ && /usr/local/bin/yarn && /usr/local/bin/yarn build’
This ensures yarn is found and executed correctly during deployment