Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions pipeline.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
- script: |
#!/bin/bash

# Exit immediately if a command exits with a non-zero status
set -e

# Ensure required environment variables are set
if [ -z "$OPSLEVEL_ROUTING_ID" ] || [ -z "$SERVICE_ALIAS" ] || [ -z "$DEPLOYMENT_ENV" ]; then
echo "Environment variables OPSLEVEL_ROUTING_ID, SERVICE_ALIAS, and DEPLOYMENT_ENV must be set."
exit 1
fi

# Variables (modify or provide dynamically in Azure pipeline environment)
DEPLOYER_ID="$(Build.QueuedById)"
DEPLOYER_ID="${DEPLOYER_ID:-$(Build.QueuedById)}"
DEPLOYER_EMAIL="$(Build.RequestedForEmail)"
DEPLOYER_NAME="$(Build.RequestedFor)"
DEPLOY_NUMBER="$(Build.BuildNumber)"
DEPLOY_URL="$(Build.BuildUri)"
VERSION="${VERSION:-$(git rev-parse --short HEAD)}" # Default to the current git commit hash
DESCRIPTION="${DESCRIPTION:-"Deployed by CI Pipeline: Deploy #${DEPLOY_NUMBER}"}"
COMMIT_SHA="${COMMIT_SHA:-$(git rev-parse HEAD)}"
COMMIT_MESSAGE="${COMMIT_MESSAGE:-$(git log -1 --pretty=%B)}"
COMMIT_BRANCH="${COMMIT_BRANCH:-$(git rev-parse --abbrev-ref HEAD)}"
COMMIT_DATE="${COMMIT_DATE:-$(git show --no-patch --format='%cd')}"
COMMITTER_NAME="${COMMITTER_NAME:-$(git show --no-patch --format='%cn')}"
COMMITTER_EMAIL="${COMMITTER_EMAIL:-$(git show --no-patch --format='%ce')}"
AUTHOR_NAME="${AUTHOR_NAME:-$(git show --no-patch --format='%an')}"
AUTHOR_EMAIL="${AUTHOR_EMAIL:-$(git show --no-patch --format='%ae')}"
AUTHORING_DATE="${AUTHORING_DATE:-$(git show --no-patch --format='%ad')}"
DEPLOYED_AT="`date -u +%Y-%m-%dT%H:%M:%SZ`"

# API endpoint
OPSLEVEL_ENDPOINT="https://app.opslevel.com/integrations/deploy/"

# Payload for the OpsLevel API
data="{
\"dedup_id\": \"$(uuidgen)\",
\"service\": \"$SERVICE_ALIAS\",
\"deployer\": {
\"id\": \"$DEPLOYER_ID\",
\"email\": \"$DEPLOYER_EMAIL\",
\"name\": \"$DEPLOYER_NAME\"
},
\"deployed_at\": \"$DEPLOYED_AT\",
\"environment\": \"$DEPLOYMENT_ENV\",
\"description\": \"$DESCRIPTION\",
\"deploy_url\": \"$DEPLOY_URL\",
\"deploy_number\": \"$DEPLOY_NUMBER\",
\"commit\": {
\"sha\": \"$COMMIT_SHA\",
\"message\": \"$COMMIT_MESSAGE\",
\"branch\": \"$COMMIT_BRANCH\",
\"date\": \"$COMMIT_DATE\",
\"committer_name\": \"$COMMITTER_NAME\",
\"committer_email\": \"$COMMITTER_EMAIL\",
\"author_name\": \"$AUTHOR_NAME\",
\"author_email\": \"$AUTHOR_EMAIL\",
\"authoring_date\": \"$AUTHORING_DATE\"
}
}"

# Send data to OpsLevel
response=$(curl -s -i -X POST "$OPSLEVEL_ENDPOINT" \
-H "Content-Type: application/json" \
-H "X-OpsLevel-Routing-ID: $OPSLEVEL_ROUTING_ID" \
-d "$data")

# Check response for errors
echo "$response" | grep -q '"errors"' && {
echo "Error sending deployment data to OpsLevel: $response"
exit 1
}

echo "Deployment data successfully sent to OpsLevel."
displayName: 'Send OpsLevel Deployment Details'