Skip to content

Commit

Permalink
Merge pull request #231 from DFE-Digital/feature/fhb-825-database-see…
Browse files Browse the repository at this point in the history
…ding

Feature/fhb 825 database seeding
  • Loading branch information
stevesatdfe authored Nov 14, 2024
2 parents dd05eca + 34e336a commit 96d0ddf
Show file tree
Hide file tree
Showing 29 changed files with 311 additions and 32 deletions.
44 changes: 44 additions & 0 deletions .github/actions/run-sql-script/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Runs SQL Scripts
description: Runs SQL script specified by the path provided against the database

inputs:
keyvault_name:
required: true
description: Name of the key vault to get the secret from
keyvault_secret_name:
required: true
description: Name of the secret containing the database connection string
sql_file_path:
required: true
description: Path to the SQL file

runs:
using: composite

steps:
- name: Run SQL Script in PowerShell
shell: pwsh
run: |-
$ConnectionString = az keyvault secret show --name ${{ inputs.keyvault_secret_name }} --vault-name ${{ inputs.keyvault_name }} --query value --output tsv
$Logs = @()
$SqlFile = Get-ChildItem -Path ${{ inputs.sql_file_path }} -File -Filter *.sql
try {
Invoke-Sqlcmd -ConnectionString $ConnectionString -InputFile $SqlFile -ErrorAction 'Stop'
$LogMessage = ($SqlFile.Name + " Executed Successfully.")
}
catch {
$LogMessage = "Error executing $($SqlFile.Name): $_"
throw
}
$Row = [PSCustomObject]@{
"File" = $SqlFile.Name
"Date" = (Get-Date -UFormat "%d-%m-%Y")
"Log" = $LogMessage
}
$Logs += $Row
Write-Output ($Logs | Format-Table -AutoSize -Wrap | Out-String)
16 changes: 8 additions & 8 deletions .github/workflows/deploy-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ jobs:
files: "${{ github.workspace }}/src/${{ inputs.project_type }}/${{ inputs.artifact_name }}/src/${{ inputs.project_name }}/appsettings.json, ./out/appsettings.json"
configure: 'Applications'

- name: Deploy Artifact to Azure
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.APP_NAME }}
publish-profile: ${{ steps.substitution.outputs.publish_profile }}
package: ./out

- name: Apply Database Migration
if: ${{ inputs.data_project_name != '' && inputs.database_context != ''}}
uses: ./.github/actions/database-migration
Expand All @@ -95,4 +88,11 @@ jobs:
startup_project_path: ${{ github.workspace }}/src/${{ inputs.project_type }}/${{ inputs.artifact_name }}/src/${{ inputs.project_name }}
azure_resource_group: ${{ env.RESOURCE_GROUP }}
azure_sql_server_resource_name: ${{ env.SQL_SERVER }}
az_firewall_rule_name: ${{ inputs.artifact_name }}
az_firewall_rule_name: ${{ inputs.artifact_name }}

- name: Deploy Artifact to Azure
uses: azure/webapps-deploy@v3
with:
app-name: ${{ env.APP_NAME }}
publish-profile: ${{ steps.substitution.outputs.publish_profile }}
package: ./out
119 changes: 106 additions & 13 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,65 @@ jobs:
publish_project: ${{ matrix.publish_project }}
dotnet_version: ${{ matrix.dotnet_version_override || vars.DOTNET_VERSION }}

reset-databases:
if: startsWith(inputs.environment, 'Test')
name: Reset the ${{ matrix.display_name }} database
needs: [ build-projects-upload-artifacts ]
strategy:
matrix:
databases: [
idam-database,
notification-database,
open-referral-mock-database,
referral-database,
report-database,
service-directory-database
]
include:
- databases: idam-database
display_name: IDAM
keyvault_secret_name: IDAM-API-CONNECTIONSTRINGS-IDAMCONNECTION
sql_file_path: ./test/reset-scripts/reset-database.sql
- databases: notification-database
display_name: Notification
keyvault_secret_name: NOTIFICATIONS-API-CONNECTIONSTRINGS-NOTIFICATIONCONNECTION
sql_file_path: ./test/reset-scripts/reset-database.sql
- databases: open-referral-mock-database
display_name: Open Referral Mock
keyvault_secret_name: MOCK-HSDA-API-CONNECTIONSTRINGS-HSDAMOCKRESPONSESCONNECTION
sql_file_path: ./test/reset-scripts/reset-database.sql
- databases: referral-database
display_name: Referral
keyvault_secret_name: REFERRAL-API-CONNECTIONSTRINGS-REFERRALCONNECTION
sql_file_path: ./test/reset-scripts/reset-database.sql
- databases: report-database
display_name: Report
keyvault_secret_name: REPORT-API-CONNECTIONSTRINGS-REPORTCONNECTION
sql_file_path: ./test/reset-scripts/reset-database.sql
- databases: service-directory-database
display_name: Service Directory
keyvault_secret_name: SD-API-CONNECTIONSTRINGS-SERVICEDIRECTORYCONNECTION
sql_file_path: ./test/reset-scripts/reset-database.sql
uses: ./.github/workflows/run-sql-script.yml
with:
environment: ${{ inputs.environment }}
display_name: ${{ matrix.display_name }}
keyvault_secret_name: ${{ matrix.keyvault_secret_name }}
sql_file_path: ${{ matrix.sql_file_path }}
secrets: inherit

