forked from InSciCo/LazyStackSMF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest_CodeBuild_Deploy.ps1
158 lines (131 loc) · 6.44 KB
/
Test_CodeBuild_Deploy.ps1
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
Write-Host "Test_CodeBuild_Deploy.ps1 - V1.0.0"
Write-Host "This script deploys two CodeBuild project stacks to a System Test Account."
Write-Host " - Test_CodeBuild_PR_Create.yaml defines the first CodeBuild project stack"
Write-Host " - Test_CodeBuild_PR_Merge.yaml defines the second CodeBuild project stack"
Write-Host "Each project is associated with a GitHub repository containing a serverless application stack."
Write-Host ""
Write-Host "Test_CodeBuild_PR_Create.yaml defines a CodeBuild project stack that builds, tests and publishes"
Write-Host "an application stack when a Pull Request is created or updated in the GitHub repository."
Write-Host "The published stack is called the PR stack. The stack is named based on the PR branch name."
Write-Host ""
Write-Host "Test_CodeBuild_PR_Merge.yaml defines a CodeBuild project stack that deletes an application stack"
Write-Host "when a Pull Request is merged in the GitHub application repository."
Write-Host ""
Write-Host "Note: Press return to accept a default values."
$LzOrgCode = (Read-Host "Enter your OrgCode")
$LzRegion = ""
do {
$LzMgmtProfile = "${LzOrgcode}Mgmt"
$LzMgmtProfileInput = (Read-Host "Enter your AWS CLI Management Account Profile (default: ${LzOrgCode}Mgmt)")
if($null -eq $LzMgmtProfileInput) {
$LzMgmtProfile = $LzMgmtProfileInput
}
$LzMgmtProfileKey = aws configure get profile.${LzMgmtProfile}.aws_access_key_id
if($LzMgmtProfileKey -eq "") {
Write-Host "Profile ${LzMgmtProfile} not found or not configured with Access Key"
$LzMgmtProfileExists = $false
}
else {
$LzMgmtProfileExists = $true
# Grab region in managment profile as default for new IAM User
$LzRegion = aws configure get profile.${LzMgmtProfile}.region
}
# Make sure LzMgmtProfile is associated with an IAM User in an Account belonging to an Organization
$null = aws organizations describe-organization --profile $LzMgmtProfile
if($? -eq $false) {
Write-Host "${LzMgmtProfile} profile is associated with an IAM User not administering an Organization."
Exit
}
}
until ($LzMgmtProfileExists)
if ($LzRegion -eq "") {
$LzRegion = "us-east-1"
}
$LzRegionInput = Read-Host "Enter Region (default ${LzRegion})"
if($LzRegionInput -ne "") {
$LzRegion = $LzRegionInput
}
do {
$LzSysCode = Read-Host "Enter the SysCode (ex: Tut)"
if($LzSysCode -eq "") {
Write-Host "System Code can't be empty. Please enter a value."
}
}
until ($LzSysCode -ne "")
$LzTestAcctName = "${LzOrgCode}${LzSysCode}Test"
$LzTestAcctNameInput = Read-Host "Enter the System Test Account Name (default: ${LzTestAcctName})"
if($LzTestAcctNameInput -ne "") {
$LzTestAcctName = $LzTestAcctNameInput
}
$LzTestAccessRoleProfile = $LzTestAcctName + "AccessRole"
$LzGitHubRepo = "https://github.com/myorg/myrepo.git"
$LzGitHubRepoInput = Read-Host "Enter the application stack's GitHub Repo URL (example: ${LzGitHubRepo})"
if($LzGitHubRepoInput -ne "") {
$LzGitHubRepo = $LzGitHubRepoInput
}
#extract "myrepo" from "https://github.com/myorg/myrepo.git"
$urlparts=$LzGitHubRepo.Split('/')
$LzRepoShortName=$urlparts[$urlparts.Count - 1]
$LzRepoShortName=$LzRepoShortName.Split('.')
$LzRepoShortName=$LzRepoShortName[0].ToLower()
$LzRepoShortNameInput = Read-Host "Enter your repo short name (default: ${LzRepoShortName})"
if($LzRepoShortNameInput -ne "") {
$LzRepoShortName = $LzRepoShortNameInput.ToLower()
}
$LzCodeBuild_PR_Create_StackName= "${LzRepoShortName}-t-pr-c"
$LzCodeBuild_PR_Merge_StackName="${LzRepoShortName}-t-pr-m"
$LzGitHubLzSmfUtilRepo = "https://github.com/myorg/myrepo.git"
$LzGitHubLzSmfUtilRepoInput = Read-Host "Enter the application stack's GitHub Repo URL (example: ${LzGitHubLzSmfRepo})"
if($LzGitHubLzSmfUtilRepoInput -ne "") {
$LzGitHubLzSmfUtilRepo = $LzGitHubLzSmfUtilRepoInput
}
do {
$LzCodeBuild_PR_Create = "Test_CodeBuild_PR_Create.yaml"
$LzCodeBuild_PR_Create_Input = Read-Host "Enter PR Create template name (default: ${LzCodeBuild_PR_Create})"
if("" -ne $LzCodeBuild_PR_Create_Input) {
$LzCodeBuild_PR_Create = $LzCodeBuild_PR_Create_Input
}
$LzFileFound = [System.IO.File]::Exists($LzCodeBuild_PR_Create)
if($false -eq $LzFileFound) {
Write-Host "That file was not found."
}
}
until ($true -eq $LzFileFound)
do {
$LzCodeBuild_PR_Merge = "Test_CodeBuild_PR_Merge.yaml"
$LzCodeBuild_PR_Merge_Input = Read-Host "Enter PR Merge template name (default: ${LzCodeBuild_PR_Merge})"
if("" -ne $LzCodeBuild_PR_Merge_Input) {
$LzCodeBuild_PR_Merge = $LzCodeBuild_PR_Merge_Input
}
$LzFileFound = [System.IO.File]::Exists($LzCodeBuild_PR_Merge)
if($false -eq $LzFileFound) {
Write-Host "That file was not found."
}
}
until ($true -eq $LzFileFound)
Write-Host "Please review and confirm the following:"
Write-Host " OrgCode: ${LzOrgCode}"
Write-Host " SysCode: ${LzSysCode}"
Write-Host " AWS CLI Management Account Profile: ${LzMgmtProfile}"
Write-Host " AWS Region: ${LzRegion}"
Write-Host " System Test Account name: ${LzTestAcctName}"
Write-Host " GitHub Repo URL: ${LzGitHubRepo}"
Write-Host " Repo short name: ${LzRepoShortName}"
Write-Host " GitHub LzGitHub Repo URL: ${LzGitHubLzSmfUtilRepo}"
Write-Host " CodeBuild PR Create project stack name: ${LzCodeBuild_PR_Create_StackName}"
Write-Host " CodeBuild PR Create project template: ${LzCodeBuild_PR_Create}"
Write-Host " CodeBuild PR Merge project stack name: ${LzCodeBuild_PR_Merge_StackName}"
Write-Host " CodeBuild PR Merge project template: ${LzCodeBuild_PR_Merge}"
$LzContinue = (Read-Host "Continue y/n")
if($LzContinue -ne "y") {
Write-Host "Exiting"
Exit
}
Write-Host ""
Write-Host "Processing Starting"
# Test Account
Write-Host "Deploying ${LzCodeBuild_PR_Create_StackName} AWS CodeBuild project to ${LzTestAcctName} account."
sam deploy --stack-name $LzCodeBuild_PR_Create_StackName -t $LzCodeBuild_PR_Create --capabilities CAPABILITY_NAMED_IAM --parameter-overrides GitHubRepoParam=$LzGitHubRepo GitHubLzSmfUtilRepoParam=$LzgitHubLzSmfUtilRepo --profile $LzTestAccessRoleProfile --region $LzRegion
Write-Host "Deploying ${LzCodeBuild_PR_Merge_StackName} AWS CodeBuild project to ${LzTestAcctName} account."
sam deploy --stack-name $LzCodeBuild_PR_Merge_StackName -t $LzCodeBuild_PR_Merge --capabilities CAPABILITY_NAMED_IAM --parameter-overrides GitHubRepoParam=$LzGitHubRepo --profile $LzTestAccessRoleProfile --region $LzRegion
Write-Host "Processing Complete"