Mongodb Server Doesn't Start at Gitlab Runner Using Gitlab-Ci

GitLab CI stuck on running NodeJS server

Note: solution works fine when using a gitlab runner with shell executor

Generally in Gitlab CI we run ordered jobs with specific tasks that should be executed one after the end of the other.

So for the job build we have the npm install -q command that runs and terminates with an exit status (0 exit status if the command was succesful), then runs the next command npm run build and so on until the job is terminated.

For the test job we have npm start & process that keeps running so the job wont be able to terminate.

The problem is that sometimes we need to have some process that need to run in background or having some process that keeps living between tasks. For example in some kind of test we need to keep the server running, something like that:

test:
stage: test
script:
- npm start
- npm test

in this case npm test will never start because npm statrt keeps running without terminating.

The solution is to use before_script where we run a shell script that keeps npm start process running then we call after_script to kill that npm start process

so on our .gitlab-ci.yml we write

test:
stage: test
before_script:
- ./serverstart.sh
script:
- npm test
after_script:
- kill -9 $(ps aux | grep '\snode\s' | awk '{print $2}')

and on the serverstart.sh

# !/bin/bash

# start the server and send the console and error logs on nodeserver.log
npm start > nodeserver.log 2>&1 &

# keep waiting until the server is started
# (in this case wait for mongodb://localhost:27017/app-test to be logged)
while ! grep -q "mongodb://localhost:27017/app-test" nodeserver.log
do
sleep .1
done
echo -e "server has started\n"
exit 0

thanks to that serverstart.sh script is terminated while keeping npm start process alive and help us by the way move to the job where we have npm test.

npm test terminates and pass to after script where we kill all nodejs process.

ECONNREFUSED when attempting to access Docker service in GitLab CI

I finally figured this out, following 4 days of reading, searching and lots of trial and error. The job running the tests was in a different container from the ones that exposed the API and the database.

I resolved this by creating a docker network in the device the runner was on:

sudo network create mynetwork

Following that, I set the network to the docker-compose.yml file, with external config, and associated both services with it:

  st-sample:
# ....
networks:
- mynetwork

mongo:
# ....
networks:
- mynetwork

networks:
mynetwork:
external: true

Also, I created a custom docker image including tests (name: test),
and in gitlab-ci.yml, I setup the job to run it within mynetwork.

docker run --network=mynetwork test

Following that, the containers/services were accessible by their names along each other, so I was able to run tests against http://st-sample.

It was a long journey to figure it all out, but it was well-worth it - I learned a lot!

gitlab CI/CD with maven doesn't setup environment variables in application.properties

Educated guess: you haven't enabled maven filtering for your application.properties property file.

Without filtering, those placeholders won't be replaced.

So have something like this in your pom file:

<project>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
</resources>
...
</build>
...
</project>


Related Topics



Leave a reply



Submit