php-actions / phpunit

Run PHPUnit tests in Github Actions.
108 stars 24 forks source link

Run arbitrary commands within the container #57

Open g105b opened 1 year ago

g105b commented 1 year ago

Many CI workflows configure the workspace environment before executing. This Github Action works in a way that completely isolates the test runner from the environment, by running your tests in its own Docker container, sharing only the project source files.

It would be useful to run any arbitrary shell command within the container. This should probably be transferred to other php-actions too.

antman3351 commented 1 year ago

Would it be possible to add a option like "BEFORE_RUN_CMD" and run it before the tests ? ( I was trying to add mysql-client to the container )

I tried to build a dockerfile and put the commands in but failed...

.... 
echo "FROM ${docker_tag}" > Dockerfile

if [ -n "$BEFORE_RUN_CMD" ]
then
  echo "RUN ${BEFORE_RUN_CMD}" >> Dockerfile
fi

echo "CMD ${command_string[@]}" >> Dockerfile

cat Dockerfile.ymal

docker build -t phpunit_container .

docker run --rm \
  --name phpunit \
    --volume "${phar_path}":/usr/local/bin/phpunit \
    --volume "${GITHUB_WORKSPACE}":/app \
    --workdir /app \
    --network host \
    --env-file <( env| cut -f1 -d= ) \
    phpunit_container
g105b commented 1 year ago

That idea is certainly on the right tracks. However I think it would be better to run something like MySQL in its own container so it can be managed in its own step - but then it would require the change here to be able to configure which ports are exposed to the PHPUnit container.

I have a feeling that the BEFORE_RUN_CMD idea might have issues, due to how the Dockerfile is built into a Package and stored on Github's container registry.

I will give it some thought!

antman3351 commented 1 year ago

MySql server is in it's own container and that worked fine I just passed the ${{ job.services.mariadb.ports['3306'] }} to PHP via an environment variable and had no problems connecting from the PHP Unit container. I need the mysql-client ( not server ) binary to be able to quickly restore the test DB multiple times during some tests without having to do it from PHP. e.g. mysql -h 127.0.0.1 -u root --password=root db_unittest < dump_unittest.sql

I'm clueless to how Github's container registry works, but another idea I pondered was delaying the execution of phpunit to allow commands to be executed before using docker exec -it <container name> sh -c "<command>"

g105b commented 1 year ago

Gotcha. I see the issue you're explaining. I'll look into how MySQL Client can be included in the PHP Alpine container, as it might just make sense to include it by default, seeing how close friends PHP and MySQL are. What do you think to that idea?

antman3351 commented 1 year ago

Sounds good, thanks!