GitLab Integration

Integrate ServiceNow with GitLab CI/CD pipelines for automated change management and deployment tracking

Overview

This guide shows how to integrate ServiceNow change management with GitLab CI/CD pipelines. The integration automates change request creation, approval gates, and deployment tracking directly from GitLab pipelines.

Integration Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         GitLab CI/CD Pipeline               β”‚
β”‚                                             β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚Build │─→│ Test │─→│ Create SNOW CR β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                 β”‚           β”‚
β”‚                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β”‚                    β–Ό                        β”‚
β”‚          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚          β”‚ Wait for Approvalβ”‚              β”‚
β”‚          β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β”‚                   β”‚ (Poll/Webhook)         β”‚
β”‚                   β–Ό                        β”‚
β”‚          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚          β”‚ Deploy to Prod   β”‚              β”‚
β”‚          β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β”‚                   β”‚                        β”‚
β”‚                   β–Ό                        β”‚
β”‚          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚          β”‚ Update SNOW CR   β”‚              β”‚
β”‚          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚
                    β–Ό
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚  ServiceNow          β”‚
         β”‚  Change Request      β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Prerequisites

ServiceNow Setup

  1. API Access: ServiceNow instance with REST API enabled

  2. User Account: Service account with itil role for change management

  3. OAuth Application (Recommended):

    • Navigate to System OAuth > Application Registry

    • Create new OAuth API endpoint for external clients

    • Note Client ID and Client Secret

GitLab Setup

  1. CI/CD Variables (Settings > CI/CD > Variables):

    • SERVICENOW_INSTANCE: Your ServiceNow instance name (e.g., dev12345)

    • SERVICENOW_CLIENT_ID: OAuth client ID (Type: Variable, Masked)

    • SERVICENOW_CLIENT_SECRET: OAuth client secret (Type: Variable, Masked)

    • SERVICENOW_USERNAME: ServiceNow username (if using basic auth)

    • SERVICENOW_PASSWORD: ServiceNow password (if using basic auth, Masked)

  2. Repository Variables:

    • ASSIGNMENT_GROUP: ServiceNow assignment group name

    • CMDB_CI: Configuration item sys_id

Integration Method 1: REST API with cURL

Simple Change Creation

Integration Method 2: Python Script

Advanced Change Management Script

Create .gitlab/scripts/servicenow.py:

Using the Python Script

Integration Method 3: GitLab Webhooks

ServiceNow Inbound Webhook

Configure ServiceNow to receive GitLab webhook events:

ServiceNow Configuration:

  1. Navigate to System Web Services > Scripted REST APIs

  2. Create new API: GitLab Integration

  3. Create resource: deployment_event

  4. Method: POST

Script:

GitLab Configuration:

Add webhook in Settings > Webhooks:

  • URL: https://instance.service-now.com/api/x_custom/gitlab_integration/deployment_event

  • Trigger: Pipeline events

  • Enable SSL verification

OAuth 2.0 Authentication

Setup OAuth in ServiceNow

  1. Create OAuth Application:

    • Navigate to System OAuth > Application Registry

    • Click New > Create an OAuth API endpoint for external clients

    • Fill in:

      • Name: GitLab CI/CD Integration

      • Client ID: (auto-generated, copy this)

      • Client Secret: (auto-generated, copy this)

      • Refresh Token Lifespan: 8640000 (100 days)

      • Access Token Lifespan: 1800 (30 minutes)

  2. Grant Access:

    • Accessible from: All application scopes

    • Active: Yes

Use OAuth in GitLab

Best Practices

Standard Changes for Speed

Pre-approve common deployment patterns:

Error Handling and Rollback

Include Testing Evidence

Troubleshooting

Common Issues

Issue
Solution

401 Unauthorized

Check credentials in GitLab CI/CD variables

403 Forbidden

Verify ServiceNow user has itil role

Timeout waiting for approval

Increase timeout, check ServiceNow approval workflow

Change not found

Verify CHANGE_SYS_ID is passed correctly via artifacts

SSL verification failed

Update curl/Python requests, check ServiceNow SSL cert

Debug Mode

Enable detailed logging:

Next Steps

Additional Resources

Last updated