Blog: ServiceNow to Github Integration

BharatC
Kilo Guru

Automatically Create JSON Files in GitHub from ServiceNow RITMs

Modern DevOps workflows often require storing structured request data from ServiceNow into external repositories like GitHub for automation, auditing, integrations, and CI/CD pipelines.

In this article, I’ll explain how to automatically generate a JSON file in GitHub whenever a Request Item (RITM) is submitted in ServiceNow.

Using an Async Business Rule and GitHub REST API, we will:

Capture submitted catalog variables
Convert variables into JSON format
Encode the content
Send data to GitHub using REST API
Automatically create a JSON file inside a GitHub repository
Solution Flow
Service Catalog Item

RITM Created

Async Business Rule Triggered

Capture Catalog Variables

Convert Variables to JSON

Base64 Encode JSON

Send Request to GitHub API

JSON File Created in GitHub Repository
Step 1: Generate GitHub Personal Access Token

To allow ServiceNow to communicate securely with GitHub, create a Personal Access Token (PAT).

Steps
Login to GitHub
Go to:
Profile → Settings → Developer Settings
Open:
Personal Access Tokens
Create either:
Fine-grained token (Recommended)
Classic token
Provide permissions:
Repository Access
Contents → Read & Write
Generate the token
Copy and store it securely
Best Practice

Do not hardcode tokens inside scripts.

Store sensitive values in:

System Properties
Credentials
Credential Aliases
Step 2: Create REST Integration in ServiceNow

Create a REST Message record for GitHub communication.

Authentication

Use:

Username → GitHub Username
Password → GitHub Personal Access Token
Example Endpoint
https://api.github.com/repos/<owner>/<repo>/contents/<folder>/file_name.json

You can also create a GET method to verify whether the file already exists in GitHub.

Step 3: Create Async Business Rule

The Business Rule will:

Trigger when a Request Item is created
Read submitted catalog variables
Build a JSON payload
Create a JSON file in GitHub
Business Rule Configuration
Field Value
Table sc_req_item
When to Run Async
Insert True
Example Condition
current.cat_item.name == 'Standard Laptop'
Business Rule Script
(function executeRule(current, previous) {

try {

// Store standard fields
var storeVariables = {
request_number: current.number + '',
requested_for: current.requested_for.getDisplayValue(),
short_description: current.short_description + ''
};

// Best Practice:
// Store values in System Properties or Credentials
var githubToken = 'YOUR_GITHUB_TOKEN';
var githubRepo = 'YOUR_REPOSITORY';
var githubOwner = 'YOUR_GITHUB_USERNAME';

// Fetch catalog variables
var itemOptions = new GlideRecord('sc_item_option_mtom');

itemOptions.addQuery('request_item', current.sys_id);
itemOptions.query();

while (itemOptions.next()) {

var option =
itemOptions.sc_item_option.getRefRecord();

if (option.isValidRecord()) {

var varName =
option.item_option_new.name.toString();

var varValue =
option.value.toString();

storeVariables[varName] = varValue;
}
}

// Convert variables into JSON
var jsonVariables =
JSON.stringify(storeVariables, null, 2);

gs.info('Variables JSON: \n' + jsonVariables);

// Encode JSON content
var encodedContent =
GlideStringUtil.base64Encode(jsonVariables);

// GitHub request body
var requestBody = {

message:
'Creating JSON file from ServiceNow RITM ' +
current.number,

content: encodedContent,

branch: 'main'
};

// Create REST call
var r = new sn_ws.RESTMessageV2();

r.setHttpMethod('PUT');

r.setEndpoint(
'https://api.github.com/repos/' +
githubOwner + '/' +
githubRepo +
'/contents/RequestItem/' +
current.number + '.json'
);

r.setRequestHeader(
'Content-Type',
'application/json'
);

r.setRequestHeader(
'Authorization',
'Bearer ' + githubToken
);

r.setRequestBody(
JSON.stringify(requestBody)
);

var response = r.execute();

var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

if (httpStatus == 201) {

gs.info(
'GitHub file created successfully'
);

} else {

gs.error(
'Failed to create GitHub file. Status: ' +
httpStatus +
', Response: ' +
responseBody
);
}

} catch (ex) {

gs.error(
'Error sending RITM to GitHub: ' +
ex.message
);
}

})(current, previous);
Example JSON Output
{
"request_number": "RITM0010001",
"requested_for": "John Doe",
"short_description": "Laptop Request",
"ram": "16GB",
"storage": "512GB SSD",
"operating_system": "Windows 11"
}
Benefits of This Integration
Supports DevOps automation
Enables CI/CD integrations
Maintains structured request records
Useful for auditing and orchestration
Simplifies external integrations

0 REPLIES 0