-
Notifications
You must be signed in to change notification settings - Fork 4
/
az_devops_create_pipeline.sh
executable file
·176 lines (154 loc) · 5.29 KB
/
az_devops_create_pipeline.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
SCRIPT_VERSION=0.1.0
# Required external tools to be available on PATH.
REQUIRED_TOOLS=("az")
LOG_LEVELS=([0]="emerg" [1]="alert" [2]="crit" [3]="err" [4]="warning" [5]="notice" [6]="info" [7]="debug")
function .log() {
local LEVEL=${1}
shift
if [ ${__VERBOSE} -ge ${LEVEL} ]; then
if [ ${LEVEL} -ge 3 ]; then
echo "[${LOG_LEVELS[$LEVEL]}]" "$@" 1>&2
else
echo "[${LOG_LEVELS[$LEVEL]}]" "$@"
fi
fi
}
__VERBOSE=${__VERBOSE:=6}
function check_tools() {
local tools=("$@")
local errors_count=0
for cmd in "${tools[@]}"
do
if ! [[ -x "$(command -v ${cmd})" ]]; then
.log 3 "${cmd} is required and was not found in PATH."
errors_count=$((errors_count + 1))
else
.log 6 "Found '${cmd}' in path"
fi
done
if [ ${errors_count} -gt 0 ]; then
exit 1
fi
}
function usage() {
echo ""
echo "Usage: $0 [-t <pat_token>] -p <pipeline_name> -f <yaml_file> [-h]" 1>&2
echo "Version: ${SCRIPT_VERSION}"
echo ""
echo "Options"
echo "-t <pat_token> Personal Access Token that should be used for creating the pipeline."
echo " OR: AZURE_DEVOPS_CLI_PAT environment variable."
echo "-o <organization> Azure DevOps org, e.g. 'https://dev.azure.com/contoso/'"
echo " OR: AZURE_DEVOPS_ORGANIZATION environment variable."
echo "-p <project_name> Name of the Azure DevOps project, e.g. 'MyProject'."
echo " OR: AZURE_DEVOPS_PROJECT environment variable."
echo "-x <prefix> Short 2-10 letters prefix for the project, e.g. 'proj'."
echo " OR: TF_VAR_prefix environment variable."
echo "-g <git_repo_name> Name of the Azure DevOps git repo, e.g. 'myrepo'."
echo " OR: AZURE_DEVOPS_GIT_REPO environment variable."
echo "-n <pipeline_name> Name of the pipeline to create, e.g. 'MyPipeline'."
echo "-f <yaml_file> YAML definition location relative to the repository root, e.g. './pipeline.yaml'."
echo "-c <service_connection> The service connection name that should be used."
echo " OR: AZURE_DEVOPS_SERVICE_CONNECTION_NAME environment variable."
echo "-h Help: Print this dialog and exit."
echo ""
exit 1
}
# =============================================
# Check Options
t=${AZURE_DEVOPS_CLI_PAT:=""}
o=${AZURE_DEVOPS_ORGANIZATION:=""}
p=${AZURE_DEVOPS_PROJECT:=""}
x=${TF_VAR_prefix:=""}
g=${AZURE_DEVOPS_GIT_REPO:=""}
c=${AZURE_DEVOPS_SERVICE_CONNECTION_NAME:=""}
n=""
f=""
while getopts ":t:o:p:x:g:n:f:c:h" z; do
case "${z}" in
t)
t=${OPTARG}
;;
o)
o=${OPTARG}
;;
p)
p=${OPTARG}
;;
x)
x=${OPTARG}
;;
g)
g=${OPTARG}
;;
n)
n=${OPTARG}
;;
f)
f=${OPTARG}
;;
c) c=${OPTARG}
;;
h)
usage
;;
*)
usage
;;
esac
done
shift $((OPTIND - 1))
opt_errors_count=0
if [ -z "${t}" ]; then
.log 3 "PAT Token Missing"
opt_errors_count=$((opt_errors_count + 1))
fi
if [ -z "${o}" ]; then
.log 3 "Organization missing."
opt_errors_count=$((opt_errors_count + 1))
fi
if [ -z "${p}" ]; then
.log 3 "Project name missing."
opt_errors_count=$((opt_errors_count + 1))
fi
if [ -z "${x}" ]; then
.log 3 "Prefix is missing."
opt_errors_count=$((opt_errors_count + 1))
fi
if [ -z "${g}" ]; then
.log 3 "Git Repo name missing."
opt_errors_count=$((opt_errors_count + 1))
fi
if [ -z "${n}" ]; then
.log 3 "Pipeline Name missing."
opt_errors_count=$((opt_errors_count + 1))
fi
if [ -z "${f}" ]; then
.log 3 "YAML file missing."
opt_errors_count=$((opt_errors_count + 1))
fi
if [ -z "${c}" ]; then
.log 3 "Service Connection Name is missing."
opt_errors_count=$((opt_errors_count + 1))
fi
if [ ${opt_errors_count} -gt 0 ]; then
usage
fi
.log 6 "[==== Check Required Tools ====]"
.log 6 "Found 'bash' (version: ${BASH_VERSION})"
check_tools "${REQUIRED_TOOLS[@]}"
.log 6 "[==== Login to Azure DevOps ====]"
echo ${t} | az devops login
.log 6 "[==== Creating Pipeline ${n}... (first run is *not* triggered) ====]"
az devops configure --defaults organization="${o}" project="${p}" --use-git-aliases true
# NOTE: There is a bug in the YAML variable group authorization path:
# https://developercommunity.visualstudio.com/content/problem/729324/variables-group-in-yml-cannot-be-authorized-from-p.html
# As there is no sensible information in the variable groups we can safely use '--authorize true' for the moment.
az pipelines variable-group create --name "IaC_Shared_Variables" --authorize true --variables TF_VAR_prefix=${x} Service_Connection_Name="${c}"
az pipelines variable-group create --name "IaC_Terraform_Backend_Variables" --authorize true --variables __TF_backend_resource_group_name= __TF_backend_location= __TF_backend_storage_account_name= __TF_backend_storage_container_name=
az pipelines create --name "${n}" --repository "${g}" --branch master --yml-path "${f}" --repository-type tfsgit --skip-first-run
.log 6 "[==== All done. ====]"