Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy terraform from GitHub #8

Merged
merged 73 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
c072e94
Add new PR workflow
russdaygh Apr 22, 2024
37b3ea4
Disable existing PR triggered workflows
russdaygh Apr 22, 2024
715b405
Fix syntax errors
russdaygh Apr 22, 2024
5cd7b01
Set working dir for tf job
russdaygh Apr 22, 2024
0caed93
Log in to Azure before applying TF
russdaygh Apr 22, 2024
85513be
Use custom GitHub App with secret write permissions
russdaygh Apr 23, 2024
720e348
Explicitly pass secrets as they only inherit from org or enterprise
russdaygh Apr 23, 2024
f8108c3
Except GITHUB_TOKEN
russdaygh Apr 23, 2024
6e1f52f
Declare new app secrets on reusable workflow
russdaygh Apr 23, 2024
674ef12
RUn plan before apply
russdaygh Apr 23, 2024
8305e5b
tf fmt, add tf state storage account
russdaygh Apr 23, 2024
f469451
Choose different names for name-conflicted resources
russdaygh Apr 23, 2024
1cad88b
Try new keyvault name
russdaygh Apr 23, 2024
c32557a
Fix incorrect GitHub env variable name
russdaygh Apr 23, 2024
23f221c
Auth with GitHub another way
russdaygh Apr 23, 2024
0b4eed1
Pass the secrets to the left hand side
russdaygh Apr 23, 2024
1d3f0b9
Pls fix
russdaygh Apr 23, 2024
ddb7136
Move client deployment token to key vault
russdaygh Apr 23, 2024
cf28dee
Remove other GitHub related bits out of terraform
russdaygh Apr 23, 2024
d07a7db
Remove passed secrets that no longer exist
russdaygh Apr 23, 2024
34ebd19
Try dynamically retrieving the API token
russdaygh Apr 23, 2024
b60c816
az login first!
russdaygh Apr 23, 2024
9d94503
Remove the asterisks?
russdaygh Apr 23, 2024
65abf1e
Fix the PAI server name in deployment
russdaygh Apr 23, 2024
43c8b5b
Deploy to test instead
russdaygh Apr 23, 2024
2d7c8f1
Quote the env name?
russdaygh Apr 23, 2024
c133667
Reduce keyvault name length some more
russdaygh Apr 23, 2024
d5bd535
Fix the seed for EventDetail
russdaygh Apr 23, 2024
ab1776e
Remove special chars from SQL password
russdaygh Apr 23, 2024
ab558ad
Remove special chars from admin login
russdaygh Apr 23, 2024
5a5aa35
Set app settings for blazor client at top level
russdaygh Apr 25, 2024
4f1e83f
Default to 'Production' blazor client environment
russdaygh Apr 25, 2024
18b545f
Remove client environment from correct pipeline
russdaygh Apr 25, 2024
adb3156
Explicitly deploy to Production
russdaygh Apr 25, 2024
94cc29b
Explicitly set Production env for blazor client
russdaygh Apr 25, 2024
3d7673f
Use the branch name
russdaygh Apr 25, 2024
8987bd7
Explicitly set app settings after deployment
russdaygh Apr 25, 2024
a8901b7
Set client environments again
russdaygh Apr 25, 2024
6d26700
Turn off preview envirnoments for blazor client
russdaygh Apr 25, 2024
101f020
Deploy explicitly to Production environment
russdaygh Apr 25, 2024
c76e2d4
Trigger on non-main branch push
russdaygh Apr 25, 2024
a4ce721
Update some actions
russdaygh Apr 25, 2024
c5befad
Remove environment specifier on blazor client deployment
russdaygh Apr 25, 2024
6269d63
Update download artifact step
russdaygh Apr 25, 2024
a7f465e
Dynamically set app setting before deployment
russdaygh Apr 25, 2024
19ad3b6
Remove env name from set app settings command
russdaygh Apr 25, 2024
786612f
Comment out now-failing code
russdaygh Apr 25, 2024
b857a76
Set the dotnet environment variables
russdaygh Apr 25, 2024
eb3f620
Only apply terraform if there are changes
russdaygh Apr 25, 2024
5b74488
Try having no app settings file
russdaygh Apr 25, 2024
94cc4da
Set apiUrl with full HTTP bits
russdaygh Apr 25, 2024
ea8bcf0
Stop setting client environment variables explicitly
russdaygh Apr 25, 2024
9065f94
Add a bit of debug around tf switch
russdaygh Apr 25, 2024
c5f625e
Remove tf optimisation
russdaygh Apr 25, 2024
2219517
Add an empty app settings
russdaygh Apr 25, 2024
66ecd64
Reinstate app settings files
russdaygh Apr 25, 2024
76e9b23
Try just replacing the default app settings json
russdaygh Apr 25, 2024
92d4e9b
Add a user to kv
russdaygh Apr 25, 2024
5172d29
Change access policy assignee
russdaygh Apr 25, 2024
a6aaa94
Fx tf syntax
russdaygh Apr 25, 2024
07e4a51
Set expected app setting file name
russdaygh Apr 25, 2024
3b0f0b7
Fix connection string mistake
russdaygh Apr 25, 2024
dd39a3d
Revert settings changes
russdaygh Apr 25, 2024
53c7e47
Merge branch 'main' into feature/deployTerraformFromGitHub
russdaygh Apr 25, 2024
6c41e51
Default to deploying to Test environment
russdaygh Apr 25, 2024
d517fc2
Rename tfvars files to match environment names
russdaygh Apr 25, 2024
855d94e
Replace test file
russdaygh Apr 25, 2024
5ba78ac
Add Test tfvars
russdaygh Apr 25, 2024
b7874f8
Let's deploy to Test2
russdaygh Apr 25, 2024
98fef8e
Add Test2 app settings and tfvars
russdaygh Apr 25, 2024
383a3c2
facepalm
russdaygh Apr 25, 2024
ee484cf
facepalm 2 electric boogaloo
russdaygh Apr 25, 2024
9d6d04d
Use different SKU
russdaygh Apr 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/DeployBlazorClient.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: Build and deploy Pocket DDD Blazor Client

