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
91 changes: 91 additions & 0 deletions opslevel-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
parameters:
- name: 'env'
default: 'Production'
type: string
- name: 'integrationId'
type: string
- name: 'serviceAlias'
type: string
- name: 'apiUrl'
default: 'https://app.opslevel.com/'
type: string
Comment on lines +2 to +11
Copy link
Contributor

@rocktavious rocktavious Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alphabetize by parameters name

Suggested change
- name: 'env'
default: 'Production'
type: string
- name: 'integrationId'
type: string
- name: 'serviceAlias'
type: string
- name: 'apiUrl'
default: 'https://app.opslevel.com/'
type: string
- name: 'apiUrl'
default: 'https://app.opslevel.com/'
type: string
- name: 'env'
default: 'Production'
type: string
- name: 'integrationId'
type: string
- name: 'serviceAlias'
type: string


jobs:
- job: deploy
steps:
- script: |
#!/bin/bash

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

# Variables (modify or provide dynamically in Azure pipeline environment)
DEPLOYER_ID="$(Build.QueuedById)"
DEPLOYER_ID="${DEPLOYER_ID:-$(Build.QueuedById)}"
Comment on lines +23 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DEPLOYER_ID="$(Build.QueuedById)"
DEPLOYER_ID="${DEPLOYER_ID:-$(Build.QueuedById)}"
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
VERSION="${VERSION:-$(git rev-parse --short HEAD)}" # Default to the current git commit hash
VERSION="$(git rev-parse --short HEAD)"

DESCRIPTION="${DESCRIPTION:-"Deployed by CI Pipeline: Deploy #${DEPLOY_NUMBER}"}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this works in AzureDevOps

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')}"
Comment on lines +31 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these have input environment variables - my understanding is they will always be blank.

So 2 things

  • Should we expose all (or some) of these as parameters?
  • Should we remove the variables and just run the git commands.

DEPLOYED_AT="`date -u +%Y-%m-%dT%H:%M:%SZ`"


# API endpoint, Routing ID and Service Alias
OPSLEVEL_ENDPOINT="${{ parameters.apiUrl }}integrations/deploy/"
DEPLOYMENT_ENV="${{ parameters.env }}"
OPSLEVEL_ROUTING_ID="${{ parameters.integrationId }}"
SERVICE_ALIAS="${{ parameters.serviceAlias }}"

# Payload for the OpsLevel API
data="{
\"dedup_id\": \"$(uuidgen)\",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be a unique ID per invocation - it should be the Build.BuildNumber or some sort of "pipeline" number. The purpose of the dedup_id is incase you need to retry the pipeline its considered the same event incase we received 2

\"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\"
}
}"

echo "$data"

# 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'