Use pm2 with CircleCI

I am using pm2 on my remote ubuntu server and CircleCI for CI, I've got the following configuration files:

version: 2.1
orbs:
  node: circleci/[email protected]
jobs:
  deploy-prod:
    docker:
    # specify the version you desire here (you might not want node)
    - image: circleci/node:7.10
    steps:
        - checkout
        - run: ssh -oStrictHostKeyChecking=no -v $DROPLET_USER@$DROPLET_IP ./deploy_project.sh $MICROSERVICE_NAME
workflows:
    build-and-test:
      jobs:
        - deploy-prod:
            filters:
              branches:
                only:
                  - master

In my deploy script I do the following:

cd /var/www/nodejs/$1
git pull [email protected]:DevandScorp/hippocrates_authorizationmicroservice.git
cd ..
pm2 restart ecosystem.config.js --only $1

But I've got the following error:

./deploy_project.sh: line 4: pm2: command not found

Is it possible to run my server's pm2 in CircleCI config or can I reload my microservice automatically in another way?

Upvotes: 3

Views: 705

Answers (2)

Jordan Réjaud
Jordan Réjaud

Reputation: 482

Add your deploy SSH keys to Github (or whatever your remote source control is), link it to CircleCI to allow Circle CI to SSH into your remote server and have it run pm2 there.

Adding your SSH Keys to Github

You'd follow similar steps for Bitbucket, etc

https://github.com/settings/ssh/new

Add your SSH fingerprint to your CI steps:

      - add_ssh_keys:
          fingerprints:
            - "bb:bb:bb:bb:bb:bb:bb:bb:bb:bb:bb:bb:bb:bb:bb:bb"

CI SSHes into your remote server and runs pm2:

    steps:
      - checkout
      - run:
          name: Deploy over SSH
          command: ssh -p your_port_number your_user@your_host "cd ../path/to/your/project; git pull; pm2 start hello_sts";

I followed this guide: https://scribe.rip/@blueish/how-to-setup-ci-cd-with-circleci-and-deploy-your-nodejs-project-to-a-remote-server-275a31d1f6c4

although it's for Bitbucket, not Github.

Upvotes: -1

So, if you want to make anything on your server using CircleCI, it's just a waste of time. CircleCI provides a virtual environment, where you can, for example, make some tests. Also you can push changes on your remote server, but CircleCI will not have any access to your server's system. So if we speak about pm2, you can enable watch mode and relaunch your microservice everytime CircleCI push changes to it

Upvotes: 3

Related Questions