|
| 1 | +#Requires -Version 7.0 |
| 2 | + |
| 3 | +<# |
| 4 | +.SYNOPSIS |
| 5 | + Configures GitHub secrets required for the CI/CD pipeline. |
| 6 | +.DESCRIPTION |
| 7 | + This script helps set up the necessary GitHub secrets for the CI/CD pipeline. |
| 8 | + It creates a new Azure service principal and outputs the commands needed to set up the GitHub secrets. |
| 9 | +.PARAMETER SubscriptionId |
| 10 | + The Azure subscription ID. |
| 11 | +.PARAMETER ResourceGroupName |
| 12 | + The name of the resource group where the service principal will have access. |
| 13 | + Defaults to 'optima-core-ci-rg'. |
| 14 | +.PARAMETER ServicePrincipalName |
| 15 | + The name for the new service principal. |
| 16 | + Defaults to 'GitHubActions-OptimaCoreCI'. |
| 17 | +.EXAMPLE |
| 18 | + .\setup-ci-secrets.ps1 -SubscriptionId "00000000-0000-0000-0000-000000000000" |
| 19 | + |
| 20 | + Sets up a new service principal with default settings and outputs the GitHub secrets setup commands. |
| 21 | +#> |
| 22 | + |
| 23 | +param ( |
| 24 | + [Parameter(Mandatory=$true)] |
| 25 | + [string]$SubscriptionId, |
| 26 | + |
| 27 | + [string]$ResourceGroupName = "optima-core-ci-rg", |
| 28 | + [string]$ServicePrincipalName = "GitHubActions-OptimaCoreCI", |
| 29 | + [string]$Location = "eastus" |
| 30 | +) |
| 31 | + |
| 32 | +# Function to write colored output |
| 33 | +function Write-Status { |
| 34 | + param( |
| 35 | + [string]$Message, |
| 36 | + [string]$Status = "info" |
| 37 | + ) |
| 38 | + |
| 39 | + $color = switch ($Status.ToLower()) { |
| 40 | + "success" { "Green" } |
| 41 | + "warning" { "Yellow" } |
| 42 | + "error" { "Red" } |
| 43 | + "info" { "Cyan" } |
| 44 | + default { "White" } |
| 45 | + } |
| 46 | + |
| 47 | + Write-Host "[$($Status.ToUpper())] $Message" -ForegroundColor $color |
| 48 | +} |
| 49 | + |
| 50 | +try { |
| 51 | + # Check if Azure CLI is installed |
| 52 | + if (-not (Get-Command az -ErrorAction SilentlyContinue)) { |
| 53 | + Write-Status -Message "Azure CLI is not installed" -Status "error" |
| 54 | + Write-Host "Please install Azure CLI: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli" |
| 55 | + exit 1 |
| 56 | + } |
| 57 | + |
| 58 | + # Login to Azure if not already logged in |
| 59 | + $account = az account show 2>$null | ConvertFrom-Json -ErrorAction SilentlyContinue |
| 60 | + if (-not $account) { |
| 61 | + Write-Status -Message "Logging in to Azure..." -Status "info" |
| 62 | + az login --output none |
| 63 | + } |
| 64 | + |
| 65 | + # Set subscription |
| 66 | + Write-Status -Message "Setting subscription to $SubscriptionId" -Status "info" |
| 67 | + az account set --subscription $SubscriptionId |
| 68 | + |
| 69 | + # Create resource group if it doesn't exist |
| 70 | + $rg = az group show --name $ResourceGroupName --query "id" -o tsv 2>$null |
| 71 | + if (-not $rg) { |
| 72 | + Write-Status -Message "Creating resource group '$ResourceGroupName'..." -Status "info" |
| 73 | + az group create --name $ResourceGroupName --location $Location --output none |
| 74 | + } else { |
| 75 | + Write-Status -Message "Using existing resource group '$ResourceGroupName'" -Status "info" |
| 76 | + } |
| 77 | + |
| 78 | + # Create service principal with Contributor role on the resource group |
| 79 | + Write-Status -Message "Creating service principal '$ServicePrincipalName'..." -Status "info" |
| 80 | + $sp = az ad sp create-for-rbac \ |
| 81 | + --name $ServicePrincipalName \ |
| 82 | + --role "Contributor" \ |
| 83 | + --scopes "/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName" \ |
| 84 | + --years 1 \ |
| 85 | + --sdk-auth | ConvertFrom-Json |
| 86 | + |
| 87 | + if (-not $sp) { |
| 88 | + throw "Failed to create service principal" |
| 89 | + } |
| 90 | + |
| 91 | + # Get Codecov token from user |
| 92 | + $codecovToken = Read-Host -Prompt "Enter your Codecov token (leave empty to skip)" |
| 93 | + |
| 94 | + # Output GitHub secrets setup instructions |
| 95 | + Write-Host "`n" |
| 96 | + Write-Host "==================================================" -ForegroundColor Green |
| 97 | + Write-Host "GitHub Secrets Setup" -ForegroundColor Green |
| 98 | + Write-Host "==================================================" -ForegroundColor Green |
| 99 | + Write-Host "Follow these steps to set up the required GitHub secrets:" |
| 100 | + Write-Host "1. Go to your GitHub repository" |
| 101 | + Write-Host "2. Navigate to Settings > Secrets > Actions" |
| 102 | + Write-Host "3. Click 'New repository secret' and add the following:" |
| 103 | + Write-Host "" |
| 104 | + |
| 105 | + # AZURE_CREDENTIALS |
| 106 | + $azureCredentials = @{ |
| 107 | + clientId = $sp.clientId |
| 108 | + clientSecret = $sp.clientSecret |
| 109 | + subscriptionId = $sp.subscriptionId |
| 110 | + tenantId = $sp.tenantId |
| 111 | + } | ConvertTo-Json -Compress |
| 112 | + |
| 113 | + Write-Host "Name: AZURE_CREDENTIALS" -ForegroundColor Yellow |
| 114 | + Write-Host "Value: $azureCredentials" |
| 115 | + Write-Host "" |
| 116 | + |
| 117 | + # CODECOV_TOKEN (if provided) |
| 118 | + if ($codecovToken) { |
| 119 | + Write-Host "Name: CODECOV_TOKEN" -ForegroundColor Yellow |
| 120 | + Write-Host "Value: $codecovToken" |
| 121 | + Write-Host "" |
| 122 | + } |
| 123 | + |
| 124 | + Write-Host "4. Save the secrets" |
| 125 | + Write-Host "" |
| 126 | + Write-Host "Your CI/CD pipeline is now ready to use!" -ForegroundColor Green |
| 127 | + Write-Host "The service principal has been granted 'Contributor' role on the resource group '$ResourceGroupName'" -ForegroundColor Green |
| 128 | + |
| 129 | + # Save service principal info to a file (excluded from git) |
| 130 | + $secretsFile = ".github/secrets.json" |
| 131 | + $secretsDir = Split-Path -Path $secretsFile -Parent |
| 132 | + if (-not (Test-Path $secretsDir)) { |
| 133 | + New-Item -ItemType Directory -Path $secretsDir -Force | Out-Null |
| 134 | + } |
| 135 | + |
| 136 | + $secrets = @{ |
| 137 | + ServicePrincipal = $sp |
| 138 | + ResourceGroup = $ResourceGroupName |
| 139 | + SubscriptionId = $SubscriptionId |
| 140 | + CodecovToken = $codecovToken |
| 141 | + CreatedAt = Get-Date -Format "yyyy-MM-dd HH:mm:ss" |
| 142 | + } | ConvertTo-Json -Depth 10 |
| 143 | + |
| 144 | + $secrets | Out-File -FilePath $secretsFile -Force |
| 145 | + |
| 146 | + # Add to .gitignore if not already there |
| 147 | + $gitignore = ".gitignore" |
| 148 | + if (-not (Select-String -Path $gitignore -Pattern $secretsFile -SimpleMatch -Quiet)) { |
| 149 | + Add-Content -Path $gitignore -Value "`n# CI/CD Secrets`n$secretsFile" |
| 150 | + } |
| 151 | + |
| 152 | + Write-Status -Message "Service principal details saved to $secretsFile (excluded from git)" -Status "success" |
| 153 | + |
| 154 | +} catch { |
| 155 | + Write-Status -Message "An error occurred: $_" -Status "error" |
| 156 | + exit 1 |
| 157 | +} |
0 commit comments