Reputation: 174
I'm trying to build a CI Pipeline with Gitlab CI/CD. My project is a really simple API based on Symfony. To create a consistent environment i'm using docker-compose with four very simple containers (nginx,PHP,MySQL & composer). My .gitlab-ci.yaml looks like this:
stages:
- setup
setup:
stage: setup
before_script:
- docker-compose up -d
script:
- sleep 15
- docker-compose exec -T php php bin/console doctrine:schema:create
after_script:
- [...]
- docker-compose down
The problem I'm encountering is that the ci script does not wait till the docker-compose up -d
is finished. To bypass this I've added this stupid sleep.
Is there a better way to do this?
Upvotes: 9
Views: 12800
Reputation: 69
To save some time for the people searching it, I implemented the solution commented by @gbrener.
The idea: Wait until getting the log that shows that the container is up, then continue the pipeline.
1 - Get the log to be the checkpoint. I used the last log of my container. Ex: Generated backend app.
2 - Get the container name. Ex: ai-server-dev.
3 - Create a sh script like the below and name it something.sh. Ex:
#!/bin/bash
while ! docker logs ai-server-dev --tail=1 | grep -q "Generated backend app";
do
sleep 10
echo "Waiting for backend to load ..."
done
4 - Replace the 'sleep 15' with 'sh wait_server.sh' as in the question to run the script in your pipeline.
Upvotes: 3