deploy-api-services:
if: always() && (needs.reset-databases.result == 'success' || needs.reset-databases.result == 'skipped')
name: ${{ matrix.job_name }}
needs: [ build-projects-upload-artifacts ]
needs: [ reset-databases ]
strategy:
matrix:
artifact_name: [ idam-api,
notification-api,
referral-api,
report-api,
service-directory-api
service-directory-api,
mock-hsda-api
]
include:
- artifact_name: idam-api
Expand All @@ -106,39 +155,34 @@ jobs:
data_project_name: FamilyHubs.Idam.Data
database_context: ApplicationDbContext
azure_app_name: as-fh-idam-api

- artifact_name: notification-api
job_name: Deploy - Notification API
keyvault_prefix: NOTIFICATIONS-API
project_name: FamilyHubs.Notification.Api
data_project_name: FamilyHubs.Notification.Data
database_context: ApplicationDbContext
azure_app_name: as-fh-notification-api

- artifact_name: referral-api
job_name: Deploy - Referral API
keyvault_prefix: REFERRAL-API
project_name: FamilyHubs.Referral.Api
data_project_name: FamilyHubs.Referral.Data
database_context: ApplicationDbContext
azure_app_name: as-fh-referral-api

- artifact_name: report-api
job_name: Deploy - Report API
keyvault_prefix: REPORT-API
project_name: FamilyHubs.Report.Api
data_project_name: FamilyHubs.Report.Data
database_context: ReportDbContext
azure_app_name: as-fh-report-api

- artifact_name: service-directory-api
job_name: Deploy - Service Directory API
keyvault_prefix: SD-API
project_name: FamilyHubs.ServiceDirectory.Api
data_project_name: FamilyHubs.ServiceDirectory.Data
database_context: ApplicationDbContext
azure_app_name: as-fh-sd-api

- artifact_name: mock-hsda-api
job_name: Deploy - Mock HSDA API
keyvault_prefix: MOCK-HSDA-API
Expand All @@ -158,8 +202,8 @@ jobs:
azure_app_name: ${{ matrix.azure_app_name }}
secrets: inherit


deploy-ui-services:
if: always() && needs.deploy-api-services.result == 'success' && (needs.reset-databases.result == 'success' || needs.reset-databases.result == 'skipped')
name: ${{ matrix.job_name }}
needs: [ deploy-api-services ]
strategy:
Expand Down Expand Up @@ -210,11 +254,10 @@ jobs:
azure_app_name: ${{ matrix.azure_app_name }}
secrets: inherit



