Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab CI: Persist MySQL data between stages

How do I persist MySQL data between different build stages?

I have MySQL set up as a service however, when going from a build to a deploy stage the MySQL database is empty and I'm unable to find any information on this.

Example gitlab-ci.yml:

image: php:latest
services:
  - mysql:5.6
stages:
  - build
  - deploy

build:
  stage: build
  script:
    - [INSERT MYSQL DATA]
  allow_failure: false
  artifacts:
    when: on_success
    paths:
      - /var/lib/mysql/
deploy:
  stage: deploy
  script:
    - [MYSQL DUMP]
  dependencies:
    - build
  allow_failure: false
  when: on_success

The MySQL dump would be empty despite successfully inserting the data at the build stage.

like image 916
Adam Johnson Avatar asked Feb 20 '17 19:02

Adam Johnson


1 Answers

As per the Gitlab issues page: Share service between build stages. The way that Gitlab-CI is set up is such that:

each job is run in each job is run independently of the others, including potentially running on different machines. And the services are run locally on each runner. There's no way for one job to access a service on a different runner. On GitLab.com, we even recycle the machine after each job runs so the services would all be destroyed. @markpundsack

Although that is 7 months old, the issue is still open at this time. The issue of getting the data from mysql is also a problem as services do not mount volumes (I do not believe any plans exist to add this feature), so the data is stuck inside the mysql container.

So you can handle this in a couple of ways:

  1. Have the setup and test in the same job
  2. Create an SQL docker with all data added already (bad practice but if you need it)
  3. Create the dump in setup and share as artifact then reload that data with test

EDIT: Adding a note that using the expire_in component for artifacts may be useful as you don't want to store a bunch of data you may not need forever.

EDIT2: After researching some more, I found this by gitlab. You can do the following:

services:
- mysql
variables:
  # Configure mysql service (https://hub.docker.com/_/mysql/)
  MYSQL_DATABASE: hello_world_test
  MYSQL_ROOT_PASSWORD: mysql
connect:
  image: mysql
  script:
  - echo "SELECT 'OK';" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql "$MYSQL_DATABASE"
  - SOME DATABASE DUMPING
 artifacts:
    when: on_success
    paths:
      - /var/lib/mysql/
like image 138
jrbeverly Avatar answered Oct 18 '22 09:10

jrbeverly