Created
February 24, 2021 16:40
-
-
Save alwalker/bcaed938ff91ad8fae2f852a9fab05c0 to your computer and use it in GitHub Desktop.
A script to merge the variables for two environments in an Azure DevOps release pipeline.
This file contains 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
#!/usr/bin/env bash | |
set -e | |
#Check Arguments | |
if [ $# -lt 3 ]; then | |
echo "Insuffecient arguments supplied. Shoudl be: ado_copy_relase_vars.sh src dest RELEASE_ID" | |
exit 1 | |
fi | |
SOURCE_ENV=$1 | |
DEST_ENV=$2 | |
RELEASE_ID=$3 | |
GET_URI="https://vsrm.dev.azure.com/[ORG]/[PROJECT]/_apis/release/definitions/$RELEASE_ID" | |
PUT_URI="https://vsrm.dev.azure.com/[ORG]/[PROJECT]/_apis/release/definitions?api-version=6.0" | |
echo "Attemping to merge variables from $SOURCE_ENV to $DEST_ENV on Release $RELEASE_ID" | |
#Get Personal Access Token | |
echo -n "PAT: " | |
read -s PAT | |
echo | |
if [ -z "$PAT" ]; then | |
echo "Please provide a personal access token" | |
exit 1 | |
fi | |
echo "Checking PAT..." | |
RETURN_CODE=$(curl -s -L --user ":$PAT" -o /dev/null -w "%{http_code}" $GET_URI) | |
if [ $RETURN_CODE -eq 400 ]; then | |
echo "Invalid Release ID" | |
echo "Exiting..." | |
exit 1 | |
fi | |
if [ $RETURN_CODE -eq 404 ]; then | |
echo "Release ID does not exist" | |
echo "Exiting..." | |
exit 1 | |
fi | |
if [ $RETURN_CODE -ne 200 ]; then | |
echo "Invalid PAT" | |
echo "Exiting..." | |
exit 1 | |
fi | |
#Check environments exist | |
echo "Getting existing release..." | |
CURRENT_RELEASE=$(curl -s -L --user ":$PAT" $GET_URI) | |
if [ -z "$CURRENT_RELEASE" ]; then | |
echo "No release found for $RELEASE_ID" | |
exit 1 | |
fi | |
SOURCE_VARS=$(echo $CURRENT_RELEASE | jq --arg env "$SOURCE_ENV" '.environments[] | select(.name==$env) | .variables') | |
if [ -z "$SOURCE_VARS" ]; then | |
echo "Source environment doesn't exist" | |
exit 1 | |
fi | |
DEST_VARS=$(echo $CURRENT_RELEASE | jq --arg env "$DEST_ENV" '.environments[] | select(.name==$env) | .variables') | |
if [ -z "$DEST_VARS" ]; then | |
echo "Destination environment doesn't exist" | |
exit 1 | |
fi | |
#Merge two environments | |
MERGED_VARIABLES=$(echo $SOURCE_VARS $DEST_VARS | jq -s '.[0] * .[1]') | |
NEW_ENV=$(echo $CURRENT_RELEASE | jq --arg env "$DEST_ENV" --argjson variables "$MERGED_VARIABLES" '.environments[] | select(.name==$env) | . + {variables: $variables}') | |
#Build new release version json | |
NEW_RELEASE=$(echo $CURRENT_RELEASE | \ | |
jq --arg env "$DEST_ENV" 'del(.environments[] | select(.name==$env))' | \ | |
jq --argjson env "$NEW_ENV" '.environments += [$env]' \ | |
) | |
#Upload new release | |
echo "Uploading new release" | |
curl -s -X PUT \ | |
--user ":$PAT" \ | |
-H "Content-Type:application/json" \ | |
--data "$NEW_RELEASE" \ | |
$PUT_URI | |
echo "Done" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment