Bitbucket Pipeline composer not found despite I install is as composer doc's say

27 Views Asked by At

In my project I try to package my app:

image: php:8.1

pipelines:
  default:
    - step:
        caches:
          - composer
        script:
          - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
          - php composer-setup.php --install-dir=/usr/local/bin --filename=composer
          - php -r "unlink('composer-setup.php');"
          - chmod +x /usr/local/bin/composer
    - step:
        caches:
          - composer
        script:
          - export PATH=/usr/local/bin:$PATH
          - composer install
          - ./vendor/bin/phpunit
    
    - step:
        caches:
          - composer
        script:
          - export PATH=/usr/local/bin:$PATH
          - composer install --no-dev
          - apt-get update && apt-get install -y zip
          - export FILE=release-$(date +"%Y%m%d%H%M%S").zip 
          - zip ${FILE} ./lib/*.php ./vendor/* composer.json composer.lock index.php config.php ./utils/*.php
          - curl -X POST --user "${BB_AUTH_STRING}" "https://api.bitbucket.org/2.0/repositories/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}/downloads" --form files=@"${FILE}"
        artifacts:
          - release-$(date +"%Y%m%d%H%M%S").zip


But at step:

   - step:
        caches:
          - composer
        script:
          - export PATH=/usr/local/bin:$PATH
          - composer install
          - ./vendor/bin/phpunit

I get the error:

bash: composer: command not found

Do you know why? Is seems like for some reason between each steps the installed composer is not shared.

1

There are 1 best solutions below

3
Alex Howansky On

caches: composer doesn't mean composer itself is shared, it means composer's cache is shared. Each step is run in its own standalone container, so if you're going to use composer in each, then you have to install composer in each. In your example, your first step does nothing -- it just starts a container, installs composer, and then throws the container away. You could have two steps, one to test and one to build the zip, each installing and running composer:

pipelines:
  default:
    - step:
        caches:
          - composer
        script:
          - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
          - php composer-setup.php --install-dir=/usr/local/bin --filename=composer
          - php -r "unlink('composer-setup.php');"
          - chmod +x /usr/local/bin/composer
          - export PATH=/usr/local/bin:$PATH
          - composer install
          - ./vendor/bin/phpunit
    - step:
        caches:
          - composer
        script:
          - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
          - php composer-setup.php --install-dir=/usr/local/bin --filename=composer
          - php -r "unlink('composer-setup.php');"
          - chmod +x /usr/local/bin/composer
          - export PATH=/usr/local/bin:$PATH
          - composer install --no-dev
          - apt-get update
          - apt-get install -y zip
          - export FILE=release-$(date +"%Y%m%d%H%M%S").zip 
          - zip ${FILE} ...
          - curl ...
        artifacts:
          - release-$(date +"%Y%m%d%H%M%S").zip

Alternatively, you could build your own custom image based on php:8.1, with composer and zip already installed -- and then reference that in your pipe config.

image: my_custom_image_with_composer_and_zip_already_in_it:8.1

pipelines:
  default:
    - step:
        caches:
          - composer
        script:
          - composer install
          - ./vendor/bin/phpunit
    - step:
        caches:
          - composer
        script:
          - composer install --no-dev
          - export FILE=release-$(date +"%Y%m%d%H%M%S").zip 
          - zip ${FILE} ...
          - curl ...
        artifacts:
          - release-$(date +"%Y%m%d%H%M%S").zip