Share service between build stages
Looking at services documentation gave me the illusion that when services are defined in root scope, they will be long lived and shared between all stages of a build. However I realised this is not the case, but it could be a great feature.
The benefit is clear: lets say I have a Java app that needs OpenJDK image to run. The database however is a PostgreSQL. This means in current setup and I need it to run integration tests. This means in current setup I have to take the OpenJDK image, install at least PostgreSQL client on it using shell scripts, and then I can run my tests. But if it was possible to keep the service running, I could have a DB setup stage that uses postgres image, runs before the java test stages and sets up DB.
As an example following build script could be considered:
stages:
- setup
- test
# lets imagine this service is shared between all stages:
services:
- postgres:9.4
variables:
POSTGRES_DB: pgdb
POSTGRES_USER: pguser
POSTGRES_PASSWORD: pgpass
# this stage sets up DB, no installation needed
SetupDB:
image: postgres:9.4
stage: setup
script:
# official way to provide password to psql: http://www.postgresql.org/docs/9.3/static/libpq-envars.html
- export PGPASSWORD=$POSTGRES_PASSWORD
- psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -f "path/to/some/file.sql"
# now we have a ready DB for running tests, independent of image used for tests
BuildAndTest:
image: mohamnag/gradle:2.12
stage: build
artifacts:
paths:
- build/libs/*.war
script:
# build and run unit tests
- gradle clean build
# run integration tests
- gradle cleanTest integrationTest