Setup Github action : shivammathur/setup-php#v2 with composer image - php

Github Action.
I have a PHP Project (using Yii2 Framework) that I need to build into a docker image, which is those project also need MongoDB extension.
My plan is in Github Action - CI phase is like this:
Commit to main branch
Enable php extension
Run composer install --no-dev
Build docker image using dockerfile, which is in those dockerfile, I copy vendor`s folder into an image container,
..so on
So, for those, I start with a configuration: 01 - Init Composer.yml like this:
name: Init composer
on:
push:
branches: ["main"]
jobs:
run-composer:
name: composer initialization
runs-on: ubuntu-latest
container:
image: composer:latest
volumes:
- ${{ github.workspace }}:/app
steps:
- name: Check out the repo
uses: actions/checkout#v3
- name: PHP setup with Extension
id: setup-php
uses: shivammathur/setup-php#v2
with:
php-version: '8.1'
extensions: mongodb-mongodb/mongo-php-driver#v1.9
- name: Cache Composer packages
id: composer-cache
uses: actions/cache#v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Running composer install
run: composer install --no-dev
I got error like this:
Run shivammathur/setup-php#v2
/usr/bin/docker exec fccfaf138a6147da33e0f9ca9add2947ae76ff4b439c3fe9c35f53042e6419fa sh -c "cat /etc/*release | grep ^ID"
/bin/bash /__w/_actions/shivammathur/setup-php/v2/src/scripts/run.sh
==> Setup PHP
/__w/_actions/shivammathur/setup-php/v2/src/scripts/linux.sh: line 216: sudo: command not found
✗ PHP Could not setup PHP 8.1
Error: The process '/bin/bash' failed with exit code 1
Any advise is so appreciated.

Running inside the composer image, it doesn't have the sudo command to perform actions to compile the extension.
In action shivammathur/setup-php it is possible enable composer, see tools support
Try running it directly in ubuntu, setting the php and composer extension first.

I had a similar problem here when running it in self hosted server.
Looks like shivammathur already fixed in the "develop" branch (see here)
It will be fixed in the next version, you can follow the issue about it here

Related

Travis composer install issue

I want to use travis with my simple php projects. My project structure is next:
My .travis.yml
language: php
sudo: required
before_install:
- cd http
before_script:
- cd http
install:
- composer self-update
- composer-install --no-interaction
script:
- phpunit --configuration phpunit.xml
and I want to run trevis into http folder, my composer.json and phpunit are there. However, as a result of my build, I received:
How can I solve this issue and run travis correctly? Thanks

How to use the image between steps in bitbucket-pipelines

I would be grateful if someone could help me with setting up bitbucket-pipeline for my php env. What I'm trying to succeed is:
Build image in the first step
In the second step run some QA stuff like unit test, code sniffer etc.
Deploy to preprod envirement
Currently I'm stuck with re-using image from the first step. This is how my bitbucket-pipelines.yml looks like:
pipelines:
branches:
develop:
- step:
name: Build docker image
caches:
- composer
script:
- apt-get update && apt-get install -y unzip
- docker-php-ext-install mysqli pdo_mysql json sockets
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
- composer install
- parallel:
- step:
caches:
- composer
name: Unit tests
script:
- vendor/bin/phpunit
- step:
caches:
- composer
name: Code sniffer
script:
- composer phpcs:all
- step:
name: Deploy to preprod
script:
- echo "Deployment"
What I get here is:
bash: vendor/bin/phpunit: No such file or directory - from "Unit tests" step
and bash: composer: command not found - from "Code sniffer" step
I already tried to set docker/composer to cache, save image in the first step and import it in the second, but still not working.
One way to share stuff between different steps is to make use of Artifacts.
Basically, bitbucket spins up each step in a separate docker container. So a way to re-use material between steps is create an artifact.
The link should give you enough information.
For example:
- step: &build
caches:
- node
name: Build
script:
- npm install
- npm run build
artifacts: # defining the artifacts to be passed to each future step.
- dist/**
- step: &read-from-artifact
name: Read from the artifact saved in the previous step
script:
- echo "Read artifact (dist folder) saved in previous step"
- ls -la dist/

How do I get php ini-value in github actions to point towards a file in my project directory

Here's my .yml file for github actions. How do I correctly add the ini-values parameter to point to a directory in my project? My project structure is InstaGetSymfony/src = symfony MVC dir.
I want my curl.cainfo ini value to point towards InstaGetSymfony/ca/cacert.pem
name: InstaGet
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
run:
runs-on: ubuntu-latest
steps:
- name: Checkout codebase
uses: actions/checkout#v2
- name: Setup PHP with pecl extension
uses: shivammathur/setup-php#v2
with:
php-version: '7.4'
tools: pecl
extensions: curl
ini-values: curl.cainfo=/home/runner/work/InstaGetSymfony/InstaGetSymfony/ca/cacert.pem
- name: Validate composer.json and composer.lock
run: composer validate
- name: Install dependencies
run: composer install
- name: Run phing
run: vendor/bin/phing
You can get the directory in a previous step and use that.
- name: Get current directory
id: pwd
run: echo "::set-output name=pwd::$(pwd)"
- name: Setup PHP
uses: shivammathur/setup-php#v2
with:
php-version: '7.4'
ini-values: curl.cainfo=${{ steps.pwd.outputs.pwd }}/ca/cacert.pem
Also curl extension is available by default and pecl is not needed for this workflow, so you can remove those.

Define PHP version for Ansible composer module

I run the following task in some deploy script for a PHP application:
- name: Update composer dependencies
composer:
command: update
working_dir: "{{ release_path }}/src"
ignore_platform_reqs: true
On the target system the CLI PHP version is 7.3, but there is also a /usr/bin/php7.2 binary, that I want to use.
How can I use the composer module and run a specific PHP version with it... or do I need to run this via the shell module like this?
- name: Update composer dependencies
shell: "/usr/bin/php7.2 composer update"
args:
chdir: "{{ release_path }}/src"
According to Ansible doc for Composer there is an option for specifying the php executable one wants to use.
So you can use something like below for your case
- name: Update composer dependencies
composer:
command: update
working_dir: "{{ release_path }}/src"
ignore_platform_reqs: true
executable: /usr/bin/php7.2

Travis configuration for composer packages

While working on a laravel 5.1+ package I have this need to run automated tests through travis-ci.org. The difference with regular automated tests is the requirement to include this package into a framework and set specific configuration options to run the tests.
So the requirement would be:
install laravel
add my package as dependency
set some travis specific configurations like the travis database access
run migrations of laravel
run migrations specific for package or run an artisan command
run package specific unit tests
I searched everywhere; asked on laravel forums, asked in a travis community chat and saw this topic being closed as too localized (although an answer would have certainly been helpful now). I'm hoping my question is fit to remain open.
At this time I have the following configuration:
language: php
php:
- 5.5
- 5.6
- hhvm
addons:
hosts:
- system.hyn.me
- tenant.hyn.me
before_install:
- sudo composer self-update
install:
- composer create-project laravel/laravel
- cd ./laravel
- composer require hyn-me/multi-tenant ~0.1.0
- composer update
before_script:
- cp .env.travis .env
- export APP_ENV="testing"
- php artisan migrate -q -n --path ./vendor/hyn-me/multi-tenant/src/migrations
- cd ./vendor/hyn-me/multi-tenant
script: phpunit
Yet my knowledge of travis (thus far) is limited and before I send in an unneeded number of commits to fix my problems I'd rather have your opinion on what would be a good method to test integration into a framework.
Ps. this concerns the package hyn/multi-tenant.
Advise on how to keep this question as generic as possible would be helpful. I hope without explicitly mentioning best practice and requesting integration into framework examples helps in defining the scope of the answers.
So after weeks of pushing commits into travis, I finally made this work.
The .travis.yml:
language: php
sudo: true
php:
- 5.5
- 5.6
- 7.0
- hhvm
addons:
hosts:
- system.hyn.me
- tenant.hyn.me
install:
# fix ipv6 issue that prevented composer requests and resulted in failing builds
- sudo sh -c "echo 'precedence ::ffff:0:0/96 100' >> /etc/gai.conf"
# updates composer on travis
- travis_retry composer self-update
# clear composer cache, might speed up finding new tags
- travis_retry composer clear-cache
# set the global github token, so connections won't be cancelled
- composer config -g github-oauth.github.com $GITHUB_TOKEN
# create a new database for the hyn connection
- mysql -e 'create database hyn;' -uroot
- mysql -e "grant all privileges on *.* to 'travis'#'localhost' with grant option;" -uroot
# create a new laravel project in the subfolder laravel (default composer behaviour)
- composer create-project laravel/laravel
# set global variables
- export DB_USERNAME=travis DB_DATABASE=hyn DB_PASSWORD= QUEUE_DRIVER=sync
script:
# run the script calling unit tests and so on
- ./scripts/travis.sh
after_script:
- if [[ $TRAVIS_PHP_VERSION != '7.0' ]]; then php vendor/bin/ocular code-coverage:upload --format=php-clover ${TRAVIS_BUILD_DIR}/coverage.clover; fi
And the scripts/travis.sh
#!/bin/bash
# e causes to exit when one commands returns non-zero
# v prints every line before executing
set -ev
cd ${TRAVIS_BUILD_DIR}/laravel
BRANCH_REGEX="^(([[:digit:]]+\.)+[[:digit:]]+)$"
if [[ ${TRAVIS_BRANCH} =~ $BRANCH_REGEX ]]; then
echo "composer require ${TRAVIS_REPO_SLUG}:${TRAVIS_BRANCH}"
composer require ${TRAVIS_REPO_SLUG}:${TRAVIS_BRANCH}
else
echo "composer require ${TRAVIS_REPO_SLUG}:dev-${TRAVIS_BRANCH}"
# development package of framework could be required for the package
composer require hyn-me/framework "dev-master as 0.1.99"
composer require "${TRAVIS_REPO_SLUG}:dev-${TRAVIS_BRANCH}#${TRAVIS_COMMIT}"
fi
# moves the unit test to the root laravel directory
cp ./vendor/${TRAVIS_REPO_SLUG}/phpunit.travis.xml ./phpunit.xml
phpunit
# phpunit --coverage-text --coverage-clover=${TRAVIS_BUILD_DIR}/coverage.clover
This code might change due to new Laravel versions or changes in travis. If this is the case, you will find the latest release here.

Categories