-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathazdo-var-group.sh
executable file
·76 lines (70 loc) · 2.51 KB
/
azdo-var-group.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
VARGROUP_NAME="shared-secrets"
echo -e "\n\e[34m╔══════════════════════════════════════════╗"
echo -e "║\e[33m Configure Azure DevOps 🧶\e[34m ║"
echo -e "║\e[32m Create KeyVault linked variable group \e[34m ║"
echo -e "╚══════════════════════════════════════════╝"
# Load env vars from .env file
if [ ! -f ".env" ]; then
echo -e "\e[31m»»» 💥 Unable to find .env file, please create file and try again!"
exit
else
echo -e "\n\e[34m»»» 🧩 \e[96mLoading environmental variables\e[0m..."
export $(egrep -v '^#' ".env" | xargs)
fi
# Get project id from project name in AzDo
echo -e "\e[34m»»» 🔍 \e[96mGetting project ID from Azure DevOps\e[0m..."
PROJ_ID=$(curl -Ss -X GET "$TF_VAR_azdo_org_url/_apis/projects?api-version=6.1-preview.4" \
--user user:$TF_VAR_azdo_pat \
| jq -r ".value[] | select (.name == \"$TF_VAR_azdo_project_name\") | .id")
echo -e "\e[34m»»» 🔍 \e[96mGot ID: $PROJ_ID\e[0m"
# Get service connection id created in
SERVICE_CONN_ID=$(terraform output keyvault_access_connection_id)
KV_NAME=$(terraform output keyvault_name)
echo -e "\e[34m»»» 📌 \e[96mService connection ID: $SERVICE_CONN_ID\e[0m"
echo -e "\e[34m»»» 🔑 \e[96mKeyVault name: $KV_NAME\e[0m"
# Call REST API to create the variable group
# Note. Variable names must match the names of the secrets created in key-vault.tf
curl -Ss -X POST "$TF_VAR_azdo_org_url/$TF_VAR_azdo_project_name/_apis/distributedtask/variablegroups?api-version=6.1-preview.2" \
--user user:$TF_VAR_azdo_pat -H 'Content-Type: application/json; charset=utf-8' \
--data-binary @- << EOF
{
"name": "$VARGROUP_NAME",
"providerData": null,
"type": "AzureKeyVault",
"variables": {
"pipeline-sp-secret": {
"isSecret": true,
"value": null,
"enabled": true
},
"pipeline-sp-clientid": {
"isSecret": true,
"value": null,
"enabled": true
},
"azure-tenant-id": {
"isSecret": true,
"value": null,
"enabled": true
},
"azure-sub-id": {
"isSecret": true,
"value": null,
"enabled": true
}
},
"providerData": {
"serviceEndpointId": "$SERVICE_CONN_ID",
"vault": "$KV_NAME"
},
"variableGroupProjectReferences": [
{
"name": "$VARGROUP_NAME",
"projectReference": {
"id": "$PROJ_ID"
}
}
]
}
EOF