on:
workflow_dispatch:
push:

jobs:
build_and_deploy_job:
Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/DeployBranchPush.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Deploy Branch Push
on:
push:
branches-ignore:
- 'main'


jobs:
deploy_to_test:
uses: ./.github/workflows/DeployEverything.yml
with:
env: "Test2"
secrets:
AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}
AZURE_STATIC_WEB_APPS_API_TOKEN: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
TERRAFORM_STATE_ACCESS_KEY: ${{ secrets.TERRAFORM_STATE_ACCESS_KEY }}
124 changes: 124 additions & 0 deletions .github/workflows/DeployEverything.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
name: Deploy Everything
on:
workflow_call:
inputs:
env:
required: true
default: "Test"
type: string
secrets:
AZURE_CREDENTIALS:
required: true
AZURE_STATIC_WEB_APPS_API_TOKEN:
required: true
TERRAFORM_STATE_ACCESS_KEY:
required: true

env:
AZURE_WEBAPP_PACKAGE_PATH: PocketDDD.Server.WebAPI/publish
CONFIGURATION: Release
DOTNET_CORE_VERSION: 8.0.x
WORKING_DIRECTORY: PocketDDD.Server/PocketDDD.Server.WebAPI

jobs:
deploy_terraform:
runs-on: ubuntu-latest
name: Deploy terraform
environment: ${{ inputs.env }}
defaults:
run:
working-directory: ./terraform

steps:
- uses: actions/checkout@v4
- name: Log in with Azure
uses: azure/login@v1
with:
creds: '${{ secrets.AZURE_CREDENTIALS }}'
- name: Setup terraform
uses: hashicorp/setup-terraform@v3
- run: |
terraform init -backend-config="key=${{ inputs.env }}.terraform.tfstate"

terraform apply -auto-approve --var-file ../tfvars/${{ inputs.env }}.tfvars
env:
ARM_ACCESS_KEY: ${{ secrets.TERRAFORM_STATE_ACCESS_KEY }}

