JorgeeFG
JorgeeFG

Reputation: 5981

Trying to execute tests in Jenkins, but vendor dir is not there

I don't know what I am missing. I have a docker image, and a docker compose file.

My goal is to execute PHP Unit tests in Jenkins

I guess this

volumes:
    - .:/var/www/html

is the source of the problems, but shouldn't the container last image have already the vendor directory?

Dockerfile

FROM quay/php-base:7.2.
COPY . /var/www/html

RUN curl -sS https://getcomposer.org/installer | php
RUN php composer.phar install
RUN rm composer.phar

EXPOSE 9000

CMD ["php-fpm"]

build.testing.yml

version: "3"

services:
    phpfpm:
        build:
            context: .
            dockerfile: Dockerfile
        image: xxx-phpfpm:${GIT_COMMIT}
        environment:
            - APP_ENV=testing
        volumes:
            - .:/var/www/html

Jenkins build shell command

printenv
echo $GIT_BRANCH
echo $GIT_COMMIT
docker-compose -f build.testing.yml up -d --build --remove-orphans
docker-compose -f build.testing.yml exec -T phpfpm bash build/phpunit.sh

phpunit.sh

#!/usr/bin/env bash
cd /var/www/html;
ls -la;
ls -la vendor;
ls -la vendor/bin;
php vendor/bin/phpunit;
exit $?

Build log (last lines)

phpunit/php-code-coverage suggests installing ext-xdebug (^2.6.0)
phpunit/phpunit suggests installing ext-soap (*)
phpunit/phpunit suggests installing ext-xdebug (*)
phpunit/phpunit suggests installing phpunit/php-invoker (^2.0)
Generating optimized autoload files

Removing intermediate container 56245736b26b
 ---> 818e587ac18a
Step 6/8 : RUN rm composer.phar
 ---> Running in 5ccbffc37e05

Removing intermediate container 5ccbffc37e05
 ---> 086e88f369fc
Step 7/8 : EXPOSE 9000
 ---> Running in 1c10e32ba134
Removing intermediate container 1c10e32ba134
 ---> 84eb9cbc3477
Step 8/8 : CMD ["php-fpm"]
 ---> Running in b6381d9b0f9a
Removing intermediate container b6381d9b0f9a
 ---> 40b32fa81d5f
Successfully built 40b32fa81d5f
Successfully tagged xxx-phpfpm:aa01355d1e8ab6d0f3daf87fb8f1f3b1be3e45be
Recreating xxxmicroservicetesting_phpfpm_1 ... 

Recreating xxxmicroservicetesting_phpfpm_1 ... done
+ docker-compose -f build.testing.yml exec -T phpfpm bash build/phpunit.sh
total 224
drwxrwxr-x   13 1002     1002          4096 Jul 29 19:35 .
drwxr-xr-x    1 root     root            18 May  5  2018 ..
-rw-rw-r--    1 1002     1002           213 Jul 26 15:38 .editorconfig
-rw-rw-r--    1 1002     1002           388 Jul 26 15:38 .env.example
drwxrwxr-x    8 1002     1002           162 Jul 29 19:41 .git
-rw-rw-r--    1 1002     1002            55 Jul 26 15:38 .gitignore
-rw-rw-r--    1 1002     1002            71 Jul 26 15:38 .styleci.yml
-rw-rw-r--    1 1002     1002           232 Jul 26 15:38 Dockerfile
-rw-rw-r--    1 1002     1002           895 Jul 29 18:25 Jenkinsfile
drwxrwxr-x   16 1002     1002           240 Jul 26 15:38 app
-rw-rw-r--    1 1002     1002          1094 Jul 26 15:38 artisan
drwxrwxr-x    2 1002     1002            21 Jul 26 15:38 bootstrap
drwxrwxr-x    2 1002     1002            24 Jul 29 19:41 build
-rw-rw-r--    1 1002     1002           250 Jul 29 19:35 build.testing.yml
-rw-rw-r--    1 1002     1002          1256 Jul 26 15:38 composer.json
-rw-rw-r--    1 1002     1002        179335 Jul 26 15:38 composer.lock
drwxrwxr-x    2 1002     1002            45 Jul 26 15:38 config
drwxrwxr-x    5 1002     1002            54 Jul 26 15:38 database
-rw-rw-r--    1 1002     1002           193 Jul 26 15:38 phpcs.xml
-rw-rw-r--    1 1002     1002           924 Jul 26 15:38 phpunit.xml
drwxrwxr-x    2 1002     1002            40 Jul 26 15:38 public
drwxrwxr-x    3 1002     1002            19 Jul 26 15:38 resources
drwxrwxr-x    2 1002     1002            40 Jul 26 15:38 routes
drwxrwxr-x    5 1002     1002            46 Jul 26 15:38 storage
drwxrwxr-x    4 1002     1002           113 Jul 26 15:38 tests
ls: vendor: No such file or directory
ls: vendor/bin: No such file or directory
Could not open input file: vendor/bin/phpunit
Build step 'Execute shell' marked build as failure
Finished: FAILURE

Upvotes: 1

Views: 532

Answers (1)

David Maze
David Maze

Reputation: 159875

When you have a volumes: directive in a docker-compose.yml like you show, it tells Docker to completely ignore the contents of the Docker image and use whatever's in the local filesystem. You should remove that.

If you're running this in Jenkins or another continuous integration (CI) system, this behavior would mean that the image that comes out of the build system hasn't actually been tested. You've run tests on whatever local source tree Jenkins has checked out, glued over the top of the built image. If the host system and the image have any sort of incompatibility around library versions or a vendored library tree, you can easily wind up in a situation where you're shipping a completely broken image, that happens to pass this artificial test environment.

In this context I'd suggest a sequence like this:

  1. Check out your source tree.

  2. Run any build steps and unit-test steps. Jenkins has a path to run these inside a Docker container, which can be more convenient than maintaining a Jenkins image with the language runtime you need, but this isn't "building and running the image". You can do this totally without Docker too.

  3. Actually build the Docker image. I'd do this with the Jenkins Docker workflow plugin, or a manual sh "docker build" step.

  4. Run integration tests against the built image. This could use a docker-compose.yml file like you show above, but without the build: or volumes: blocks. The test itself wouldn't be built into the image, but it would make external calls into the image's published API.

  5. Push the built image to a registry. Trigger an automated deployment sequence, if that's appropriate.

Upvotes: 1

Related Questions