LoginSignup
3
0

More than 5 years have passed since last update.

CircleCI 2.0で「composer: command not found」エラーが出た場合の対処法

Posted at

CircleCI 2.0でPHPのプロジェクトを追加して、デフォルトのconfig.ymlでbuildしたところ、 composerの実行でエラーになったときのメモです。

現象

ADD PROJECTSメニューからCIしたいリポジトリを選択して、「Set Up Project」画面に移動します。
言語はPHPだったのでPHPを選択しページ下部に表示されるCircleCIの実行時の設定ファイルのサンプルを使ってビルドしたところ、composerが動作しませんでした…(2018/04/23時点)

CircleCI2.0 composer エラー

デフォルトのサンプルは以下のとおりです。

sample.yml
# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/php:7.1.5-browsers

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # - image: circleci/mysql:9.4

    working_directory: ~/repo

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "composer.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run: composer install -n --prefer-dist

      - save_cache:
          paths:
            - ./vendor
          key: v1-dependencies-{{ checksum "composer.json" }}

      # run tests!
      - run: phpunit

これをリポジトリ直下の .circleci/config.yml として配置しpushしビルドを走らせます。
すると composer install -n --prefer-dist で以下のエラーになりました。

#!/bin/bash -eo pipefail
composer install -n --prefer-dist
/bin/bash: composer: command not found
Exited with code 127

解決方法

調べて見ると CircleCIのフォーラム に同じような投稿がありました。
どうやらsample.ymlには CircleCIが推奨するDocker Imageではないイメージが記載 されているのが原因のようです。
上記ページの回答どおりにimageを circleci/php:latest を指定することで対処しました。これでcomposerのエラーは出なくなりました。

config.yml
      - image: circleci/php:latest

latestに変更してpushしましたが今度はPHPUnitのエラーが出ます。

#!/bin/bash -eo pipefail
phpunit
/bin/bash: phpunit: command not found
Exited with code 127

CircleCI2.0 PHPUnitエラー

phpunitが読み込めていないようなので

config.yml
      - run: vendor/bin/phpunit

と変更することでビルドが全て通るようになりました!

以下が変更後のconfig.ymlです。

config.yml
# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/php:latest            # ★変更した

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # - image: circleci/mysql:9.4

    working_directory: ~/repo

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "composer.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run: composer install -n --prefer-dist

      - save_cache:
          paths:
            - ./vendor
          key: v1-dependencies-{{ checksum "composer.json" }}

      # run tests!
      - run: vendor/bin/phpunit            # ★変更した

ビルドがとおって良かった良かった。
CircleCIでPHPプロジェクトを扱う場合config.ymlの設定にご注意ください:smiley:

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0