Skip to content

Commit

Permalink
[DEVOPS-406] Fix game config SimplySql PowerShell Module issues (#112)
Browse files Browse the repository at this point in the history
<details open>
<summary><a href="https://amuniversal.atlassian.net/browse/DEVOPS-406"
title="DEVOPS-406" target="_blank">DEVOPS-406</a></summary>
  <br />
  <table>
    <tr>
      <th>Summary</th>
      <td>Game config deployment failing on package error</td>
    </tr>
    <tr>
      <th>Type</th>
      <td>
<img alt="Bug"
src="https://amuniversal.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10308?size=medium"
/>
        Bug
      </td>
    </tr>
    <tr>
      <th>Status</th>
      <td>In Development Env</td>
    </tr>
    <tr>
      <th>Points</th>
      <td>N/A</td>
    </tr>
    <tr>
      <th>Labels</th>
      <td>-</td>
    </tr>
  </table>
</details>
<!--
do not remove this marker as it will break action-jira-linter's
functionality.
  added_by_jira_lint
-->
---

<!-- Please make sure you read the contribution guidelines and then fill
out the blanks below.

Please format the PR title appropriately based on the type of change:
  [JIRA-XXX]: <description>
-->

## Description

- Fix game config SimplySql PowerShell Module issues
  - Move script to workflow
  - Add steps in workflow to install modules
  - Specify version `1.9.1` for the module

## Related Links

<!-- List any links related to this pull request here

Replace "JIRA-XXX" with the your Jira issue key -->

- Jira Issue: DEVOPS-406
- Successful workflow run: [![🚀
Deploy](https://github.com/Andrews-McMeel-Universal/game_config/actions/workflows/deploy.yml/badge.svg?branch=bug%2FDEVOPS-406%2Fgame-config-package-issues)](https://github.com/Andrews-McMeel-Universal/game_config/actions/workflows/deploy.yml)
  • Loading branch information
ebronson68 authored Feb 15, 2024
1 parent e3d4ea0 commit bae0979
Showing 1 changed file with 144 additions and 1 deletion.
145 changes: 144 additions & 1 deletion .github/workflows/update-game-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,156 @@ jobs:
with:
ref: ${{ inputs.checkoutRef }}

- name: Login via Az module
uses: azure/login@v1
with:
creds: "${{ secrets.AZURE_CREDENTIALS }}"
enable-AzPSSession: true

- name: Generate .env file from Azure Key Vaults
uses: Andrews-McMeel-Universal/get-envs@v1
with:
azurecredentials: ${{ secrets.AZURE_CREDENTIALS }}
environment: ${{ inputs.environment }}

- name: Install SimplySql PowerShell module
shell: pwsh
run: |
Install-Module -Name SimplySql -RequiredVersion 1.9.1 -Force -AllowClobber
- name: Install Azure PowerShell module
shell: pwsh
run: |
Install-Module -Name Az -Force -AllowClobber
- name: Update Game Configs
shell: pwsh
run: |
./deployments/scripts/Update-CloudGameConfigs.ps1 -DBConnectionString "${{ env.DB_CONNECTION_STRING }}" -StorageAccountKey "${{ env.STORAGE_ACCOUNT_KEY }}" -ContainerName "${{ env.STORAGE_ACCOUNT_CONTAINER }}" -UpdatedBy 'GitHub Actions'
$StorageAccountName = "gameconfigs"
$StorageAccountTable = "GameConfigs"
$UpdatedBy = "AMU Agents"
$CDNBasePath = "gameconfigs.amuniversal.com"
$CacheControl = "max-age=86400"
Import-Module SimplySql
Import-Module Az
Function Get-MimeType {
param (
[parameter(Mandatory = $true)]
[string]$FileExtension
)
switch ($FileExtension) {
".png" { $MimeType = "image/png" }
".json" { $MimeType = "application/json" }
".jpg" { $MimeType = "image/jpeg" }
".jpeg" { $MimeType = "image/jpeg" }
".gif" { $MimeType = "image/gif" }
".svg" { $MimeType = "image/svg" }
}
return $MimeType
}
$BasePath = Get-Location
Write-Host "Base path is: $BasePath" -ForegroundColor DarkGray
$GameConfigsBasePath = Join-Path $BasePath $StorageAccountName
Write-Host "Game Configs base path is: $GameConfigsBasePath" -ForegroundColor DarkGray
# Script has to be ran from project basepath
if (!(Test-Path -Path $GameConfigsBasePath)) {
$CurrentLocation = Get-Location
$ErrorMessage = "Must be ran from project's base path. Current location is: $CurrentLocation"
Write-Error $ErrorMessage
throw $ErrorMessage
}
# Set Storage Context
$StorageContext = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey "${{ env.STORAGE_ACCOUNT_KEY }}"
try {
$Connection = New-Object Npgsql.NpgsqlConnection;
$Connection.ConnectionString = "${{ env.DB_CONNECTION_STRING }}"
$Connection.Open()
}
catch {
$ErrorMessage = "Connection Failed Error message was: $_.Exception.Message"
Write-Error $ErrorMessage
throw $ErrorMessage
}
# Remove Existing Storage Blobs
Write-Host "Removing existing blobs in '${{ env.STORAGE_ACCOUNT_CONTAINER }}' container." -ForegroundColor DarkGray
Get-AzStorageBlob -Container "${{ env.STORAGE_ACCOUNT_CONTAINER }}" -Context $StorageContext | Remove-AzStorageBlob
# Remove entries in GameConfigs table.
Write-Host "Removing current data in '$StorageAccountTable' table." -ForegroundColor DarkGray
$DeleteQuery = "DELETE FROM ""$StorageAccountTable"""
try {
$Command = $Connection.CreateCommand()
$Command.CommandText = $DeleteQuery
[Void]$Command.ExecuteNonQuery()
}
catch {
$ErrorMessage = "Error on Query. Query was: '$DeleteQuery'. Error is: $_.Exception.Message"
Write-Error $ErrorMessage
throw $ErrorMessage
}
$Features = Get-ChildItem -path $GameConfigsBasePath -Directory
$Features | ForEach-Object {
$FeatureId = $_.Name
$Products = Get-ChildItem -Path (Join-Path $GameConfigsBasePath $FeatureId) -Directory
$Products | ForEach-Object {
$Product = $_.Name
$GameFilesPath = $_.FullName
$GameFiles = Get-ChildItem -Path $GameFilesPath -File
$GameFiles | ForEach-Object {
$FileName = $_.name
$FilePath = $_.FullName
$FileMimeType = Get-MimeType($_.Extension)
if (!$FileMimeType) {
$ErrorMessage = "File type not included in MimeType list. Please add it before trying again."
Write-Error $ErrorMessage
throw $ErrorMessage
}
$BlobName = "$FeatureId/$Product/$FileName"
$BlobProperties = @{"ContentType" = $FileMimeType; "CacheControl" = $CacheControl; };
$BlobMetaData = @{"FeatureId" = $FeatureId }
# Create Blob
Write-Host "Updating Storage Blob Content with file: '$FilePath'" -ForegroundColor DarkGray
Set-AzStorageBlobContent -File $FilePath -Container "${{ env.STORAGE_ACCOUNT_CONTAINER }}" -Blob $BlobName -Properties $BlobProperties -Context $StorageContext `
-Metadata $BlobMetaData -Force
# Update Database
$UriPath = "$CDNBasePath/${{ env.STORAGE_ACCOUNT_CONTAINER }}/$BlobName"
$UpdateDate = (Get-Date -Format "yyyy-MM-dd HH:mm:ss")
$InsertQuery = "INSERT INTO ""GameConfigs"" (""ConfigName"", ""UriPath"", ""MimeType"", ""OriginalFileName"", ""FeatureId"", ""CreatedDate"", ""CreatedBy"", `
""ModifiedDate"", ""ModifiedBy"", ""ActiveFlag"") `
VALUES ('$Product', '$UriPath', '$FileMimeType', '$FileName', '$FeatureId', '$UpdateDate', '$UpdatedBy', '$UpdateDate', `
'$UpdatedBy', '1') "
try {
$Command = $Connection.CreateCommand()
$Command.CommandText = $InsertQuery
[Void]$Command.ExecuteNonQuery()
}
catch {
$ErrorMessage = "Error on Query. Query was: $InsertQuery. Error is: $_.Exception.Message"
Write-Error $ErrorMessage
throw $ErrorMessage
}
}
}
}
[Void]$Connection.Close()

0 comments on commit bae0979

Please sign in to comment.