Skip to content

Commit d387fbb

Browse files
committed
Update CI/CD workflow and parameters for Azure deployment
1 parent 37c9b5f commit d387fbb

3 files changed

Lines changed: 152 additions & 31 deletions

File tree

.github/workflows/cicd.yml

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ permissions:
1313
security-events: write
1414

1515
env:
16-
AZURE_WEBAPP_NAME: app-gh-aspnet-webapp-01 # set this to your application's name
17-
SRC_PROJECT_PATH: '/webapp01/webapp01.csproj'
18-
AZURE_WEBAPP_PACKAGE_PATH: './src' # set this to the path to your web app project, defaults to the repository root
19-
DOTNET_VERSION: '9.0.x' # set this to the dot net version to use
16+
AZURE_WEBAPP_NAME: app-gh-aspnet-webapp-01 # set this to your application's name
17+
SRC_PROJECT_PATH: "/webapp01/webapp01.csproj"
18+
AZURE_WEBAPP_PACKAGE_PATH: "./src" # set this to the path to your web app project, defaults to the repository root
19+
DOTNET_VERSION: "9.0.x" # set this to the dot net version to use
20+
AZURE_ACR_NAME: crdevsecopscldev01 # set this to your Azure Container Registry name
2021

2122
jobs:
2223
cicd:
@@ -25,55 +26,55 @@ jobs:
2526
steps:
2627
# Checkout the repo
2728
- uses: actions/checkout@v5
28-
29+
2930
# Setup .NET Core SDK
3031
- name: Setup .NET Core
3132
uses: actions/setup-dotnet@v4
3233
with:
33-
dotnet-version: ${{ env.DOTNET_VERSION }}
34-
34+
dotnet-version: ${{ env.DOTNET_VERSION }}
35+
3536
# Run dotnet build and publish
3637
- name: dotnet build and publish
3738
run: |
3839
dotnet restore ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}${{ env.SRC_PROJECT_PATH }}
3940
dotnet build --configuration Release ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}${{ env.SRC_PROJECT_PATH }}
4041
dotnet publish -c Release --property:PublishDir='bin/publish' ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}${{ env.SRC_PROJECT_PATH }}
41-
42+
4243
- name: Azure Login
4344
uses: azure/login@v2
4445
with:
45-
client-id: ${{ secrets.AZURE_CLIENT_ID }}
46-
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
47-
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
48-
46+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
47+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
48+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
49+
4950
# Deploy to Azure Web apps
50-
- name: 'Run Azure webapp deploy action using publish profile credentials'
51-
if: false # This disables the action
51+
- name: "Run Azure webapp deploy action using publish profile credentials"
52+
if: false # This disables the action
5253
uses: azure/webapps-deploy@v3
53-
with:
54+
with:
5455
app-name: ${{ env.AZURE_WEBAPP_NAME }} # Replace with your app name
55-
package: '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/webapp01/bin/publish'
56-
56+
package: "${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/webapp01/bin/publish"
57+
5758
- uses: azure/docker-login@v2
5859
with:
59-
login-server: crdevsecopscldev.azurecr.io
60+
login-server: ${{ env.AZURE_ACR_NAME }}.azurecr.io
6061
username: ${{ secrets.REGISTRY_USERNAME }}
6162
password: ${{ secrets.REGISTRY_PASSWORD }}
6263
- run: |
63-
docker build ./src/webapp01 --file ./src/webapp01/Dockerfile -t crdevsecopscldev.azurecr.io/webapp01:${{ github.sha }}
64-
docker push crdevsecopscldev.azurecr.io/webapp01:${{ github.sha }}
65-
64+
docker build ./src/webapp01 --file ./src/webapp01/Dockerfile -t ${{ env.AZURE_ACR_NAME }}.azurecr.io/webapp01:${{ github.sha }}
65+
docker push ${{ env.AZURE_ACR_NAME }}.azurecr.io/webapp01:${{ github.sha }}
66+
6667
- name: Azure Web Apps Deploy
6768
uses: azure/webapps-deploy@v3
6869
with:
6970
app-name: ${{ env.AZURE_WEBAPP_NAME }}
70-
images: 'crdevsecopscldev.azurecr.io/webapp01:${{ github.sha }}'
71-
71+
images: "${{ env.AZURE_ACR_NAME }}.azurecr.io/webapp01:${{ github.sha }}"
72+
7273
- name: logout
7374
run: |
7475
az logout
75-
76-
# https://docs.github.com/en/actions/security-for-github-actions/using-artifact-attestations/using-artifact-attestations-and-reusable-workflows-to-achieve-slsa-v1-build-level-3
76+
77+
# https://docs.github.com/en/actions/security-for-github-actions/using-artifact-attestations/using-artifact-attestations-and-reusable-workflows-to-achieve-slsa-v1-build-level-3
7778
container-build-publish:
7879
name: Build and Publish Container Image
7980
needs: []
@@ -83,4 +84,4 @@ jobs:
8384
version: v1.0.0
8485
container-file: ./src/webapp01/Dockerfile
8586
container-context: ./src/webapp01
86-
container-name: "${{ github.repository }}/webapp01"
87+
container-name: "${{ github.repository }}/webapp01"

