Created
May 28, 2025 02:00
-
-
Save devops-school/b7a7f9f54f9784c35bec02ece9d0dce5 to your computer and use it in GitHub Desktop.
Gitlab Integration with Servicenow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stages: | |
- plan | |
- approval | |
- deploy | |
variables: | |
SERVICENOW_INSTANCE: "https://your-instance.service-now.com" | |
CHANGE_REQUEST_ID_FILE: "change_request_id.txt" | |
# Step 1: Create a Change Request in ServiceNow | |
create_change_request: | |
stage: plan | |
script: | |
- echo "Creating Change Request in ServiceNow..." | |
- > | |
curl -s -X POST "$SERVICENOW_INSTANCE/api/devops/create-change" \ | |
-H "Authorization: Bearer $SERVICENOW_TOKEN" \ | |
-H "Content-Type: application/json" \ | |
-d '{ | |
"short_description": "GitLab CI Deployment - $CI_PROJECT_NAME", | |
"category": "Software", | |
"pipeline_id": "'$CI_PIPELINE_ID'", | |
"commit_id": "'$CI_COMMIT_SHA'" | |
}' | jq -r '.result.change_request_id' > $CHANGE_REQUEST_ID_FILE | |
- export CHANGE_ID=$(cat $CHANGE_REQUEST_ID_FILE) | |
- echo "Created CR: $CHANGE_ID" | |
artifacts: | |
paths: | |
- $CHANGE_REQUEST_ID_FILE | |
expire_in: 1 hour | |
# Step 2: Wait for Change Approval | |
wait_for_approval: | |
stage: approval | |
dependencies: | |
- create_change_request | |
script: | |
- export CHANGE_ID=$(cat $CHANGE_REQUEST_ID_FILE) | |
- echo "Waiting for approval of CR: $CHANGE_ID..." | |
- | | |
for i in {1..30}; do | |
STATUS=$(curl -s -X GET "$SERVICENOW_INSTANCE/api/devops/change-status?cr_id=$CHANGE_ID" \ | |
-H "Authorization: Bearer $SERVICENOW_TOKEN" | jq -r '.result.status') | |
echo "Current status: $STATUS" | |
if [[ "$STATUS" == "approved" ]]; then | |
echo "✅ Change Request approved." | |
break | |
elif [[ "$STATUS" == "rejected" ]]; then | |
echo "❌ Change Request rejected." | |
exit 1 | |
fi | |
echo "Waiting for approval..." | |
sleep 30 | |
done | |
when: on_success | |
timeout: 15 minutes | |
# Step 3: Proceed with Deployment | |
deploy_app: | |
stage: deploy | |
script: | |
- echo "Deploying application to production..." | |
- ./deploy-prod.sh | |
only: | |
- main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment