GitLab-CI Multi Runner php composer cache - php

I'm using gitlab-ci-multi-runner with docker containers. Everything is going fine, but docker containers don't keep the composer cache so in every run composer downloads dependencies again and again, which takes a lot of time. Is there any way to configure gitlab-ci-runner docker container to keep the composer cache or mount a volume on each run where the composer cache is kept?

You can change the composer cache path by exporting the COMPOSER_CACHE_DIR environment variable in your runner configuration file, and then add a volume in the [runners.docker] section to match it.
If you run gitlab-runner as root or with sudo, then your configuration file is located at /etc/gitlab-runner/config.toml. Otherwise it's located at $HOME/.gitlab-runner/config.toml.
# config.toml
[[runners]]
name = "Generic Docker Runner"
...
environment = ["COMPOSER_CACHE_DIR=/cache"]
executor = "docker"
[runners.docker]
...
volumes = ["/var/cache:/cache:rw"]
cache_dir = "/cache"

You could modify the composer cache path and write the stuff to a docker volume.
That storage is persistent and can be shared across containers.
Referencing:
https://gitlab.com/gitlab-org/gitlab-runner/blob/master/docs/configuration/advanced-configuration.md#volumes-in-the-runnersdocker-section
https://docs.docker.com/engine/admin/volumes/volumes/

Related

How to setup and run laravel, from git?

Either I miss something, or the whole chain lacks something.
Here's my assumption:
The whole point of containerization in development, is to reduce the cost of environment setup, and create a prepared image with all the required pieces.
So, when I read that Laravel Sail is installing laravel via containerization, I get excited. Thus I install it via their instructions, and everything works.
Then the problem begins. Because:
After a successful installation, I create a git repo, with GitHub's default laravel .gitignore
Then I push the newly installed laravel app into my git repo.
Then I ask a developer to start developing it. Please note that:
He does not have PHP installed
He does not have Composer installed
He clonse the repo, and as per installation guide, runs ./vendor/bin/sail up
But ./vender folder is correctly excluded in .gitignore
Thus his command results in:
bash: ./vendor/bin/sail: No such file or directory
He Googles it of course, and finds out that people suggest to run composer update
He goes to install composer, then before that PHP, then all extensoins of PHP, then ...
Do I miss something here? The whole point of containerization was to not install the required environment locally.
What is the proper way of running a laravel app, that is not installed from https://laravel.build, but is cloned from a git repo, WITHOUT having PHP or Composer installed locally?
Update
I found Bitnami laravel docker and it's exactly what containers should be.
You are right and the other developer doesn't need to have php nor composer installed.
All he/she needs is Docker installed on the local machine.
If you scaffolded the project with what is mentioned in the official Laravel docs under the Getting started section, then you will have a docker-compose.yml file in your project root directory.
For Windows
For Linux
For Mac OS
All the developer has to do after git cloning the repository is to run
docker-compose up --build -d
That's it.
For those struggling with this issue... I've found a command that work perfectly fine.
First of all, you don't need to locally have any PHP or Composer installed, maybe there is a misunderstanding about it, all you need is Docker.
Docker will install everything you need in something I understand is like a sandbox, not locally, for each project.
And for those downloaded projects, from GIT as example, that does not have vendor folder, and obviously cannot execute sail up you can simple execute:
docker run --rm --interactive --tty -v $(pwd):/app composer install
That command will download a composer image for docker, if you do not have one yet. Then, will run a composer install and you are free to execute a ./vendor/bin/sail up if you hadn't configured an alias or just sail up if you already configure an alias.
That's all.
The official documentation lists the following command.
docker run --rm \
-u "$(id -u):$(id -g)" \
-v $(pwd):/var/www/html \
-w /var/www/html \
laravelsail/php81-composer:latest \
composer install --ignore-platform-reqs
If you were to clone a Laravel project and run this command in the project root, it would create a very small container with php and composer installed and run composer in the project root to install all php dependencies. In effect, this installs the Laravel core code into the cloned project. Once the project in set up this way, the user should create a local .env file to match their development evironment.
cp .env.example .env # creates a .env file to be populated for the local environment
With the envronment set up, they can now create the application containers in docker and run the application. Laravel provides the Sail helper for this.
./vendor/bin/sail up -d # runs the docker containers in detached mode
Now it's a matter of setting up the laravel app and running the Laravel app. (I'm assuming the app uses one of the Laravel start kits that rely on Node.js. If you are using a Blade only application, you can skip the "npm" commands.)
sail artisan key:generate # (Best Practice) Generate a new application key on each machine
sail artisan migrate # Scaffold the database structure
sail artisan db:seed # (Optional) Seed the database with data
sail npm install # (Optional) Install front-end dependencies (Inertia, Vue, React, others...)
sail npm run dev # (Optional) Run the front-end framework in development mode
With this, the new developer should be running an exact copy of both the project and the development environment as the original developer.
Your project README may include additional steps to set up some other dependencies, but this is the basic workflow for contributing to a Laravel project.
The only prerequisites for this workflow is to have Docker installed with an Internet connection. This is most easily accomplished on Windows, Mac, and Linux by installing Docker Desktop.
Alternate for Older Projects
If you are working on an older project that doesn't use Laravel Sail, but does have a docker-compose.yml file, you should be able to build and run the necessary containers with the following command.
docker-compose up --build -d
Once you have the containers running, you would need to install the project dependencies directly into the container.
docker ps # find the container ID of your project's container
docker exec -it CONTAINER_ID php artisan key:generate
docker exec -it CONTAINER_ID php artisan migrate
docker exec -it CONTAINER_ID php artisan db:seed
docker exec -it CONTAINER_ID npm install
docker exec -it CONTAINER_ID npm run dev
Of course, Docker Desktop simplifies this process. With a button click you can have a terminal shell open directly in your container eliminating the need for the docker exec command.

Laravel application $_ENV array is empty after running composer update

After updating my laravel application with composer update my .env file is not loaded into scripts anymore. When I var_dump($_ENV) I get an empty array.
I use docker - and precisely laradock - to host my application. I only boot docker-compose up nginx mariadb so I only have the following containers running: nginx, php-fpm, workspace, mariadb and docker-in-docker.
When I ssh into the container docker-composer exec workspace /bin/bash and cat .env in the /var/www directory, I see that the .env file with all variables is available inside the container. But still, the $_ENV array is empty.
Solutions provided this question did not work form me. As laradock is not part of my composer definition nothing changed about the server setup and there is no .htaccess file that might mess things up as nginx is doing the job.
Any ideas where to start debugging?

Call phpunit from console in my symfony project

this is my current status:
I have a running symfony environment, based on a docker image. Everything works fine and from PHPStorm i can execute phpunit tests. But i want to execute them manually from console.
After i started all services by using docker-compose up --build, i login into the phpfpm service by: docker-compose exec -it phpfpm bash
then i move into my symfony project folder that contain all folders and files like "app/, bin/, vendor/, ... composer.json... etc"
I could go by calling vendor/phpunit/phpunit/phpunit but i want to get a shorter way. Is there any chance, maybe calling bin/phpunit or something like this?
Thanks in advance,
Max
PHPUnit has that script in the own composer.json, so bin (or vendor/bin) directory should contains a relevant symlink after running composer install. Also check your composer.json for bin-dir settings.
At least you always can create a symlink:
$ ln -s vendor/phpunit/phpunit/phpunit phpunit
According to the symfony docs the preferable way of using phpunit is by .phar file. In this way you can download phpunit as a .phar file and it will works both outside and inside docker container.
wget https://phar.phpunit.de/phpunit-6.0.phar
php phpunit-6.0.phar --version
Type now php phpunit-6.0.phar to run tests.

Laravel - Installing packages that were installed with composer on live server

I have a laravel project on namecheap shared server, I have set up everything and it is working there. But after that, I have added locally on my machine to the project imagecache intervention package with composer. I don't know how to install it on the server though, I have tried with running composer commands through ssh:
php composer require intervention/imagecache
But I got an error:
Could not open input file: composer
Since, their support told me that they actually have composer uploaded in the folder public_html and not where my project is and that is public_html/myProject if that is the problem. Since I have no idea, how the composer is installed and setup there, I wonder how can I install this package. I have also tried by just uploading the whole directory of the package from my local vendor folder in the project to the production server, but that didn't work either, images were still not visible when I was using imagecache route. How can I fix that?
Run this: php -c php.ini composer.phar install
First You have to create php.ini file with this info.
max_execution_time = 300
max_input_time = 300
memory_limit = 512M
suhosin.executor.include.whitelist = phar
detect_unicode = Off
If you have ssh then you can create a folder where ever you wan't.
It is better if you put it in the same level that your public folder and then install composer inside this folder:
with curl
curl -sS https://getcomposer.org/installer | php
if you don't have curl or the server has restrictions you can use:
php -r "readfile('https://getcomposer.org/installer');" | php
or you can "Manual Download" the composer.phar and upload it trough ftp
https://getcomposer.org/download/
Then you need to give execution rights to the composer.phar file and you
chmod a+x composer.phar
can also create an alias to run composer from your public folder
alias composer="path/to/composer/composer.phar"
This alias will work until you close your console. In some shared servers you will see a file call: .bashrc where you can put the alias

php composer inside container lost vendor directory

I have the following Dockerfile
FROM bitgandtter/sf:php7
# basic env fix
ENV TERM xterm
# install packages
ADD . /var/www
# update dependencies
RUN cd Helpers && SYMFONY_ENV=prod composer update -o --no-dev
ENV SYMFONY_ENV prod
After build the image the Helpers directory does not contain the vendor directory.
I really dont know why is that since the previous compsoer update just execute successfully and the image was created just fine.
Any help please
NOTE: the image bitgandtter/sf:php7 use a VOLUME declaration on /var/www
In fact i discover that the VOLUME declaration on the base image was the main issue.
As explained in the official doc after define a VOLUME on a dockerfile if any file changes happens inside that volume will be lost.
So the solution is to not declare VOLUMES on base images.

Categories