skip to Main Content

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


  1. Chosen as BEST ANSWER

    This is the full workable code

    image: atlassian/default-image:4
    
    pipelines:
      branches:
        dev:
          - step:
              name: Install Dependencies & Deploy
              script:
                - echo "Setting up SSH agent..."
                # Run deployment script on the remote server
                - ssh -v $USER@$SERVER 'cd /home/ubuntu/api && ls && echo $PATH && /bin/bash deploy.sh'
    

    Here is my deploy.sh

    echo "Deploy script started"
    cd /home/ubuntu/api
    node -version
    sh pull.sh
    echo "Deploy script finished execution"
    

    and here is the pull.sh

    git pull origin dev
    
    #!/bin/bash
    
    echo "Deploy script started"
    
    # Set the PATH explicitly (if needed)
    export PATH=$PATH:/usr/local/bin:/usr/bin
    
    
    # Install Node.js and Yarn
    echo "Installing Node.js and Yarn..."
    sudo apt-get update && sudo apt-get install -y curl
    curl -sL https://deb.nodesource.com/setup_20.x | sudo -E bash -
    sudo apt-get install -y nodejs
    sudo npm install -g yarn
    sudo npm install -g pm2
    # Display installed versions of Node.js and Yarn for debugging
    node -v
    yarn -v
    
    # Pull latest changes from the dev branch
    echo "Pulling latest changes from dev branch..."
    git pull origin dev
    
    # Install dependencies and build the application
    echo "Installing dependencies and building the application..."
    yarn install
    yarn build
    
    echo "Deploy script finished execution"
    
    
    
    
    # yarn 
    # yarn build
    pm2 start
    

  2. 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):

    • 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/ && 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search