1
+ trigger :
2
+ branches :
3
+ include :
4
+ - master
5
+
6
+ pool :
7
+ vmImage : ' ubuntu-latest'
8
+
9
+ variables :
10
+ - group : ' spk-infra-hld-vg-kv'
11
+
12
+ steps :
13
+ - checkout : self
14
+ persistCredentials : true
15
+ clean : true
16
+
17
+ - bash : |
18
+ curl $BEDROCK_BUILD_SCRIPT > build.sh
19
+ chmod +x ./build.sh
20
+ displayName : Download Bedrock orchestration script
21
+ env :
22
+ BEDROCK_BUILD_SCRIPT : https://raw.githubusercontent.com/Microsoft/bedrock/master/gitops/azure-devops/build.sh
23
+
24
+ - script : |
25
+ dpkg --configure -a
26
+ curl -sL https://aka.ms/InstallAzureCLIDeb | bash
27
+ az extension add --name azure-devops
28
+ az --version
29
+ wget https://releases.hashicorp.com/terraform/$(tf_version)/terraform_$(tf_version)_linux_amd64.zip -q
30
+ unzip -q terraform_$(tf_version)_linux_amd64.zip
31
+ sudo mv terraform /usr/local/bin
32
+ terraform -version
33
+ displayName : ' Install az-cli, az devops extension, and Terraform'
34
+
35
+ - script : |
36
+ . build.sh --source-only
37
+ init
38
+ get_os_spk
39
+ get_spk_version
40
+ download_spk
41
+ echo 'SPK Version: '
42
+ spk --version
43
+ set -e
44
+ echo "GENERATING $(PROJECTDIRECTORY)"
45
+ cd $(PROJECTDIRECTORY)
46
+ spk infra generate -p $(CLUSTER)
47
+ env :
48
+ ARM_CLIENT_ID : $(ARMCLIENTID)
49
+ ARM_CLIENT_SECRET : $(ARMCLIENTSECRET)
50
+ ARM_TENANT_ID : $(ARMTENANTID)
51
+ ARM_SUBSCRIPTION_ID : $(ARMSUBSCRIPTIONID)
52
+ displayName : ' SPK'
53
+
54
+ - script : |
55
+ . build.sh --source-only
56
+ init
57
+ cd $(PROJECTDIRECTORY)-generated/$(CLUSTER)
58
+ terraform init -backend-config=./backend.tfvars
59
+ displayName : ' Terraform init'
60
+
61
+ - script : |
62
+ . build.sh --source-only
63
+ init
64
+ cd $(PROJECTDIRECTORY)-generated/$(CLUSTER)
65
+ terraform plan -var-file=./spk.tfvars
66
+ env :
67
+ ARM_CLIENT_ID : $(ARMCLIENTID)
68
+ ARM_CLIENT_SECRET : $(ARMCLIENTSECRET)
69
+ ARM_TENANT_ID : $(ARMTENANTID)
70
+ ARM_SUBSCRIPTION_ID : $(ARMSUBSCRIPTIONID)
71
+ displayName : ' Terraform plan'
72
+
73
+ - script : |
74
+ # Exit on error
75
+ set -e
76
+
77
+ # Retrieve most recent commit hash from HLD repo
78
+ commit_hash=$(git rev-parse HEAD)
79
+ echo "Commit Hash: $commit_hash"
80
+
81
+ # Clone Generated Repo and copy generated components over
82
+ echo "Cloning Generated Repo: $(GENERATEDREPO)"
83
+ git clone $(GENERATEDREPO)
84
+
85
+ # Extract repo name from url
86
+ repo_url=$(GENERATEDREPO)
87
+ repo=${repo_url##*/}
88
+ repo_name=${repo%.*}
89
+
90
+ cd "$repo_name"
91
+ rsync -rv --exclude=.terraform $HOME/$(PROJECTDIRECTORY)-generated .
92
+
93
+ # Set git identity
94
+ git config user.email "[email protected] "
95
+ git config user.name "Automated Account"
96
+
97
+ # Following variables have to be set for TeamCity
98
+ export GIT_AUTHOR_NAME="Automated Account"
99
+ export GIT_COMMITTER_NAME="Automated Account"
100
+
101
+
102
+ # Format Terraform files
103
+ terraform fmt
104
+
105
+ # Add generated files to repository
106
+ git status
107
+ git add .
108
+ git commit -m "Adding generated components for $(PROJECTDIRECTORY)/$(CLUSTER)"
109
+
110
+ # Create a new branch
111
+ PR_BRANCH_NAME=pr_$commit_hash
112
+ git checkout -b $PR_BRANCH_NAME
113
+
114
+ # Git Push
115
+ repo_url=$(GENERATEDREPO)
116
+ repo_url="${repo_url#http://}"
117
+ repo_url="${repo_url#https://}"
118
+ echo "$repo_url"
119
+ echo "GIT PUSH: https://<ACCESS_TOKEN_SECRET>@$repo_url"
120
+ git push "https://$ACCESS_TOKEN_SECRET@$repo_url" $PR_BRANCH_NAME
121
+
122
+ # Create PR
123
+ # If using GitHub repos:
124
+ echo "CREATE PULL REQUEST"
125
+ if [[ $(GENERATEDREPO) == *"github"* ]]; then
126
+ echo "Installing Hub"
127
+ sudo add-apt-repository ppa:cpick/hub
128
+ sudo apt-get update
129
+ sudo apt-get install hub
130
+ export GITHUB_TOKEN=$ACCESS_TOKEN_SECRET
131
+ hub pull-request -m "Adding Generated Components for $PROJECT_DIRECTORY/$CLUSTER"
132
+ elif [[ $(GENERATEDREPO) == *"azure"* ]] || [[ $(GENERATEDREPO) == *"visualstudio"* ]]; then
133
+ # If using AzDo repos:
134
+ # You will need to specify variables $AZDO-ORG-NAME and $AZDO-PROJECT-NAME
135
+ export AZURE_DEVOPS_EXT_PAT=$ACCESS_TOKEN_SECRET
136
+ az devops configure --defaults organization=$(AZDOORGNAME) project=$(AZDOPROJECTNAME)
137
+ echo "Making pull request for $PR_BRANCH_NAME against master"
138
+ az repos pr create --description "Automated PR for $PR_BRANCH_NAME against master"
139
+ else
140
+ echo "Unable to create pull request."
141
+ exit 1
142
+ fi
143
+ env :
144
+ ACCESS_TOKEN_SECRET : $(ACCESSTOKENSECRET)
145
+ displayName : ' Commit and Push to Generated Repository'
0 commit comments