Skip to content

Instantly share code, notes, and snippets.

@GTRekter
Last active April 13, 2023 06:19
Show Gist options
  • Save GTRekter/c7846f526f32fcdb412d90d0d9690d8c to your computer and use it in GitHub Desktop.
Save GTRekter/c7846f526f32fcdb412d90d0d9690d8c to your computer and use it in GitHub Desktop.
The script is designed to automate the process of creating an Azure Resource Manager (Azurerm) and GitHub service connection. The script first checks if the service connections already exist, and if they do not, it creates them.
PAT=""
ORG_NAME="ivanporta"
PROJECT_NAME="Sample"
DEFAULT_JSON='{
"pipeline": {
"service_endpoints": [
{
"azurerm": [
{
"name": "Azure",
"tenant_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"subscription_name": "",
"subscription_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"service_principal_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
],
"github": [
{
"name": "GitHub",
"url": "https://github.com/xxxx"
}
]
}
]
}
}'
echo "Create service endpoints in $PROJECT_NAME project"
echo "Read organization ID. This property is needed to get a list of service endpoints"
RESPONSE=$(curl --silent \
--write-out "\n%{http_code}" \
--header "Authorization: Basic $(echo -n :$PAT | base64)" \
--header "Content-Type: application/json" \
--data-raw '{"contributionIds": ["ms.vss-features.my-organizations-data-provider"],"dataProviderContext":{"properties":{}}}' \
"https://dev.azure.com/$ORG_NAME/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1")
HTTP_STATUS=$(tail -n1 <<< "$RESPONSE")
RESPONSE_BODY=$(sed '$ d' <<< "$RESPONSE")
if [ $HTTP_STATUS != 200 ]; then
echo "Failed to get the list of existing service endpoints. $RESPONSE"
exit 1;
else
echo "The list of existing service endpoints was succesfully retrieved"
fi
ORG_ID=$(echo "$RESPONSE_BODY" | jq '.dataProviders."ms.vss-features.my-organizations-data-provider".organizations[] | select(.name == "'"$ORG_NAME"'") | .id' | tr -d '"')
echo "The ID of the $ORG_NAME organization is $ORG_ID"
echo "Read the list of existing service endpoints"
RESPONSE=$(curl --silent \
--request POST \
--write-out "\n%{http_code}" \
--header "Authorization: Basic $(echo -n :$PAT | base64)" \
--header "Content-Type: application/json" \
--data-raw '{"contributionIds":["ms.vss-distributed-task.resources-hub-query-data-provider"],"dataProviderContext":{"properties":{"resourceFilters":{"createdBy":[],"resourceType":[],"searchText":""},"sourcePage":{"url":"https://dev.azure.com/'$ORG_NAME'/'$PROJECT_NAME'/_settings/adminservices","routeId":"ms.vss-admin-web.project-admin-hub-route","routeValues":{"project":"Sample","adminPivot":"adminservices","controller":"ContributedPage","action":"Execute","serviceHost":"'$ORG_ID' ('$ORG_NAME')"}}}}}' \
"https://dev.azure.com/$ORG_NAME/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1")
HTTP_STATUS=$(tail -n1 <<< "$RESPONSE")
SERVICE_ENDPOINT_LIST_RESPONSE_BODY=$(sed '$ d' <<< "$RESPONSE")
if [ $HTTP_STATUS != 200 ]; then
echo "Failed to get the list of existing service endpoints. $RESPONSE"
exit 1;
else
echo "The list of existing service endpoints was succesfully retrieved"
fi
for SERVICE_ENDPOINT in $(echo "$DEFAULT_JSON" | jq -r '.pipeline.service_endpoints[] | @base64'); do
SERVICE_ENDPOINT_JSON=$(echo "$SERVICE_ENDPOINT" | base64 --decode | jq -r '.')
echo "Creating Azure service endpoint"
for AZURE_SERVICE_ENDPOINT in $(echo "$SERVICE_ENDPOINT_JSON" | jq -r '.azurerm[] | @base64'); do
AZURE_SERVICE_ENDPOINT_JSON=$(echo "$AZURE_SERVICE_ENDPOINT" | base64 --decode | jq -r '.')
NAME=$(echo "$AZURE_SERVICE_ENDPOINT_JSON" | jq -r '.name')
TENANT_ID=$(echo "$AZURE_SERVICE_ENDPOINT_JSON" | jq -r '.tenant_id')
SUBSCRIPTION_ID=$(echo "$AZURE_SERVICE_ENDPOINT_JSON" | jq -r '.subscription_id')
SUBSCRIPTION_NAME=$(echo "$AZURE_SERVICE_ENDPOINT_JSON" | jq -r '.subscription_name')
SERVICE_PRINCIPAL_ID=$(echo "$AZURE_SERVICE_ENDPOINT_JSON" | jq -r '.service_principal_id')
echo "Checking if $NAME service endpoint already exists"
if [ $(echo "$SERVICE_ENDPOINT_LIST_RESPONSE_BODY" | jq '.dataProviders."ms.vss-distributed-task.resources-hub-query-data-provider".resourceItems[] | select(.name == "'"$NAME"'") | length') -gt 0 ]; then
echo "$NAME service endpoint already exists. Skipping..."
continue
else
echo "$NAME service endpoint does not exist."
fi
echo "Creating $NAME service endpoint"
RESPONSE=$(az devops service-endpoint azurerm create --azure-rm-service-principal-id "$SERVICE_PRINCIPAL_ID" --azure-rm-subscription-id "$SUBSCRIPTION_ID" --azure-rm-subscription-name "$SUBSCRIPTION_NAME" --azure-rm-tenant-id "$TENANT_ID" --name "$NAME" --organization "https://dev.azure.com/$ORG_NAME" --project "$PROJECT_NAME" --output json)
if [ $? -eq 0 ]; then
echo "The $NAME service endpoint was successfully created"
else
echo "Error during the creation of the $NAME service endpoint"
exit 1
fi
done
for GITHUB_SERVICE_ENDPOINT in $(echo "$SERVICE_ENDPOINT_JSON" | jq -r '.github[] | @base64'); do
GITHUB_SERVICE_ENDPOINT_JSON=$(echo "$GITHUB_SERVICE_ENDPOINT" | base64 --decode | jq -r '.')
NAME=$(echo "$GITHUB_SERVICE_ENDPOINT_JSON" | jq -r '.name')
URL=$(echo "$GITHUB_SERVICE_ENDPOINT_JSON" | jq -r '.url')
echo "Checking if $NAME service endpoint already exists"
if [[ $(echo "$SERVICE_ENDPOINT_LIST_RESPONSE_BODY" | jq '.dataProviders."ms.vss-distributed-task.resources-hub-query-data-provider".resourceItems[] | select(.name == "'"$NAME"'") | length') -gt 0 ]]; then
echo "$NAME service endpoint already exists. Skipping..."
continue
else
echo "$NAME service endpoint does not exist."
fi
echo "Creating $NAME service endpoint"
RESPONSE=$(az devops service-endpoint github create --github-url "$URL" --name "$NAME" --organization "https://dev.azure.com/$ORG_NAME" --project "$PROJECT_NAME" --output json)
if [ $? -eq 0 ]; then
echo "The $NAME service endpoint was successfully created"
else
echo "Error during the creation of the $NAME service endpoint"
exit 1
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment