Sample CircleCI Configuration with Prisma
0 votes
34 views
by Lydia Hallie

Introduction

In this tutorial, you will learn how to configure continuous integration using CircleCI with your Prisma server. The tutorial's examples will be based on the folder structure of this boilerplate app, which uses the latest Prisma Client and Jest for unit testing. This tutorial assumes that you:

  • have a CircleCI account or sign up to CircleCI.
  • are familiar with the CircleCI interface.

In order to understand the steps that need to be taken in order to configure CircleCI, it's important to know your folder structure and how to update the file paths accordingly.

Basic CircleCI Configuration

In order for CircleCI to build your project, you need to provide a config.yml file within a .circleci folder in your repository.

Step 1

Please make sure that you create the folder in your working directory.

mkdir .circleci
cd .circleci
touch config.yml
Copy

In case you're not yet familiar with the YAML setup of the configuration file, please take a look at CircleCI's configuration reference.

Step 2

In our case, assuming that we simply want to run the test script provided in our package.json in order to run the unit tests, a very basic config.yml file to get up and running with CircleCI could be:

version: 2
jobs:
  build:
    machine: true
    working_directory: ~/typescript-graphql-test
    steps:
      - checkout
      - run:
          name: Install Node Version Manager
          command: |
            curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
            export NVM_DIR="/opt/circleci/.nvm"
            [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
            nvm install v8.12.0
            nvm alias default v8.12.0
            echo 'export NVM_DIR="/opt/circleci/.nvm"' >> $BASH_ENV
            echo "[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"" >> $BASH_ENV
      - run:
          name: Start Prisma server
          command: |
            npm install && npm install -g prisma
            docker-compose up -d
            cd prisma
            sleep 20
            prisma deploy
      - run: npm run test
Copy

Executor type

CircleCI offers 3 executor types: docker, machine and macos. In order for CircleCI to work with our Prisma service, we have to run it in the Linux virtual machine image by setting machine to true. The machine setup gives us a lot more flexibility when it comes to using the exposed 4466 port that Prisma uses, compared to using docker.

Build steps

In this basic setup, we configure everything in order for our unit tests to run in CircleCI. You can basically seperate them into 3 different processes:

Installing Node Version Manager

Since we're using the Linux virtual machine image, we first need to install the Node Version Manager using cURL.

Step 3

As our app relies on the latest Node version, we need to install the latest stable version (currently 8.12.0).

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
export NVM_DIR="/opt/circleci/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
nvm install v8.12.0
nvm alias default v8.12.0
echo 'export NVM_DIR="/opt/circleci/.nvm"' >> $BASH_ENV
echo "[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"" >> $BASH_ENV
Copy

If you want to know more about how to install NVM on your virtual machine image, see NVM's install documentation.

Running the Prisma server

After installing the dependencies based on your package.json file and installing prisma globally, we run the Docker container based on your docker-compose.yml config the same way it works on your local machine. This process starts the Prisma server, which requires the mySQL Docker image to be available. As this could take some time to start, we need to set a timeout of some seconds before we can deploy our Prisma service.

Read more about the timeout that's required for Prisma here

Step 4
npm install && npm install -g prisma
docker-compose up -d
cd prisma
sleep 20
prisma deploy
Copy

Running the tests

After all dependencies have been installed and the Prisma server is up and running, the tests should get run and succeed.

Step 5
npm run test
Copy