Robert Strauch
Robert Strauch

Reputation: 12896

Automate Flyway migration with Docker and Jenkins

I'd like to automate the Flyway migrations for our MariaDB database. For testing purposes I added the following service to my docker-compose.yml running only the info command.

  flyway:
    image: boxfuse/flyway:5.2.4
    command: -url=jdbc:mariadb://mariadb_service -schemas=$${MYSQL_DATABASE} -table=schema_version -connectRetries=60 info
    volumes:
      - ./db/migration:/flyway/sql
    depends_on:
      - mariadb_service

This seems to be working, i.e. I can see the output of info.

Now I'd like to take this idea one step further and integrate this into our Jenkins build pipeline. This is where I get stuck.

  1. If I deployed the Docker stack with the above docker-compose.yml in my Jenkinsfile would the corresponding stage fail upon errors during the migration? Speaking, would Jenkins notice that error?
  2. If this is not true how can I integrate the Flyway migration in my Jenkins pipeline? I found that there is a Flyway Runner plugin but I didn't see if this can connect to a database in a Docker stack deployed by the Jenkinsfile

Upvotes: 1

Views: 2965

Answers (1)

Marcin Kłopotek
Marcin Kłopotek

Reputation: 5931

You can use Jenkins built-in support for Docker. Then your pipeline script may contain the stage

stage('Apply DB changes') {

    agent {
        docker {
            image 'boxfuse/flyway:5.2.4'
            args '-v ./db/migration:/flyway/sql --entrypoint=\'\''
        }
    }

    steps {
        sh "/flyway/flyway -url=jdbc:mariadb://mariadb_service -schemas=${MYSQL_DATABASE} -table=schema_version -connectRetries=60 info"
    }

}

This way the steps will be executed within temporary Docker container created by Jenkins agent from boxfuse/flyway image. If the command fails the entire stage will fail as well.

Upvotes: 4

Related Questions