Configuring CircleCI

Now that you have configured the CircleCI access to your droplet, you need to add a config file to your project to specify the jobs you want to execute for the deployment process. This process is shown in the following steps:

  1. For this, you need to create the .circleci directory and add the following inside the file config.yml:
  version: 2
jobs:
build:
branches:
only:
- master
working_directory: ~/tmp
docker:
- image: circleci/node:10
steps:
- checkout
- run: npm install
- run: npm run lint
- run: npm test
- run: ssh -o StrictHostKeyChecking=no $DROPLET_USER@$DROPLET_IP 'cd production; git checkout master; git pull; npm install; npm run start:production;'
  1. When you have a .yml file, you need to be careful with the indentation; since it is similar to Python, if you don't indent correctly you will get an error. Let's see how this file is structured:
  version: 2
  1. Specify the CircleCI version we will use. In this case, you are using version 2 (the latest one, at the time of writing this book):
  jobs:
  1. Inside jobs, we will specify that it needs to configure the container; we will create it using Docker, and also outline the steps to follow for the deployment process:
  build:
branches:
only:
- master
  1. In this block, we are specifying that we will use only the master branch of the repository. Of course, you can change it for any branch you want to deploy:
working_directory: ~/tmp
  1. The working_directory will be the temporal directory we will use to install the NPM packages and run our deploy scripts. In this case, I decided to use the tmp directory, as follows:
docker:
- image: circleci/node:10
  1. As I said before, we will create a Docker container, and in this case, I selected an existing image that includes node: 10. If you want to know about all the available images, you can visit https://circleci.com/docs/2.0/circleci-images:
steps:
- checkout
- run: npm install
- run: npm run lint
- run: npm test
- run: ssh -o StrictHostKeyChecking=no $DROPLET_USER@$DROPLET_IP 'cd production; git checkout master; git pull; npm install; npm run start:production;'

For code case, the checkout is a simple git checkout to master, then on each run sentence, you need to specify the scripts you want to run. Follow these steps:

  1. npm install: First, you need to install the NPM packages to be able to perform the next tasks.
    1. npm run lint: Executes the ESLint validation; if it fails, it will break the deployment process, otherwise, it continues with the next run.
  2. npm run test: Executes the jest validations; if it fails, it will break the deployment process, otherwise, it continues with the next run.
  3. In the last step, we connect to our Digital Ocean Droplet, passing the StrictHostKeyChecking=no flag to disable the strict host key checking, then we use the $DROPLET_USER and $DROPLET_IP ENV variables to connect it (we will create those in the next step), and finally, we will specify all the commands we will perform inside our droplet using single quotes. These commands are listed as follows: 
    • cd production: Grants access to the production (or your Git repository name)
    • git checkout master: This will checkout the master branch
    • git pull: Pulls the latest changes from our repository
    • npm run start:production: This is the final step, which runs our project in production mode
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.174.76