Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 55e9cca

Browse files
Adding infra generation pipeline template with keyvault enabled (#400)
* adding key vault guidance * updates to infra kv guidance * syntax update * adding permit access note * Pushing Keyvault generation pipeline * updating docs var names * update kv doc on versioning * updating docs * removing duplicate var set
1 parent 78ef90f commit 55e9cca

File tree

3 files changed

+168
-17
lines changed

3 files changed

+168
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
export EMAIL="[email protected]"
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'

azure-pipelines/templates/infra-generation-pipeline.yml

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ steps:
8181
# Clone Generated Repo and copy generated components over
8282
echo "Cloning Generated Repo: $GENERATED_REPO"
8383
git clone $GENERATED_REPO
84-
repo_url=$GENERATED_REPO
8584
8685
# Extract repo name from url
8786
repo_url=$GENERATED_REPO

guides/infra/spk-infra-generation-pipeline.md

+23-16
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,25 @@ name: "spk-infra-hld-vg-kv"
120120
description: "key vault variable group for infra hld"
121121
type: "AzureKeyVault"
122122
variables:
123-
ACCESS-TOKEN-SECRET:
123+
ACCESSTOKENSECRET:
124124
enabled: true
125-
ARM-CLIENT-ID:
125+
ARMCLIENTID:
126126
enabled: true
127-
ARM-CLIENT-SECRET:
127+
ARMCLIENTSECRET:
128128
enabled: true
129-
ARM-SUBSCRIPTION-ID:
129+
ARMSUBSCRIPTIONID:
130130
enabled: true
131-
ARM-TENANT-ID:
131+
ARMTENANTID:
132132
enabled: true
133133
CLUSTER:
134134
enabled: true
135-
GENERATED-REPO:
135+
GENERATEDREPO:
136136
enabled: true
137-
PROJECT-DIRECTORY:
137+
PROJECTDIRECTORY:
138138
enabled: true
139-
AZDO-ORG-NAME: (optional)
139+
AZDOORGNAME: (optional)
140140
enabled: true
141-
AZDO-PROJECT-NAME: (optional)
141+
AZDOPROJECTNAME: (optional)
142142
enabled: true
143143
key_vault_provider:
144144
name: "myvault" # name of the Azure Key Vaukt with Secrets
@@ -167,16 +167,23 @@ previously provisioned.
167167

168168
![](../images/kvsetupvg.png)
169169

170-
Additionally, be sure to select the respective Key secrets you wish to map to
171-
your variable group.
170+
Additionally, be sure to select the respective key secrets you wish to map to
171+
your variable group. Only the secret _names_ are mapped to the variable group,
172+
not the secret values. The latest version of the value of each secret is fetched
173+
from the vault and used in the pipeline linked to the variable group during the
174+
run.
172175

173176
![](../images/secrets-kv-vg.png)
174177

175-
> When using a variable group, you may be prompted to grant access permission to
176-
> all pipelines in order for your newly created pipeline to have access to an
177-
> agent pool and specific service connections. Be sure to navigate to the
178-
> Pipeline UI to permit permission to use the agent pool and the service
179-
> connection to authenticate against your key vault.
178+
A template generation pipeline that uses a variable group backed by key vault is
179+
provided in the
180+
[infra-generation-pipeline.yml](../../azure-pipelines/templates/infra-generation-kv-pipeline.yml)
181+
182+
> When using a variable group with key vault, you may be prompted to grant
183+
> access permission to all pipelines in order for your newly created pipeline to
184+
> have access to an agent pool and specific service connections in AzDO. Be sure
185+
> to navigate to the Pipeline UI to permit permissions to use the agent pool and
186+
> the service connection to authenticate against your key vault.
180187

181188
![](../images/permit_access.jpg)
182189

0 commit comments

Comments
 (0)