build_api_server:
runs-on: ubuntu-latest
name: Build API Server
steps:
- uses: actions/checkout@v4
- name: Setup .NET SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ env.DOTNET_CORE_VERSION }}
- name: Restore
run: dotnet restore "${{ env.WORKING_DIRECTORY }}"
- name: Build
run: dotnet build "${{ env.WORKING_DIRECTORY }}" --configuration ${{ env.CONFIGURATION }} --no-restore
- name: Test
run: dotnet test "${{ env.WORKING_DIRECTORY }}" --no-build
- name: Publish
run: dotnet publish "${{ env.WORKING_DIRECTORY }}" --configuration ${{ env.CONFIGURATION }} --no-build --output "${{ env.AZURE_WEBAPP_PACKAGE_PATH }}"
- name: Publish Artifacts
uses: actions/upload-artifact@v4
with:
name: webapp
path: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}

deploy_api_server:
name: Deploy API Server
runs-on: ubuntu-latest
environment: ${{ inputs.env }}
needs: [deploy_terraform, build_api_server]
steps:
- name: Log in with Azure
uses: azure/login@v1
with:
creds: '${{ secrets.AZURE_CREDENTIALS }}'
- name: Download artifact from build job
uses: actions/download-artifact@v4
with:
name: webapp
path: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
- name: Deploy to Azure WebApp
uses: azure/webapps-deploy@v2
with:
app-name: pocketddd-${{ inputs.env }}-api-server-web-app
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}

build_and_deploy_blazor_client:
runs-on: ubuntu-latest
environment: ${{ inputs.env }}
name: Build and Deploy Blazor Client
needs: deploy_terraform
steps:
- uses: actions/checkout@v2
with:
submodules: true
- name: Log in with Azure
uses: azure/login@v1
with:
creds: '${{ secrets.AZURE_CREDENTIALS }}'

- run: |
cp PocketDDD.BlazorClient/PocketDDD.BlazorClient/wwwroot/appsettings.${{ inputs.env }}.json PocketDDD.BlazorClient/PocketDDD.BlazorClient/wwwroot/appsettings.Production.json

- run: |
apiToken=$(az staticwebapp secrets list --name pocketddd-${{ inputs.env }}-blazorclient --query "properties.apiKey" -o tsv)
echo "WEB_APP_API_TOKEN=$apiToken" >> "$GITHUB_ENV"

- name: Build And Deploy
id: builddeploy
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ env.WEB_APP_API_TOKEN }}
repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments)
action: "upload"
###### Repository/Build Configurations - These values can be configured to match your app requirements. ######
# For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig
app_location: "/PocketDDD.BlazorClient/PocketDDD.BlazorClient" # App source code path
api_location: "" # Api source code path - optional
output_location: "wwwroot" # Built app content directory - optional
###### End of Repository/Build Configurations ######
1 change: 0 additions & 1 deletion .github/workflows/DeployServerWebAPI.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: Build and deploy Pocket DDD Server
on:
workflow_dispatch:
push:

env:
AZURE_WEBAPP_PACKAGE_PATH: PocketDDD.Server.WebAPI/publish
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"apiUrl": "https://dddsw2023pocketdddserverwebapi.azurewebsites.net/api/",
"apiUrl": "https://pocketddd-production-api-server-web-app.azurewebsites.net/api/",
"fakeBackend": false
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"apiUrl": "https://pocketddd-test2-api-server-web-app.azurewebsites.net/api/",
"fakeBackend": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"apiUrl": "https://pocketddd-test-api-server-web-app.azurewebsites.net/api/",
"fakeBackend": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ delete EventDetail

GO

DBCC CHECKIDENT ('[EventDetail]', RESEED, 0);
DBCC CHECKIDENT ('[EventDetail]', RESEED, 1);
DBCC CHECKIDENT ('[Tracks]', RESEED, 0);
DBCC CHECKIDENT ('[TimeSlots]', RESEED, 0);
DBCC CHECKIDENT ('[Sessions]', RESEED, 0);
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Authorization: <insert-admin-key>
Ensure the Azure, GitHub, and terraform CLIs are installed
```
brew install azure-cli
bre install gh
brew install gh
brew install terraform
```

