Skip to content

Commit 1f4e07a

Browse files
authored
Merge pull request #11 from OptimaCore/feature/automated-testing-pipeline-GitHubActions
Add - CI/CD setup github actions and api references, docs
2 parents 030a032 + 15db78d commit 1f4e07a

43 files changed

Lines changed: 10644 additions & 247 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#Requires -Version 7.0
2+
3+
<#
4+
.SYNOPSIS
5+
Verifies that the CI/CD setup is configured correctly.
6+
.DESCRIPTION
7+
This script checks if all required tools and configurations are in place
8+
for the CI/CD pipeline to run successfully.
9+
#>
10+
11+
# Function to write colored output
12+
function Write-Status {
13+
param(
14+
[string]$Message,
15+
[string]$Status = "info"
16+
)
17+
18+
$color = switch ($Status.ToLower()) {
19+
"success" { "Green" }
20+
"warning" { "Yellow" }
21+
"error" { "Red" }
22+
"info" { "Cyan" }
23+
default { "White" }
24+
}
25+
26+
Write-Host "[$($Status.ToUpper())] $Message" -ForegroundColor $color
27+
}
28+
29+
function Test-CommandExists {
30+
param($command)
31+
try {
32+
Get-Command $command -ErrorAction Stop | Out-Null
33+
return $true
34+
} catch {
35+
return $false
36+
}
37+
}
38+
39+
# Check required tools
40+
$tools = @(
41+
@{ Name = "Node.js"; Command = "node"; Required = $true },
42+
@{ Name = "npm"; Command = "npm"; Required = $true },
43+
@{ Name = "Terraform"; Command = "terraform"; Required = $true },
44+
@{ Name = "Azure CLI"; Command = "az"; Required = $true },
45+
@{ Name = "Git"; Command = "git"; Required = $true }
46+
)
47+
48+
Write-Host "`nVerifying required tools..." -ForegroundColor Cyan
49+
$allToolsAvailable = $true
50+
51+
foreach ($tool in $tools) {
52+
$exists = Test-CommandExists -command $tool.Command
53+
$status = if ($exists) { "success" } else { "error" }
54+
$message = if ($exists) { "Found $($tool.Name)" } else { "Missing $($tool.Name) (required: $($tool.Required))" }
55+
56+
Write-Status -Message $message -Status $status
57+
58+
if (-not $exists -and $tool.Required) {
59+
$allToolsAvailable = $false
60+
}
61+
}
62+
63+
# Check required files
64+
$requiredFiles = @(
65+
".github/workflows/ci-cd.yml",
66+
"scripts/ci-setup.js",
67+
"scripts/teardown.js",
68+
"infrastructure/main.tf",
69+
"infrastructure/variables.tf",
70+
"infrastructure/outputs.tf"
71+
)
72+
73+
Write-Host "`nVerifying required files..." -ForegroundColor Cyan
74+
$allFilesExist = $true
75+
76+
foreach ($file in $requiredFiles) {
77+
$exists = Test-Path $file -PathType Leaf
78+
$status = if ($exists) { "success" } else { "error" }
79+
$message = if ($exists) { "Found $file" } else { "Missing $file" }
80+
81+
Write-Status -Message $message -Status $status
82+
83+
if (-not $exists) {
84+
$allFilesExist = $false
85+
}
86+
}
87+
88+
# Check GitHub secrets (if running in GitHub Actions)
89+
if ($env:GITHUB_ACTIONS -eq "true") {
90+
Write-Host "`nVerifying GitHub secrets..." -ForegroundColor Cyan
91+
92+
$requiredSecrets = @(
93+
@{ Name = "AZURE_CREDENTIALS"; Required = $true },
94+
@{ Name = "CODECOV_TOKEN"; Required = $false }
95+
)
96+
97+
foreach ($secret in $requiredSecrets) {
98+
$exists = [bool]($env:($secret.Name) -ne $null)
99+
$status = if ($exists) { "success" } else { if ($secret.Required) { "error" } else { "warning" } }
100+
$message = if ($exists) { "Found secret: $($secret.Name)" } else { "Missing secret: $($secret.Name) (Required: $($secret.Required))" }
101+
102+
Write-Status -Message $message -Status $status
103+
104+
if (-not $exists -and $secret.Required) {
105+
$allSecretsAvailable = $false
106+
}
107+
}
108+
}
109+
110+
# Summary
111+
Write-Host "`nVerification Summary:" -ForegroundColor Cyan
112+
if ($allToolsAvailable -and $allFilesExist) {
113+
Write-Status -Message "✅ All required tools and files are available" -Status "success"
114+
115+
if ($env:GITHUB_ACTIONS -eq "true") {
116+
if ($allSecretsAvailable) {
117+
Write-Status -Message "✅ All required GitHub secrets are available" -Status "success"
118+
} else {
119+
Write-Status -Message "❌ Some required GitHub secrets are missing" -Status "error"
120+
Write-Host "Run '.github\scripts\setup-ci-secrets.ps1' to set up the required secrets." -ForegroundColor Yellow
121+
}
122+
} else {
123+
Write-Status -Message "ℹ️ Run this script in a GitHub Actions environment to verify secrets" -Status "info"
124+
}
125+
126+
Write-Host "`nNext steps:" -ForegroundColor Green
127+
Write-Host "1. Push your changes to trigger the CI/CD pipeline"
128+
Write-Host "2. Monitor the workflow in the GitHub Actions tab"
129+
Write-Host "3. Check the logs for any issues"
130+
131+
exit 0
132+
} else {
133+
Write-Status -Message "❌ Some requirements are not met" -Status "error"
134+
135+
if (-not $allToolsAvailable) {
136+
Write-Host "- Install the missing tools listed above" -ForegroundColor Red
137+
}
138+
139+
if (-not $allFilesExist) {
140+
Write-Host "- Ensure all required files are present in the repository" -ForegroundColor Red
141+
}
142+
143+
exit 1
144+
}

0 commit comments

Comments
 (0)