deploy-functions:
if: always() && (needs.reset-databases.result == 'success' || needs.reset-databases.result == 'skipped')
name: ${{ matrix.job_name }}
needs: [ build-projects-upload-artifacts ]
needs: [ reset-databases ]
strategy:
matrix:
artifact_name: [
Expand All @@ -233,12 +276,58 @@ jobs:
keyvault_prefix: ${{ matrix.keyvault_prefix }}
azure_app_name: ${{ matrix.azure_app_name }}
secrets: inherit


seed-databases:
if: startsWith(inputs.environment, 'Test')
name: Seed the ${{ matrix.display_name }} database
needs: [ deploy-api-services, deploy-ui-services, deploy-functions ]
strategy:
matrix:
databases: [
idam-database,
notification-database,
open-referral-mock-database,
referral-database,
report-database,
service-directory-database
]
include:
- databases: idam-database
display_name: IDAM
keyvault_secret_name: IDAM-API-CONNECTIONSTRINGS-IDAMCONNECTION
sql_file_path: ./test/seed-scripts/test/idam-data.sql
- databases: notification-database
display_name: Notification
keyvault_secret_name: NOTIFICATIONS-API-CONNECTIONSTRINGS-NOTIFICATIONCONNECTION
sql_file_path: ./test/seed-scripts/test/notification-data.sql
- databases: open-referral-mock-database
display_name: Open Referral Mock
keyvault_secret_name: MOCK-HSDA-API-CONNECTIONSTRINGS-HSDAMOCKRESPONSESCONNECTION
sql_file_path: ./test/seed-scripts/test/open-referral-mock-data.sql
- databases: referral-database
display_name: Referral
keyvault_secret_name: REFERRAL-API-CONNECTIONSTRINGS-REFERRALCONNECTION
sql_file_path: ./test/seed-scripts/test/referral-data.sql
- databases: report-database
display_name: Report
keyvault_secret_name: REPORT-API-CONNECTIONSTRINGS-REPORTCONNECTION
sql_file_path: ./test/seed-scripts/test/report-data.sql
- databases: service-directory-database
display_name: Service Directory
keyvault_secret_name: SD-API-CONNECTIONSTRINGS-SERVICEDIRECTORYCONNECTION
sql_file_path: ./test/seed-scripts/test/service-directory-data.sql
uses: ./.github/workflows/run-sql-script.yml
with:
environment: ${{ inputs.environment }}
display_name: ${{ matrix.display_name }}
keyvault_secret_name: ${{ matrix.keyvault_secret_name }}
sql_file_path: ${{ matrix.sql_file_path }}
secrets: inherit

run-api-acceptance-tests:
if: startsWith(inputs.environment, 'Test')
name: ${{ matrix.job_name }}
needs: [ deploy-ui-services ]
needs: [ seed-databases ]
strategy:
matrix:
project_name: [
Expand All @@ -250,20 +339,24 @@ jobs:
- project_name: report-api
job_name: Test - Report API
keyvault_prefix: TEST-REPORT-API
app_name: as-fh-report-api
test_project_name: FamilyHubs.Report.Api.AcceptanceTests
- project_name: referral-api
job_name: Test - Referral API
keyvault_prefix: TEST-REFERRAL-API
app_name: as-fh-referral-api
test_project_name: FamilyHubs.Referral.Api.AcceptanceTests
- project_name: service-directory-api
job_name: Test - Service Directory API
keyvault_prefix: TEST-SERVICE-DIRECTORY-API
app_name: as-fh-sd-api
test_project_name: FamilyHubs.ServiceDirectory.Api.AcceptanceTests
uses: ./.github/workflows/run-acceptance-tests.yml
with:
environment: ${{ inputs.environment }}
project_name: ${{ matrix.project_name }}
keyvault_prefix: ${{ matrix.keyvault_prefix }}
app_name: ${{ matrix.app_name }}
test_project_name: ${{ matrix.test_project_name }}
project_type: service
secrets: inherit
22 changes: 21 additions & 1 deletion .github/workflows/run-acceptance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ on:
keyvault_prefix:
required: true
type: string
app_name:
required: true
type: string
test_project_name:
required: true
type: string
Expand All @@ -27,6 +30,8 @@ jobs:
env:
KEYVAULT: ${{ vars.AZURE_RESOURCE_PREFIX }}-kv-fh-general
RESOURCE_GROUP: ${{ vars.AZURE_RESOURCE_PREFIX }}-familyhubs
API_APP_NAME: ${{ vars.AZURE_RESOURCE_PREFIX }}-${{ inputs.app_name }}
APP_FIREWALL_NAME: acceptance-tests
steps:
- name: Checkout Repository
uses: actions/checkout@v4
Expand All @@ -45,6 +50,15 @@ jobs:
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

- name: Get Workflow Runner IP
id: runner-ip
uses: ./.github/actions/get-runner-ip-address

- name: Add IP Address to App Firewall
shell: pwsh
run: |-
az webapp config access-restriction add -g ${{ env.RESOURCE_GROUP }} -n ${{ env.API_APP_NAME }} --rule-name ${{ env.APP_FIREWALL_NAME }} --action Allow --ip-address ${{ steps.runner-ip.outputs.ip_address }} --priority 50
- name: Test Settings Variable Substitution
id: substitution
uses: ./.github/actions/variable-substitution
Expand All @@ -56,4 +70,10 @@ jobs:

- name: Run Acceptance Tests
shell: bash
run: dotnet test "${{ github.workspace }}/src/${{ inputs.project_type }}/${{ inputs.project_name }}/acceptance-tests/${{ inputs.test_project_name }}/${{ inputs.test_project_name }}.csproj" --logger "html;logfilename=testResults.html"
run: dotnet test "${{ github.workspace }}/src/${{ inputs.project_type }}/${{ inputs.project_name }}/acceptance-tests/${{ inputs.test_project_name }}/${{ inputs.test_project_name }}.csproj" --logger "html;logfilename=testResults.html"

- name: Remove IP Address from App Firewall
if: always()
shell: pwsh
run: |-
az webapp config access-restriction remove -g ${{ env.RESOURCE_GROUP }} -n ${{ env.API_APP_NAME }} --rule-name ${{ env.APP_FIREWALL_NAME }}
Loading

0 comments on commit 96d0ddf

Please sign in to comment.