# CI Integration using Neurelo CLI

## Overview

Testing is a critical step in the software development lifecycle. [Neurelo CLI](https://docs.neurelo.com/cli-preview-version) makes it really easy to set up a "CI" environment that can be used for testing purposes.

## Creating a Machine User

The first step to automating your testing process with Neurelo is to create a new "Machine User" for your Organization. This user will have access to all projects in your Organization, and can be removed at any time.&#x20;

To create one,&#x20;

* Go to the [Org members page](https://dashboard.neurelo.com/members)&#x20;
* Click on the dropdown next to "Invite User" on the top right
* Click "Create Machine User"
* Set an identifier for your "Machine User" and click "Submit".

{% hint style="danger" %}
Once the "Machine User" has been created, you will be shown two pieces of information: the "client id" and the "client secret", make sure to store them safely and don't lose them, as the "client secret" won't be shown again.
{% endhint %}

This "client id" and "client secret" can now be used to authenticate the CLI and execute commands without any human interaction. For more details on the Neurelo CLI, please refer [here](https://docs.neurelo.com/cli-preview-version)

## Using the machine user in the CLI

Before running the CLI,

* Setup  environment variables `NEURELO_CLIENT_ID` and `NEURELO_CLIENT_SECRET` using the "client\_id" and "client\_secret" from the prior step

Then,

* Run `neurelo login`

The CLI will look for the environment variables `NEURELO_CLIENT_ID` and `NEURELO_CLIENT_SECRET`, and will use them automatically when `neurelo login` is invoked.

## Using the CLI for CI tests

Once the CLI is authenticated, you can invoke&#x20;

{% code overflow="wrap" %}

```bash
neurelo ci start --projectId=.. --commitId=.. --region=.. --connectionString=.. --runId=.. --resetDb
```

{% endcode %}

which will do multiple things:

* Use the project configured with the `--projectId=` value
* Create a Data Source pointing to the provided `--connectionString=` value
* Create an Environment using the provided `--region=` and `--commitId=` values and attaching the previously created Data Source to it
* Create an API Key for the created Environment
* Start the Environment runners and wait for them to be ready
* Present the API Key as the output for it to be used in your tests
* The `--runId=` value is used to generate the Data Source, Environment, and API Key names. It is recommended to use unique and easy-to-identify values for this, like the short version of a commit hash or similar
* The `--resetDb` flag indicates that a full reset of the database should be applied before the runners are started. This means, delete the existing db schema and execute the available migrations for the environment

{% hint style="info" %}
Note - Mock Data Generation support is coming soon which will also enable you to generate mock data for your testing automatically as part of this piepline
{% endhint %}

After your tests have finished executing, by just invoking `neurelo ci stop`, all the previously created resources will be deleted.

## Sample Script

Here is a sample bash script that makes use of the Neurelo CLI to execute tests for a nodejs project:

{% code overflow="wrap" %}

```sh
#####################################################
#                    SET UP                         #
#####################################################

NEURELO_CLIENT_ID=3cf50a07...
NEURELO_CLIENT_SECRET=jj2h...

NEURELO_PROJECT_ID=prj_613d3826...
NEURELO_COMMIT_ID=cmt_e020f328...
DS_CONN_STRING=postgres://user:pass@psqlinstance:5432/cli
CI_IDENTIFIER=my-unique-identifier

neurelo login

ci_start_output=$(neurelo ci start --projectId=$NEURELO_PROJECT_ID --commitId=$NEURELO_COMMIT_ID --region=us-west-2 --connectionString=$DS_CONN_STRING --runId=$CI_IDENTIFIER)

NEURELO_API_KEY=$(echo $ci_start_output | grep 'Key: neurelo_' | cut -d ' ' -f 2| cut -b 1-245)

#####################################################
#                    RUN TESTS                      #
#####################################################

npm run test

#####################################################
#                   TEAR DOWN                       #
#####################################################

neurelo ci stop
```

{% endcode %}
