Nick
Nick

Reputation: 10153

Build FAILED but job status is SUCCESS in Gitlab

My Dockerfile:

FROM mm_php:7.1

ADD ./docker/test/source/entrypoint.sh /work/entrypoint.sh
ADD ./docker/wait-for-it.sh /work/wait-for-it.sh
RUN chmod 755 /work/entrypoint.sh \
    && chmod 755 /work/wait-for-it.sh

ENTRYPOINT ["/work/entrypoint.sh"]

entrypoint.sh:

#!/bin/bash -e

/work/wait-for-it.sh db:5432 -- echo "PostgreSQL started"

./vendor/bin/parallel-phpunit --pu-cmd="./vendor/bin/phpunit -c phpunit-docker.xml" tests

docker-compose.yml:

version: '2'
services:

  test:
    build:
      context: .
      args:
        ssh_prv_key: ${ssh_prv_key}
        application_env: ${application_env}
      dockerfile: docker/test/source/Dockerfile
    links:
      - db

  db:
    build:
      context: .
      dockerfile: docker/test/postgres/Dockerfile
    environment:
      PGDATA: /tmp

.gitlab-ci.yml:

image: docker:latest

services:
  - name: docker:dind
    command: ["--insecure-registry=my.domain:5000 --registry-mirror=http://my.domain"]

before_script:
  - apk add --no-cache py-pip
  - pip install docker-compose
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts
  - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
  - chmod 600 ~/.ssh/id_rsa
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

test:
  stage: test
  script:
    - export ssh_prv_key="$(cat ~/.ssh/id_rsa)" && export application_env="testing-docker" && docker-compose up --build test

All works good. But if tests are failed, status of job in Gitlab is SUCCESS instead of FAILED.

How to obtain status FAILED if tests are failed?

UPD

If I run docker-compose up locally, it return no error code:

$ export ssh_prv_key="$(cat ~/.ssh/id_rsa)" && export application_env="testing-docker" && docker-compose up --build test
Building db
Step 1/2 : FROM mm_postgres:9.6
...
test_1       | FAILURES!
test_1       | Tests: 1, Assertions: 1, Failures: 1.
test_1       | Success: 2 Fail: 2 Error: 0 Skip: 2 Incomplete: 0
mmadmin_test_1 exited with code 1

$ echo $?
0

Upvotes: 2

Views: 1933

Answers (2)

Venantius
Venantius

Reputation: 2539

It looks to me like it's reporting failed on the test without necessarily reporting failure on the return value of the docker-compose call. Have you tried capturing the return value of docker-compose when tests fail locally?

In order to get docker-compose to return the exit code from a specific service, try this:

docker-compose up --exit-code-from=service

Upvotes: 7

Bezerra
Bezerra

Reputation: 146

When Gitlab CI runs something, if the process executed returns something different from zero, then, your build will fail.

In your case, you are running a docker-compose and this program returns zero when the container finish, what is correct.

You are trying to get phpunit's failure.

I think that is better you split your build in steps and not use docker-compose in this case:

gitlab.yml:

stages:
  - build
  - test

build:
  image: docker:latest
  stage: build
  script:
    - docker build -t ${NAME_OF_IMAGE} .
    - docker push ${NAME_OF_IMAGE}

test:
  image: ${NAME_OF_IMAGE}
  stage: test
  script:
    - ./execute_your.sh

Upvotes: 2

Related Questions