-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazureScripts.ps1
76 lines (60 loc) · 3.15 KB
/
azureScripts.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
# This script sets up Azure resources
# Connect to Azure account
Connect-AzAccount
# Get the Azure subscription context
$context = Get-AzSubscription -SubscriptionName PAYG-Sandboxes
# Set the Azure context to the specified subscription
Set-AzContext $context
# Set the default resource group
Set-AzDefault -ResourceGroupName rg_sb_eastus_89803_1_171259668489
##change resourse group name
# Set the GitHub organization and repository names
$githubOrganizationName = 'nenad0707'
$githubRepositoryName = 'TodoApp'
# Create a new Azure AD application
$testApplicationRegistration = New-AzADApplication -DisplayName 'TodoApp-Test'
# Create a new Azure AD application federated credential
New-AzADAppFederatedCredential `
-Name 'TodoApp-Test' `
-ApplicationObjectId $testApplicationRegistration.Id `
-Issuer 'https://token.actions.githubusercontent.com' `
-Audience 'api://AzureADTokenExchange' `
-Subject "repo:$($githubOrganizationName)/$($githubRepositoryName):environment:Test"
# Create a new Azure AD application federated credential for the TodoApp-test-branch
New-AzADAppFederatedCredential `
-Name 'TodoApp-test-branch' `
-ApplicationObjectId $testApplicationRegistration.Id `
-Issuer 'https://token.actions.githubusercontent.com' `
-Audience 'api://AzureADTokenExchange' `
-Subject "repo:$($githubOrganizationName)/$($githubRepositoryName):ref:refs/heads/main"
# Get the resource group
$productionApplicationRegistration = New-AzADApplication -DisplayName 'TodoApp-Production'
New-AzADAppFederatedCredential `
-Name 'TodoApp-Production' `
-ApplicationObjectId $productionApplicationRegistration.Id `
-Issuer 'https://token.actions.githubusercontent.com' `
-Audience 'api://AzureADTokenExchange' `
-Subject "repo:$($githubOrganizationName)/$($githubRepositoryName):environment:Production"
New-AzADAppFederatedCredential `
-Name 'TodoApp-production-branch' `
-ApplicationObjectId $productionApplicationRegistration.Id `
-Issuer 'https://token.actions.githubusercontent.com' `
-Audience 'api://AzureADTokenExchange' `
-Subject "repo:$($githubOrganizationName)/$($githubRepositoryName):ref:refs/heads/main"
$testResourceGroup = Get-AzResourceGroup -Name rg_sb_southeastasia_89803_3_171259668780 ##change resourse group name
New-AzADServicePrincipal -AppId $($testApplicationRegistration.AppId)
New-AzRoleAssignment `
-ApplicationId $($testApplicationRegistration.AppId) `
-RoleDefinitionName Contributor `
-Scope $($testResourceGroup.ResourceId)
$productionResourceGroup = Get-AzResourceGroup -Name rg_sb_eastus_89803_1_171259668489 ##change resourse group name
New-AzADServicePrincipal -AppId $($productionApplicationRegistration.AppId)
New-AzRoleAssignment `
-ApplicationId $($productionApplicationRegistration.AppId) `
-RoleDefinitionName Contributor `
-Scope $($productionResourceGroup.ResourceId)
$azureContext = Get-AzContext
Write-Host "AZURE_CLIENT_ID_TEST: $($testApplicationRegistration.AppId)"
Write-Host "AZURE_CLIENT_ID_PRODUCTION: $($productionApplicationRegistration.AppId)"
Write-Host "AZURE_TENANT_ID: $($azureContext.Tenant.Id)"
Write-Host "AZURE_SUBSCRIPTION_ID: $($azureContext.Subscription.Id)" ## write these secrets to github secrets