Skip to content

Instantly share code, notes, and snippets.

@frg
Created May 1, 2023 22:28
Show Gist options
  • Save frg/8c6b50b2e8fdc858b82c5b197b0c0404 to your computer and use it in GitHub Desktop.
Save frg/8c6b50b2e8fdc858b82c5b197b0c0404 to your computer and use it in GitHub Desktop.
Get Octopus Deployment Details from Project and Release inc. from git commit

Octopus Deploy Details

This script fetches deployment details for a specific release version of a project in Octopus Deploy.

Usage

./octopus_deploy_details.sh \
    --octopus-url <octopus_url> \
    --api-key <api_key> \
    --project-name <project_name> \
    --release-version <release_version> \
    [--no-headers] /

Required Arguments

  • --octopus-url/-u <octopus_url>: The Octopus Deploy instance URL.
  • --api-key/-k <api_key>: The API key.
  • --project-name/-p <project_name>: The project name.
  • --release-version/-r <release_version>: The release version.

Optional Arguments

  • --no-headers: Exclude the deployment header and project/release details from the output.

Identifying Deployment Date of Commit

Using the git tag --contains <commit-hash> command we're able to find all the tags in your Git repository that include a specific commit. This is useful when you need to determine which versions or releases of a project contain a particular change or bug fix.

For example, if a CI/CD setup creates a git tag with the template <environment>-<version>, and names the Octopus release with the same (or similar) naming convention we can do the following steps to get the information of all deployments that contain a given commit hash.

  1. Get all tags containing a commit git tag --contains <commit-hash>
  2. Filter git tags to only include build tags using grep -P '^(prod-|stage-|dev-)'
  3. Remove environment prefix (if needed) using sed 's/^\(prod\|stage\|dev\)-//'
  4. Loop the output and pass value as release version to octopus_deploy_details.sh

Example

git tag --contains <commit-hash> \
    | grep -P '^(prod-|stage-|dev-)' \
    | sed 's/^\(prod\|stage\|dev\)-//' \
    | while read -r release; do ./octopus_deploy_details.sh \
        -u <octopus_url> \
        -k <api_key> \
        -p <project_name> \
        -r "$release"; \
    done /
#!/bin/bash
# Set default values
exclude_headers=false
# Parse named arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-u|--octopus-url)
octopus_url="$2"
shift 2
;;
-k|--api-key)
api_key="$2"
shift 2
;;
-p|--project-name)
project_name="$2"
shift 2
;;
-r|--release-version)
release_version="$2"
shift 2
;;
--no-headers)
exclude_headers=true
shift
;;
*)
echo "Invalid option: $1" >&2
exit 1
;;
esac
done
# Check if required arguments are provided
if [ -z "$octopus_url" ] || [ -z "$api_key" ] || [ -z "$project_name" ] || [ -z "$release_version" ]; then
echo "Usage: $0 --octopus-url <octopus_url> --api-key <api_key> --project-name <project_name> --release-name <release_version> [--no-headers]"
exit 1
fi
# Get project details
project_response=$(curl -s -w "\n%{http_code}" -X GET "${octopus_url}/api/projects/all" -H "X-Octopus-ApiKey: ${api_key}")
project_http_code=$(echo "$project_response" | tail -n1)
project_id=$(echo "$project_response" | head -n-1 | jq -r ".[] | select(.Name == \"${project_name}\") | .Id")
if [ -z "$project_id" ] || [ "$project_http_code" = "404" ]; then
echo "Error: Project '${project_name}' not found."
exit 1
fi
# Get release details
release_response=$(curl -s -w "\n%{http_code}" -X GET "${octopus_url}/api/projects/${project_id}/releases/${release_version}" -H "X-Octopus-ApiKey: ${api_key}")
release_http_code=$(echo "$release_response" | tail -n1)
release_id=$(echo "$release_response" | head -n-1 | jq -r ".Id")
if [ -z "$release_id" ] || [ "$release_http_code" = "404" ]; then
echo "Error: Release '${release_version}' not found in project '${project_name}'."
exit 1
fi
# Output project and release details (conditionally)
if [ "$exclude_headers" = false ]; then
echo "Project Name: ${project_name}"
echo "Project ID: ${project_id}"
echo "Release Version: ${release_version}"
echo "Release ID: ${release_id}"
fi
# Get deployment details
deployments=$(curl -s -X GET "${octopus_url}/api/releases/${release_id}/deployments" -H "X-Octopus-ApiKey: ${api_key}")
# Print deployment details
if [ "$exclude_headers" = false ]; then
echo "Release Version | Date Time | Environment Name | Tenant Name"
fi
deployments_length=$(echo "$deployments" | jq -r ".Items | length")
for ((i = 0; i < deployments_length; i++)); do
datetime=$(echo "$deployments" | jq -r ".Items[${i}].Created")
environment_id=$(echo "$deployments" | jq -r ".Items[${i}].EnvironmentId")
environment_name=$(curl -s -X GET "${octopus_url}/api/environments/${environment_id}" -H "X-Octopus-ApiKey: ${api_key}" | jq -r ".Name")
tenant_id=$(echo "$deployments" | jq -r ".Items[${i}].TenantId")
if [ "$tenant_id" != "null" ]; then
tenant_name=$(curl -s -X GET "${octopus_url}/api/tenants/${tenant_id}" -H "X-Octopus-ApiKey: ${api_key}" | jq -r ".Name")
else
tenant_name="No Tenant"
fi
echo "${release_version} | ${datetime} | ${environment_name} | ${tenant_name}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment