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.
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:
setup
and test
in the same jobsetup
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With