# JSON File Listing Components

## Using the list of agents currently active in a specific environment <a href="#using-the-list-of-agents-currently-active-in-a-specific-environment" id="using-the-list-of-agents-currently-active-in-a-specific-environment"></a>

The script below retrieves the list of all the active instances of your application’s components from a specific environment and creates from it an Integration Build on Sealights Dashboard.\
It provides a “picture” of the testing environment (Lab) at this given point in time.

{% hint style="warning" %}
This solution is aimed to illustrate the concept and is based on the following assumptions:

* All the components are currently live in the specific environment, up and running as well as appearing in the *Cockpit Agent Monitor* page
* No component is designed with a “lazy loading” mechanism bringing it when necessary (on-demand). This restriction applies to front-end components as well (since they’re actively reporting only when the application is opened on the client-side browser).

If one of the assumptions above is not valid in your configuration, it may end up with incorrect data.
{% endhint %}

*

{% code overflow="wrap" lineNumbers="true" %}

```bash
#!/bin/bash

DOMAIN="mycompany.sealights.co"
SL_API_TOKEN=`cat sl_api_token.txt`
SL_AGENT_TOKEN=`cat sl_agent_token.txt`

INTEGRATION_BUILD_NAME="myIntegrationBuildSample"
QA_ENV="QA-01"

#Retrieve the Sealights Generated LabID based on App and Environment name
SL_LABID=`curl -sX GET "https://$DOMAIN/sl-api/v1/lab-ids?appName=$INTEGRATION_BUILD_NAME&branchName=$QA_ENV&buildType=integration" \
                -H "Authorization: Bearer $SL_API_TOKEN" -H "accept: application/json" | jq --raw-output .data.labIds[0].labId`
echo "Sealights' LabID is $SL_LABID"

#Prepare list of microservices in environment
curl -sX GET "https://$DOMAIN/sl-api/v1/agents/live" -H "Authorization: Bearer $SL_API_TOKEN" -H "accept: application/json">liveagents.txt
cat liveagents.txt | jq -c --arg LABID_PARAM $SL_LABID '.data[] | select(.labId==$LABID_PARAM and .type=="TestListener")' | jq -s 'group_by(.bsid)[] | {appName: .[0].appName, branch: .[0].branchName, build: .[0].buildName}' | jq -s>sl-integration-components.json

echo "List of components generated for the $INTEGRATION_BUILD_NAME on $QA_ENV"
cat sl-integration-components.json

#Download the java agent if not installed already
if [ ! -f "sealights-java-latest.zip" ]; then
    echo "Downloading Sealights Agent..."
    wget -nv  https://agents.sealights.co/sealights-java/sealights-java-latest.zip
    unzip -oq sealights-java-latest.zip
    echo "Local agent version is now:" `cat sealights-java-version.txt`
fi
                   
#Report new Integration Build to SL ("Picture" of ms deployed in the environment)
echo "Reporting new version of $INTEGRATION_BUILD_NAME (for $QA_ENV) to Sealights"
java -jar sl-build-scanner.jar -config -token $SL_AGENT_TOKEN -appname $INTEGRATION_BUILD_NAME -branchname $QA_ENV -buildname `date +"%y%m%d_%H%M"` -pi "*integration.build*" -buildsessionidfile integrationBSID.txt
java -jar sl-build-scanner.jar -scan -token $SL_AGENT_TOKEN -buildsessionidfile integrationBSID.txt -componentfile sl-integration-components.json 

echo Script is complete: $QA_ENV can be tested.
```

{% endcode %}

## Sample script for a Jenkins Pipeline job <a href="#sample-script-for-a-jenkins-pipeline-job" id="sample-script-for-a-jenkins-pipeline-job"></a>

Jenkins Pipeline scripts support `readJSON` and `writeJSON` and the sample code below allows you to create a JSON file for integration build declaration.

{% hint style="warning" %}
Plugin [Pipeline Utility Steps Plugin](https://wiki.jenkins.io/display/JENKINS/Pipeline+Utility+Steps+Plugin) needs to be installed on your Jenkins instance
{% endhint %}

{% code overflow="wrap" lineNumbers="true" %}

```groovy
pipeline {

   agent any

   stages {
       
       stage ("Creating JSON") {
            steps {
                script{
                    def sldata = readJSON text: '[{ "component_name": "frontend", "buildSessionId": "123" },{ "component_name": "backend", "buildSessionId": "456" }]'
                    
                    writeJSON(file: 'sl-ib-components.json', json: sldata )
                 }
             }
        }
       
      stage('Validate JSON') {
            steps {
                sh "cat sl-ib-components.json | jq ."
         }
      }
   }
}
```

{% endcode %}

{% hint style="info" %}
The sample code above is OS agnostic and should work for Jenkins instances installed on Windows or Linux servers.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sealights.io/knowledgebase/setup-and-configuration/build-and-test-execution/integration-build/integration-build-explicit-components-and-versions/json-file-listing-components.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
