# CodeShip

> Add a CodeShip pipeline that triggers a Testsigma test plan after a successful build.

CodeShip pipelines run custom commands after a successful build, so a pipeline can trigger a Testsigma execution, wait for the tests, and hold deployment until they pass.

## Prerequisites

- A CodeShip account with your organization, project, and source control (GitHub, Bitbucket, or GitLab) connected.

## Configure the pipeline

For GitHub, install the CodeShip App from the GitHub Marketplace so your repositories are accessible. Connected services are listed on CodeShip's **Authentications** page. For other SCMs, check the CodeShip documentation or contact CodeShip support.

1. **Open the project**: click **Projects** on the CodeShip home page and select your project. From the **Dashboard**, click **Project Settings**.

2. **Choose custom commands**: on the **Configure your Tests** page, select **I want to create my own custom commands** in the technology selector box.

3. **Install jq**: in the setup commands, enter `sudo apt-get install jq`. The trigger script uses `jq`, a JSON query parser library for bash.

4. **Add a pipeline**: click **Add Pipeline** and enter a name, for example `Testsigma Execution Trigger`.

## Add the trigger script

Get the execution ID from the Testsigma executions page. Then replace ``, ``, and `` in the script with your values.

```bash
#!/bin/bash

echo "Triggering Execution!"

# Triggering an execution using the <Execution-ID> from Testsigma Execution details page. Use your account login details as <Username> and <Password>
currentrunid=$(curl -sS -X POST -H 'Content-type: application/json' -u <Username:<Password> https://app.testsigma.com/rest/execution/<Execution-ID>/run)

echo "Started Execution. Waiting for tests to complete..."

currentstatus=1

#Use the Execution Status Check API and loop until the value of 'status' in the API Response equals to 0.
while [ $currentstatus -ne 0 ]; do currentstatus=$(curl -sS -X GET -H 'Content-type: application/json' -u <Username:<Password> https://app.testsigma.com/rest/execution/<Execution-ID>/run/$currentrunid/status | jq '.status'); sleep 5; done

echo "Test Execution Completed. Checking results..."

#Get the value of 'result' from the API Response for Execution Status Check API
currentresult=$(curl -sS -X GET -H 'Content-type: application/json' -u <Username:<Password> https://app.testsigma.com/rest/execution/<Execution-ID>/run/$currentrunid/status | jq '.result')

echo $currentresult

#Check the result of the execution using Execution Status Check API - If the value of 'result' is 0, the test passed. Else, the test failed.
if [ $currentresult -ne 0 ]; then echo "Test Failed. Please check Testsigma to know the reason for failure."; else echo "Test Passed! You may deploy now!"; fi

#Return the result as exit code - Non-zero value for execution result depicts Test failure and also triggers this script failure.
$(exit $currentresult)
```

This legacy script authenticates with a username and password against the older `/rest/execution` endpoints. For new setups, paste the current [shell script](https://testsigma.com/docs/v2/integrations/ci-cd/shell-script/) as the pipeline body instead; it authenticates with a bearer API key.

1. **Paste and save**: paste the script into the new test commands box and click **Save and go to Dashboard**.

2. **Add a build trigger**: go to **Project Settings > Build Trigger** and add one.

3. **Optional, add deployment**: under **Project Settings > Deploy**, add the details of your deployment platform.

With these settings, every change to the repository triggers a build, and the build starts the tests in Testsigma. The script waits until the run completes and checks that the tests passed.
