Umberto D'Ovidio

Setup a Github Worflow for migrating your Cloud SQL instance with Flyway

I’ve recently wrote a blog post on how to setup the connection between Flyway and Google Cloud SQL. In this post I will explain how to create a Github workflow that will run the flyway migration once triggered.

You will need of course a yaml file in your github repo under the directory .github/workflows. Here’s the code

 1name: Migrate
 2on: [workflow_dispatch]
 3
 4jobs:
 5  migrate:
 6    runs-on: ubuntu-latest
 7    env:
 8      FLYWAY_URL: ${{ secrets.CLOUD_SQL_JDBC_URL }}
 9      FLYWAY_USER: ${{ secrets.CLOUD_SQL_USER }}
10      FLYWAY_PASSWORD: ${{ secrets.CLOUD_SQL_PASSWORD }}
11
12    steps:
13      - uses: actions/checkout@v2
14      - uses: google-github-actions/setup-gcloud@master
15        with:
16          project_id: ${{ secrets.GCP_PROJECT_ID }}
17          service_account_key: ${{ secrets.GCP_SA_KEY}}
18          export_default_credentials: true
19      - name: Set up JDK 1.11
20        uses: actions/setup-java@v1
21        with:
22          java-version: 1.11
23      - name: Get Flyway info
24        run: mvn flyway:info
25      - name: Migrate
26        run: mvn flyway:migrate

I’ve used on: [workflow_dispatch] so that this action can be triggered only manually. Then I’ve set some environment variables that specify the credentials and the url used for the jdbc connection. Then I’m checking out the project, and exporting the default credentials. The GCP_PROJECT_ID should contain the id of your project, while the GCP_SA_KEY will contain your service account in json format. The export default credentials will then populate the GOOGLE_APPLICATION_CREDENTIALS which is needed for the Cloud SQL connection. I’m then setting up java with the setup-java action (which includes setting up maven), and finally I’m printing some info from flyway before executing the migration. That’s it, you can now run your migrations with a click from github :)

Github Action