This repository was archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathinfra-generation-pipeline.yml
164 lines (141 loc) · 4.86 KB
/
infra-generation-pipeline.yml
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
trigger:
branches:
include:
- master
pool:
vmImage: 'ubuntu-latest'
variables:
- group: 'spk-infra-hld-vg'
steps:
- checkout: self
persistCredentials: true
clean: true
- bash: |
curl $BEDROCK_BUILD_SCRIPT > build.sh
chmod +x ./build.sh
displayName: Download Bedrock orchestration script
env:
BEDROCK_BUILD_SCRIPT: https://raw.githubusercontent.com/Microsoft/bedrock/master/gitops/azure-devops/build.sh
- script: |
dpkg --configure -a
curl -sL https://aka.ms/InstallAzureCLIDeb | bash
az extension add --name azure-devops
az --version
wget https://releases.hashicorp.com/terraform/$(tf_version)/terraform_$(tf_version)_linux_amd64.zip -q
unzip -q terraform_$(tf_version)_linux_amd64.zip
sudo mv terraform /usr/local/bin
terraform -version
displayName: 'Install az-cli, az devops extension, and Terraform'
- script: |
. build.sh --source-only
init
get_os_spk
get_spk_version
download_spk
echo 'SPK Version: '
spk --version
set -e
echo "GENERATING $PROJECT_DIRECTORY"
cd $PROJECT_DIRECTORY
spk infra generate -p $CLUSTER
env:
ARM_CLIENT_ID: $(ARM_CLIENT_ID)
ARM_CLIENT_SECRET: $(ARM_CLIENT_SECRET)
ARM_TENANT_ID: $(ARM_TENANT_ID)
ARM_SUBSCRIPTION_ID: $(ARM_SUBSCRIPTION_ID)
displayName: 'SPK'
- script: |
. build.sh --source-only
init
cd $PROJECT_DIRECTORY-generated/$CLUSTER
terraform init -backend-config=./backend.tfvars
displayName: 'Terraform init'
- script: |
. build.sh --source-only
init
cd $PROJECT_DIRECTORY-generated/$CLUSTER
terraform plan -var-file=./spk.tfvars
env:
ARM_CLIENT_ID: $(ARM_CLIENT_ID)
ARM_CLIENT_SECRET: $(ARM_CLIENT_SECRET)
ARM_TENANT_ID: $(ARM_TENANT_ID)
ARM_SUBSCRIPTION_ID: $(ARM_SUBSCRIPTION_ID)
displayName: 'Terraform plan'
- script: |
# Exit on error
set -e
# Retrieve most recent commit hash from HLD repo
commit_hash=$(git rev-parse HEAD | cut -c1-7)
echo "Commit Hash: $commit_hash"
# Clone Generated Repo and copy generated components over
echo "Cloning Generated Repo: $GENERATED_REPO"
git clone $GENERATED_REPO
repo_url=$GENERATED_REPO
# Extract repo name from url
repo=${repo_url##*/}
repo_name=${repo%.*}
cd "$repo_name"
git pull
rsync -rv --exclude=.terraform $HOME/$PROJECT_DIRECTORY-generated .
# Check if PR branch already exists:
pr_list=$(git ls-remote origin)
if [[ $pr_list == *"pr-$commit_hash"* ]]; then
echo "PR Branch already exist. Iterating..."
count=$(git ls-remote origin | grep "pr-$commit_hash" | wc -l | tr -d " ")
count=$((count + 1))
PR_BRANCH_NAME=pr-$commit_hash-$count
echo "PR BRANCH NAME: $PR_BRANCH_NAME"
git checkout -b $PR_BRANCH_NAME
else
echo "PR Branch does not exist. Creating a new PR Branch"
PR_BRANCH_NAME=pr-$commit_hash-1
echo "PR BRANCH NAME: $PR_BRANCH_NAME"
git checkout -b $PR_BRANCH_NAME
fi
# Set git identity
git config user.email "[email protected]"
git config user.name "Automated Account"
# Following variables have to be set for TeamCity
export GIT_AUTHOR_NAME="Automated Account"
export GIT_COMMITTER_NAME="Automated Account"
export EMAIL="[email protected]"
# Format Terraform files
terraform fmt
# Add generated files to repository
git status
git add .
git commit -m "Adding generated components for $PROJECT_DIRECTORY/$CLUSTER"
# Create a new branch
PR_BRANCH_NAME=pr_$commit_hash
git checkout -b $PR_BRANCH_NAME
# Git Push
repo_url=$GENERATED_REPO
repo_url="${repo_url#http://}"
repo_url="${repo_url#https://}"
echo "$repo_url"
echo "GIT PUSH: https://<ACCESS_TOKEN_SECRET>@$repo_url"
git push "https://$ACCESS_TOKEN_SECRET@$repo_url" $PR_BRANCH_NAME
# Create PR
# If using GitHub repos:
echo "CREATE PULL REQUEST"
if [[ $GENERATED_REPO == *"github"* ]]; then
echo "Installing Hub"
sudo add-apt-repository ppa:cpick/hub
sudo apt-get update
sudo apt-get install hub
export GITHUB_TOKEN=$ACCESS_TOKEN_SECRET
hub pull-request -m "Adding Generated Components for $PROJECT_DIRECTORY/$CLUSTER"
elif [[ $GENERATED_REPO == *"azure"* ]] || [[ $GENERATED_REPO == *"visualstudio"* ]]; then
# If using AzDo repos:
# You will need to specify variables $AZDO_ORG_NAME and $AZDO_PROJECT_NAME
export AZURE_DEVOPS_EXT_PAT=$ACCESS_TOKEN_SECRET
az devops configure --defaults organization=$AZDO_ORG_NAME project=$AZDO_PROJECT_NAME
echo "Making pull request for $PR_BRANCH_NAME against master"
az repos pr create --description "Automated PR for $PR_BRANCH_NAME against master"
else
echo "Unable to create pull request."
exit 1
fi
env:
ACCESS_TOKEN_SECRET: $(ACCESS_TOKEN_SECRET)
displayName: 'Commit and Push to Generated Repository'