infra/deploy.ps1

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<#
2+
.SYNOPSIS
3+
Deploys Azure infrastructure using Bicep templates.
4+
5+
.DESCRIPTION
6+
This script deploys the Azure infrastructure defined in main.bicep
7+
using the parameters from main.parameters.json.
8+
9+
.PARAMETER ParameterFile
10+
Path to the parameters file. Defaults to main.parameters.json.
11+
12+
.PARAMETER Location
13+
Azure region for deployment. Defaults to canadacentral.
14+
15+
.PARAMETER DeploymentName
16+
Name of the deployment. Defaults to a timestamped name.
17+
18+
.PARAMETER WhatIf
19+
Performs a what-if operation without actually deploying.
20+
21+
.EXAMPLE
22+
.\deploy.ps1
23+
24+
.EXAMPLE
25+
.\deploy.ps1 -Location "eastus" -WhatIf
26+
#>
27+
28+
[CmdletBinding()]
29+
param(
30+
[Parameter(Mandatory = $false)]
31+
[string]$ParameterFile = "main.parameters.json",
32+
33+
[Parameter(Mandatory = $false)]
34+
[string]$Location = "canadacentral",
35+
36+
[Parameter(Mandatory = $false)]
37+
[string]$DeploymentName = "deploy-$(Get-Date -Format 'yyyyMMdd-HHmmss')",
38+
39+
[Parameter(Mandatory = $false)]
40+
[switch]$WhatIf
41+
)
42+
43+
$ErrorActionPreference = "Stop"
44+
45+
# Get the script directory
46+
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
47+
48+
# Resolve paths
49+
$BicepFile = Join-Path $ScriptDir "main.bicep"
50+
$ParameterFilePath = Join-Path $ScriptDir $ParameterFile
51+
52+
# Validate files exist
53+
if (-not (Test-Path $BicepFile)) {
54+
Write-Error "Bicep file not found: $BicepFile"
55+
exit 1
56+
}
57+
58+
if (-not (Test-Path $ParameterFilePath)) {
59+
Write-Error "Parameter file not found: $ParameterFilePath"
60+
exit 1
61+
}
62+
63+
# Check if Azure CLI is installed
64+
if (-not (Get-Command az -ErrorAction SilentlyContinue)) {
65+
Write-Error "Azure CLI is not installed. Please install it from https://docs.microsoft.com/cli/azure/install-azure-cli"
66+
exit 1
67+
}
68+
69+
# Check if logged in to Azure
70+
$account = az account show 2>$null | ConvertFrom-Json
71+
if (-not $account) {
72+
Write-Host "Not logged in to Azure. Please log in..." -ForegroundColor Yellow
73+
az login
74+
if ($LASTEXITCODE -ne 0) {
75+
Write-Error "Failed to log in to Azure"
76+
exit 1
77+
}
78+
}
79+
80+
Write-Host "=== Azure Infrastructure Deployment ===" -ForegroundColor Cyan
81+
Write-Host "Subscription: $($account.name)" -ForegroundColor Green
82+
Write-Host "Bicep File: $BicepFile" -ForegroundColor Green
83+
Write-Host "Parameters: $ParameterFilePath" -ForegroundColor Green
84+
Write-Host "Location: $Location" -ForegroundColor Green
85+
Write-Host "Deployment: $DeploymentName" -ForegroundColor Green
86+
Write-Host ""
87+
88+
if ($WhatIf) {
89+
Write-Host "Running What-If analysis..." -ForegroundColor Yellow
90+
az deployment sub what-if `
91+
--name $DeploymentName `
92+
--location $Location `
93+
--template-file $BicepFile `
94+
--parameters @$ParameterFilePath
95+
}
96+
else {
97+
Write-Host "Starting deployment..." -ForegroundColor Yellow
98+
az deployment sub create `
99+
--name $DeploymentName `
100+
--location $Location `
101+
--template-file $BicepFile `
102+
--parameters @$ParameterFilePath
103+
104+
if ($LASTEXITCODE -eq 0) {
105+
Write-Host ""
106+
Write-Host "Deployment completed successfully!" -ForegroundColor Green
107+
108+
# Show deployment outputs
109+
Write-Host ""
110+
Write-Host "Deployment outputs:" -ForegroundColor Cyan
111+
az deployment sub show `
112+
--name $DeploymentName `
113+
--query "properties.outputs" `
114+
--output table
115+
}
116+
else {
117+
Write-Error "Deployment failed with exit code: $LASTEXITCODE"
118+
exit $LASTEXITCODE
119+
}
120+
}

infra/main.parameters.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
"contentVersion": "1.0.0.0",
44
"parameters": {
55
"acrName": {
6-
"value": "acrwebapp01dev"
6+
"value": "crdevsecopscldev01"
77
},
88
"acrSku": {
99
"value": "Basic"
1010
},
1111
"appServicePlanName": {
12-
"value": "aspwebapp01dev"
12+
"value": "asp-gh-aspnet-webapp-01"
1313
},
1414
"webAppName": {
15-
"value": "webapp01dev"
15+
"value": "app-gh-aspnet-webapp-01"
1616
},
1717
"location": {
1818
"value": "canadacentral"
1919
},
2020
"containerImage": {
21-
"value": "acrwebapp01dev.azurecr.io/webapp01:latest"
21+
"value": "crdevsecopscldev01.azurecr.io/webapp01:latest"
2222
},
2323
"resourceGroupName": {
24-
"value": "rg-webapp01-dev"
24+
"value": "rg-gh-aspnet-webapp-01"
2525
}
2626
}
2727
}

0 commit comments

Comments
 (0)