Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate db in django using CI/CD with Google CloudSQL?

I have a django repository setup in gitlab and I am trying to automate build and deploy on google cloud using gitlab CI/CD.

The app has to be deployed on App Engine and has to use CloudSQL for dynamic data storage.

Issue that I am facing is while executing migration on the db, before deploying my application.

I am supposed to run ./manage.py migrate which connects to cloudSQL.

I have read that we can use cloud proxy to connect to cloudSQL and migrate db. But it kind of seems like a hack. Is there a way to migrate my db via CI/CD pipeline script?

Any help is appreciated. Thanks.

like image 220
Saket Patel Avatar asked Oct 15 '25 04:10

Saket Patel


1 Answers

When running Django in the App Engine Standard environment the recommended way of approaching database migration is to run ./manage.py migrate directly from the Console shell or from your local machine (which requires using the cloud sql proxy).

If you want the database migration to be decoupled from your application deployment and run it in Gitlab CI/CD you could do something along these lines:

  • Use as base image google/cloud-sdk:latest
  • Acquire credentials gcloud auth activate-service-account --key-file $GOOGLE_SERVICE_ACCOUNT_FILE
  • Download the cloudsqlproxy with wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy and make it executable chmod +x cloud_sql_proxy.
  • Start the proxy ./cloud_sql_proxy -instances="[YOUR_INSTANCE_CONNECTION_NAME]"=tcp:3306.
  • Finally, run the migration script.

You might also create a custom docker image that already does what's above behind the scenes, the result would be the same.

If you want to read further on the matter I suggest taking a look at the following articles link1 link2.

like image 83
Happy-Monad Avatar answered Oct 17 '25 18:10

Happy-Monad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!