Skip to content

Commit

Permalink
Merge pull request #2105 from unboxed/remove-db-migrate-task
Browse files Browse the repository at this point in the history
Remove db-migrate task
  • Loading branch information
benjamineskola authored Jan 13, 2025
2 parents e2468be + af188f2 commit 9e1cf5d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 65 deletions.
65 changes: 1 addition & 64 deletions .github/workflows/deploy-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,73 +71,10 @@ jobs:
-t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \
-f Dockerfile.production .
deploy-db-migrate-service:
name: Perform database migrations on ${{ inputs.environment-name }}
runs-on: ubuntu-24.04
needs: [build-image]

steps:
- name: Get github commit sha
id: github
run: |
echo "sha=$(echo ${GITHUB_SHA::7})" >>$GITHUB_OUTPUT
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/bops_github_deploy_${{ inputs.environment-name }}
aws-region: eu-west-2

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2

- name: Get image URI
id: ecr-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: bops/${{ inputs.environment-name }}
IMAGE_TAG: ${{ steps.github.outputs.sha }}
run: |
echo "uri=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >>$GITHUB_OUTPUT
- name: Download task definition for db_migrate, strip unused properties and update image to latest image_arn
env:
IMAGE_ARN: ${{ steps.ecr-image.outputs.uri }}
run: |
aws ecs describe-task-definition --task-definition bops-db-migrate-${{ inputs.environment-name }} --query taskDefinition | \
jq -r 'del(.compatibilities, .taskDefinitionArn, .requiresAttributes, .revision, .status, .registeredAt, .registeredBy)' | \
jq -r '.containerDefinitions[].image = "'$IMAGE_ARN'"' > db_migrate.json
- name: Register db_migrate on a new revision
run: |
aws ecs register-task-definition \
--cli-input-json file://db_migrate.json
- name: Run db_migrate
env:
CLUSTER: bops-${{ inputs.environment-name }}
TASK_DEFINITION: bops-db-migrate-${{ inputs.environment-name }}
LOG_GROUP: bops-db-migrate-${{ inputs.environment-name }}
BOPS_SUBNETS: subnet-${{ secrets[format('{0}', inputs.db_subnets)] }}
BOPS_SG: sg-${{ secrets[format('{0}', inputs.db_sg)] }}
run: |
start_time=$(date -Iseconds)
task_arn=$(aws ecs run-task --cluster $CLUSTER \
--task-definition $TASK_DEFINITION --launch-type FARGATE \
--network-configuration '{"awsvpcConfiguration": {"subnets": ["'$BOPS_SUBNETS'"],"securityGroups": ["'$BOPS_SG'"],"assignPublicIp": "ENABLED"}}' | \
jq -r '.tasks[].taskArn')
aws ecs wait tasks-stopped --cluster $CLUSTER --tasks "$task_arn"
aws logs tail $LOG_GROUP --format short --since $start_time
result_json=$(aws ecs describe-tasks --cluster $CLUSTER --task $task_arn)
exit_code=$(echo "$result_json" | jq -r '.tasks[].containers[0].exitCode // 1')
if [ $exit_code -gt 0 ]; then echo "$result_json" | jq -r; fi
exit $exit_code
deploy-services:
name: Deploy service ${{ matrix.service_type }} to ${{ inputs.environment-name }}
runs-on: ubuntu-24.04
needs: [build-image, deploy-db-migrate-service]
needs: [build-image]
strategy:
matrix:
service_type: ["console", "worker", "web"]
Expand Down
2 changes: 1 addition & 1 deletion bops-applicants
19 changes: 19 additions & 0 deletions config/initializers/automigrate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

Rails.application.config.after_initialize do
if defined?(Rails::Server) || defined?(Sidekiq::Processor)
connection = ActiveRecord::Tasks::DatabaseTasks.migration_connection
lock_id = ActiveRecord::Migrator::MIGRATOR_SALT * Zlib.crc32(connection.current_database)

if connection.get_advisory_lock(lock_id)
connection.release_advisory_lock(lock_id)
ActiveRecord::Tasks::DatabaseTasks.migrate
else
until connection.get_advisory_lock(lock_id)
warn "waiting 5s for migration lock ..."
sleep 5
end
connection.release_advisory_lock(lock_id)
end
end
end

0 comments on commit 9e1cf5d

Please sign in to comment.