Skip to content

Commit

Permalink
fix(azure-migrations): changed the pipeline to run the migrations and…
Browse files Browse the repository at this point in the history
… seeders in a different tasks

Signed-off-by: Kalil Teixeira Ventura Monteiro <[email protected]>
  • Loading branch information
kalilventura committed Oct 23, 2024
1 parent 5326cce commit 618a2a2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Exceptions are acceptable depending on the circumstances (critical bug fixes tha
- changed `javascript` pipeline for `azure-devops` to publish the code coverage in `sonarqube`
- changed `Replace Azure Function Variables` script to modify all variables in a single command
- changed `golang` pipeline for `azure-devops` to use caching in the `delivery` stage
- changed the `azure-devops` to execute the migrations and seeders in a different tasks

### Fixed

Expand Down
84 changes: 67 additions & 17 deletions azure-devops/golang/stages/40-delivery/arm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ stages:
variables:
GOPATH: "$(Build.SourcesDirectory)/.go"
MIGRATIONS_CACHE: "$(Build.SourcesDirectory)/.migration"
SEEDERS_CACHE: "$(Build.SourcesDirectory)/.seeder"
AZURE_DEPLOY_CACHE: "$(Build.SourcesDirectory)/.azuredeploy"
PIPELINE_FIREWALL_NAME: 'PipelineFirewall_$(date +"%Y%m%d%H%M%S")'
steps:
# TODO: check if this is necessary
- script: |
set -e
mkdir -p $(MIGRATIONS_CACHE)
mkdir -p $(SEEDERS_CACHE)
mkdir -p $(AZURE_DEPLOY_CACHE)
displayName: 'Setup Cache'
Expand All @@ -26,12 +29,20 @@ stages:

- task: 'CacheBeta@1'
inputs:
key: 'go-migrations|db/migrations/*.sql|db/seeders/*.sql'
key: 'go-migrations|db/migrations/*.sql'
path: "$(MIGRATIONS_CACHE)"
cacheHitVar: 'MIGRATIONS_CACHE_HIT'
displayName: 'Cache Migrations'
continueOnError: true

- task: 'CacheBeta@1'
inputs:
key: 'go-migrations|db/seeders/*.sql'
path: "$(SEEDERS_CACHE)"
cacheHitVar: 'SEEDERS_CACHE_HIT'
displayName: 'Cache Seeders'
continueOnError: true

- task: 'Cache@2'
inputs:
key: 'azure-deploy|azuredeploy.json'
Expand Down Expand Up @@ -171,7 +182,26 @@ stages:
- task: 'AzureCLI@2'
condition: eq(variables['MIGRATIONS_CACHE_HIT'], 'false')
displayName: 'Migrations'
displayName: 'Create Flexible Firewall Rule to execute migrations and seeders'
inputs:
azureSubscription: "$(AZM_SERVICE_CONNECTION)"
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
set -e
DATABASE_SERVER_NAME=$(echo $OUTPUTS | jq -r '.databaseServerName.value')
PIPELINE_IP=$(curl -s ifconfig.me/ip)
az postgres flexible-server firewall-rule create \
--resource-group "$(resourceGroupName)" \
--name "$DATABASE_SERVER_NAME" \
--rule-name "$(PIPELINE_FIREWALL_NAME)" \
--start-ip-address "$PIPELINE_IP"
- task: 'AzureCLI@2'
condition: eq(variables['MIGRATIONS_CACHE_HIT'], 'false')
displayName: 'Execute Migrations'
inputs:
azureSubscription: "$(AZM_SERVICE_CONNECTION)"
scriptType: 'bash'
Expand All @@ -181,36 +211,43 @@ stages:
# TODO: move this "IF" to a file and (or) use an Azure resource to create a condition
if grep -r 'goose' db/migrations/*.sql; then
DATABASE_SERVER_NAME=$(echo $OUTPUTS | jq -r '.databaseServerName.value')
PORT=$(echo $OUTPUTS | jq -r '.databaseServerPort.value')
HOST=$(echo $OUTPUTS | jq -r '.databaseServerHost.value')
USER=$(echo $OUTPUTS | jq -r '.databaseServerUsername.value')
PASSWORD=$(echo $OUTPUTS | jq -r '.databaseServerPassword.value')
DBNAME=$(echo $OUTPUTS | jq -r '.databaseName.value')
SSL=$(echo $OUTPUTS | jq -r '.databaseServerSSL.value')
PIPELINE_IP=$(curl -s ifconfig.me/ip)
PIPELINE_FIREWALL_NAME="PipelineFirewall_$(date +"%Y%m%d%H%M%S")"
# TODO: create firewall for others databases
# TODO: this command is here because, without it, the code wasn't authorized, need to investigate the reason
az postgres flexible-server firewall-rule create \
--resource-group "$(resourceGroupName)" \
--name "$DATABASE_SERVER_NAME" \
--rule-name "$PIPELINE_FIREWALL_NAME" \
--start-ip-address "$PIPELINE_IP"
CONNECTION_STRING="host=$HOST port=$PORT user=$USER password=$PASSWORD dbname=$DBNAME sslmode=$SSL"
go install github.com/pressly/goose/v3/cmd/[email protected]
CONNECTION_STRING="host=$HOST port=$PORT user=$USER password=$PASSWORD dbname=$DBNAME sslmode=$SSL"
$(go env GOPATH)/bin/goose -dir db/migrations postgres "$CONNECTION_STRING" up
else
echo "No Goose migration files found. Skipping Goose installation and migration."
fi
- task: 'AzureCLI@2'
condition: eq(variables['SEEDERS_CACHE_HIT'], 'false')
displayName: 'Execute Seeders'
inputs:
azureSubscription: "$(AZM_SERVICE_CONNECTION)"
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
set -e
if grep -r 'goose' db/seeders/*.sql; then
PORT=$(echo $OUTPUTS | jq -r '.databaseServerPort.value')
HOST=$(echo $OUTPUTS | jq -r '.databaseServerHost.value')
USER=$(echo $OUTPUTS | jq -r '.databaseServerUsername.value')
PASSWORD=$(echo $OUTPUTS | jq -r '.databaseServerPassword.value')
DBNAME=$(echo $OUTPUTS | jq -r '.databaseName.value')
SSL=$(echo $OUTPUTS | jq -r '.databaseServerSSL.value')
CONNECTION_STRING="host=$HOST port=$PORT user=$USER password=$PASSWORD dbname=$DBNAME sslmode=$SSL"
go install github.com/pressly/goose/v3/cmd/[email protected]
for dir in "" "dev" "prod"; do
if [ -z "$dir" ] || [ "$ENVIRONMENT" == "$dir" ]; then
dir_path="db/seeders/$dir"
Expand All @@ -224,10 +261,23 @@ stages:
echo "No Goose seed files found. Skipping seed"
fi
- task: 'AzureCLI@2'
condition: eq(variables['MIGRATIONS_CACHE_HIT'], 'false')
displayName: 'Delete Flexible Firewall Rule to execute migrations and seeders'
inputs:
azureSubscription: "$(AZM_SERVICE_CONNECTION)"
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
set -e
DATABASE_SERVER_NAME=$(echo $OUTPUTS | jq -r '.databaseServerName.value')
PIPELINE_IP=$(curl -s ifconfig.me/ip)
az postgres flexible-server firewall-rule delete --yes \
--resource-group "$(resourceGroupName)" \
--name "$DATABASE_SERVER_NAME" \
--rule-name "$PIPELINE_FIREWALL_NAME"
--rule-name "$(PIPELINE_FIREWALL_NAME)"
- task: 'AzureCLI@2'
displayName: 'Publish Function'
Expand Down

0 comments on commit 618a2a2

Please sign in to comment.