Search code examples
gitlab-ciflyway

Run flyway in gitlab-ci


I use flyway container and it works well locally by: docker run -v ${PWD}/db/migration:/flyway/sql --rm flyway/flyway -mixed=true -url='jdbc:sqlserver://my-server.net;databaseName=TEST_DB' -user=$MSSQL_USER -password=$MSSQL_PW migrate

I tried to use it in gitlab-ci, but the following does not work because the docker run part throws error - cannot connect to docker daemon.

stages:
  - test
  - deploy

test-migration:
  stage: test
  services:
    - microsoft/mssql-server-linux:2017-latest-ubuntu
    - docker-hub/docker:dind
  variables:
    ACCEPT_EULA: 'Y'
    SA_PASSWORD: 'YourStrong!Passw0rd'
  script:
    - docker run -v ${PWD}/db/migration:/flyway/sql --rm flyway/flyway -mixed=true -url='jdbc:sqlserver://ssql-server-linux:1433;databaseName=TEST_DB' -user=SA -password='YourStrong!Passw0rd' migrate

deploy:
  stage: deploy
  services:
    - docker-hub/docker:dind
  script:
    - docker run -v ${PWD}/db/migration:/flyway/sql --rm flyway/flyway -mixed=true -url='jdbc:sqlserver://my-server.net;databaseName=TEST_DB' -user=$MSSQL_USER -password=$MSSQL_PW migrate

Instead of calling docker run, should use

image: flyway/flyway , and calling the command in the script section.

But I'm wondering about mounting the volume that is not supported in gitlab CI yet. https://gitlab.com/gitlab-org/gitlab-runner/issues/3207

Any idea?


Solution

  • cannot connect to docker daemon means DOCKER_HOST is missing

    After adding DOCKER_HOST in variables this works!

    https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-workflow-with-docker-executor

    Mounting a volume also works, and the following works well.

    script:
        - docker run -v ${PWD}/db/migration:/flyway/sql --rm flyway/flyway -mixed=true -url='jdbc:sqlserver://ssql-server-linux:1433;databaseName=TEST_DB' -user=SA -password='YourStrong!Passw0rd' migrate