Expand All @@ -24,11 +24,16 @@ az login
gh auth login
```

Retrieve the access key for the terraform state storage account
```
export ARM_ACCESS_KEY=$(az storage account keys list -g pocketddd-terraform-state -n pocketdddterraformstate --query [0].value -o tsv)
```

From the `terraform` directory run init, plan, then apply if happy with the changes.
```
cd ./terraform

terraform init
terraform init -backend-config="dev.terraform.tfstate"
terraform plan -var-file ../tfvars/dev.tfvars
terraform apply -var-file ../tfvars/dev.tfvars
```
8 changes: 7 additions & 1 deletion terraform/api_server.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ resource "azurerm_service_plan" "api_server_service_plan" {


resource "azurerm_linux_web_app" "api_server_web_app" {
name = "${local.resource_prefix}-api-server"
name = "${local.resource_prefix}-api-server-web-app"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_service_plan.api_server_service_plan.location
service_plan_id = azurerm_service_plan.api_server_service_plan.id
Expand All @@ -30,3 +30,9 @@ resource "azurerm_linux_web_app" "api_server_web_app" {
"AdminKey" = random_password.admin_api_key.result
}
}

resource "azurerm_key_vault_secret" "api_admin_key" {
name = "${local.resource_prefix}-admin-api-key"
value = random_password.admin_api_key.result
key_vault_id = azurerm_key_vault.key_vault.id
}
20 changes: 14 additions & 6 deletions terraform/blazor_client.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ resource "azurerm_static_web_app" "blazor-client" {

sku_tier = var.client_sku_tier
sku_size = var.client_sku_size

app_settings = {
"ASPNETCORE_ENVIRONMENT": "${ var.env }"
"DOTNET_ENVIRONMENT": "${ var.env }"
"apiUrl": "https://pocketddd-${ var.env }-api-server-web-app.azurewebsites.net/api/"
"fakeBackend": "false"
}

preview_environments_enabled = false
}

resource "github_actions_environment_secret" "test_secret" {
repository = data.github_repository.repo.name
environment = github_repository_environment.repo_environment.environment
secret_name = "AZURE_STATIC_WEB_APPS_API_TOKEN"
plaintext_value = azurerm_static_web_app.blazor-client.api_key
}
resource "azurerm_key_vault_secret" "blazor_client_deployment_token" {
name = "${local.resource_prefix}-blazor-client-deployment-token"
value = azurerm_static_web_app.blazor-client.api_key
key_vault_id = azurerm_key_vault.key_vault.id
}
20 changes: 16 additions & 4 deletions terraform/database.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ resource "azurerm_mssql_server" "sqlserver" {
}

resource "azurerm_mssql_database" "sqldb" {
name = "${local.resource_prefix}-sqldatabase"
server_id = azurerm_mssql_server.sqlserver.id
sku_name = var.sql_db_sku
max_size_gb = var.sql_max_storage
name = "${local.resource_prefix}-sqldatabase"
server_id = azurerm_mssql_server.sqlserver.id
sku_name = var.sql_db_sku
max_size_gb = var.sql_max_storage
storage_account_type = "Local"

tags = {
Expand All @@ -34,3 +34,15 @@ resource "azurerm_mssql_firewall_rule" "firewall_rule" {
start_ip_address = "0.0.0.0"
end_ip_address = "0.0.0.0"
}

resource "azurerm_key_vault_secret" "sqldb_connectionstring" {
name = "${local.resource_prefix}-db-connection-string"
value = local.db_connection_string
key_vault_id = azurerm_key_vault.key_vault.id
}

resource "azurerm_key_vault_secret" "sqldb_admin_password" {
name = "${local.resource_prefix}-db-admin-password"
value = random_password.admin_password.result
key_vault_id = azurerm_key_vault.key_vault.id
}
53 changes: 0 additions & 53 deletions terraform/keyvault.tf

This file was deleted.

Loading
Loading