Programming

Composer의 개발 / 생산 스위치를 사용할 때 올바르게 배포하는 방법은 무엇입니까?

procodes 2020. 6. 5. 22:11
반응형

Composer의 개발 / 생산 스위치를 사용할 때 올바르게 배포하는 방법은 무엇입니까?


Composer에는 개발 중에 만 여러 종속성을로드 할 수있는 옵션이 있으므로 프로덕션 환경 (라이브 서버)에 도구가 설치되지 않습니다. 이것은 이론적으로 테스트, 가짜 데이터 도구, 디버거 등과 같이 개발에만 의미가있는 스크립트에 매우 유용합니다.

가는 방법 require-dev은 개발자가 필요로하는 도구 로 추가 블록 을 추가하는 것입니다 .

"require-dev": {
    "codeception/codeception": "1.6.0.3"
}

그런 다음 이론적으로 이러한 종속성을로드합니다.

composer install --dev

문제 및 질문 :

작곡가의 동작을 변경 install하고 update극적으로 2013 년, require-dev-dependencies가 기본으로 설치되어있다 (!)에있어서 A composer.json 만들어 주시기 require-dev블록과 수행 composer install재현.

가장 널리 배포되는 방법은 작곡가를 밀어내는 것입니다. 잠금 (현재 작곡가 설정을 보유)하고 composer install프로덕션 서버에서 작업 을 수행 하면 개발 항목도 설치됩니다.

-dev 의존성 설치 하지 않고 이것을 배포하는 올바른 방법은 무엇입니까 ?

참고 : 이상한 Composer 배포를 명확히하기 위해 여기에 표준 Q / A를 만들려고합니다. 이 질문을 자유롭게 편집하십시오.


오늘날 Composer가 --dev기본적으로 (설치 업데이트시) 플래그를 사용하는 이유는 IMHO 입니다. Composer는 주로 시나리오에서 원하는 동작으로 실행됩니다.

기본 Composer 워크 플로우는 다음과 같습니다.

  • composer.phar install --dev, json 및 lock 파일이 VCS에 커밋 된 새 프로젝트가 시작 됩니다.
  • 다른 개발자는 프로젝트 작업을 시작 : VCS의 체크 아웃하고 composer.phar install --dev.
  • 개발자는 dependancies : composer.phar require <package>를 추가 --dev합니다. require-dev섹션 에서 패키지를 원하면 추가 하십시오 (및 커밋).
  • (체크 아웃 등) : 다른 사람을 따라 간다 composer.phar install --dev.
  • 개발자는 최신 버전의 종속성 composer.phar update --dev <package>(및 커밋)을 원합니다 .
  • (체크 아웃 등) : 다른 사람을 따라 간다 composer.phar install --dev.
  • 프로젝트가 배포되었습니다 : composer.phar install --no-dev

보시다시피 , 특히 프로젝트에서 작업하는 개발자 수가 증가 할 때 플래그가 플래그 --dev보다 훨씬 많이 사용됩니다 --no-dev.

프로덕션 배포

"dev"의존성을 설치하지 않고 이것을 배포하는 올바른 방법은 무엇입니까?

composer.jsoncomposer.lock파일은 VCS에 커밋되어야합니다. composer.lock사용해야하는 패키지 버전에 대한 중요한 정보가 포함되어 있으므로 생략하지 마십시오 .

프로덕션 배포를 수행 할 때 --no-dev플래그를 Composer에 전달할 수 있습니다 .

composer.phar install --no-dev

composer.lock파일에는 dev-packages에 대한 정보가 포함될 수 있습니다. 이것은 중요하지 않습니다. --no-dev플래그는 해당 개발 패키지가 설치되지 않았는지 확인합니다.

"프로덕션 배포"라고 말하면 프로덕션에서 사용되는 것을 목표로하는 배포를 의미합니다. composer.phar install프로덕션 서버에서 수행해야하는지 또는 검토 할 수있는 준비 서버에서 수행 해야하는지에 대해서는 논쟁하지 않습니다 . 이것이이 답변의 범위가 아닙니다. 나는 단지 composer.phar install"dev"의존성을 설치하지 않고 방법을 지적하고있다 .

주제를 벗어

--optimize-autoloader플래그는 생산에 바람직 할 수있다 (이것은 응용 프로그램에서 자동 로딩 속도가 빨라집니다 클래스 맵을 생성) :

composer.phar install --no-dev --optimize-autoloader

또는 자동 배포가 완료된 경우 :

composer.phar install --no-ansi --no-dev --no-interaction --no-plugins --no-progress --no-scripts --no-suggest --optimize-autoloader

당신의 코드베이스가 지원하는 경우에, 당신은 밖으로 교환 수 --optimize-autoloader에 대해 --classmap-authoritative. 더 많은 정보는 여기에


실제로 프로덕션 서버에 종속성을 설치하는 것이 좋습니다.

권장 사항은 배포 시스템에서 코드를 체크 아웃하고 필요에 따라 종속성을 설치 한 다음 (코드가 프로덕션 환경으로 갈 경우 개발자 종속성을 설치하지 않는 것을 포함) 모든 파일을 대상 시스템으로 옮기는 것이 좋습니다.

왜?

  • 공유 호스팅에서 명령 행에 도달하지 못할 수 있습니다.
  • 하더라도 PHP는 명령, 메모리 또는 네트워크 액세스 측면에서 제한 될 수 있습니다.
  • 저장소 CLI 도구 (Git, Svn)가 설치되지 않았을 수 있습니다. 잠금 파일이 해당 커밋을 ZIP으로 다운로드하는 대신 특정 커밋을 체크 아웃하는 종속성을 기록한 경우 실패합니다 (--prefer-source를 사용했거나 Composer가 그 버전을 얻는 다른 방법은 없습니다)
  • 프로덕션 머신이 소규모 테스트 서버와 비슷하다면 (Amazon EC2 마이크로 인스턴스를 생각하십시오) 실행할 메모리가 충분하지 않을 수 있습니다. composer install
  • 작곡가가 문제를 일으키지 않으려 고 시도하는 동안 작곡가 설치 단계에서 일부 임의의 종속성을로드 할 수 없기 때문에 부분적으로 깨진 프로덕션 웹 사이트로 끝나는 것에 대해 어떻게 생각하십니까

간단히 말해 : 제어 할 수있는 환경에서 Composer를 사용하십시오. Composer를 작동하는 데 필요한 모든 것이 이미 있으므로 개발 시스템이 적합합니다.

What's the correct way to deploy this without installing the -dev dependencies?

The command to use is

composer install --no-dev

This will work in any environment, be it the production server itself, or a deployment machine, or the development machine that is supposed to do a last check to find whether any dev requirement is incorrectly used for the real software.

The command will not install, or actively uninstall, the dev requirements declared in the composer.lock file.

If you don't mind deploying development software components on a production server, running composer install would do the same job, but simply increase the amount of bytes moved around, and also create a bigger autoloader declaration.


Now require-dev is enabled by default, for local development you can do composer install and composer update without the --dev option.

When you want to deploy to production, you'll need to make sure composer.lock doesn't have any packages that came from require-dev.

You can do this with

composer update --no-dev

Once you've tested locally with --no-dev you can deploy everything to production and install based on the composer.lock. You need the --no-dev option again here, otherwise composer will say "The lock file does not contain require-dev information".

composer install --no-dev

Note: Be careful with anything that has the potential to introduce differences between dev and production! I generally try to avoid require-dev wherever possible, as including dev tools isn't a big overhead.


On production servers I rename vendor to vendor-<datetime>, and during deployment will have two vendor dirs.

A HTTP cookie causes my system to choose the new vendor autoload.php, and after testing I do a fully atomic/instant switch between them to disable the old vendor dir for all future requests, then I delete the previous dir a few days later.

This avoids any problem caused by filesystem caches I'm using in apache/php, and also allows any active PHP code to continue using the previous vendor dir.


Despite other answers recommending against it, I personally run composer install on the server, since this is faster than rsync from my staging area (a VM on my laptop).

I use --no-dev --no-scripts --optimize-autoloader. You should read the docs for each one to check if this is appropriate on your environment.


I think is better automate the process:

Add the composer.lock file in your git repository, make sure you use composer.phar install --no-dev when you release, but in you dev machine you could use any composer command without concerns, this will no go to production, the production will base its dependencies in the lock file.

On the server you checkout this specific version or label, and run all the tests before replace the app, if the tests pass you continue the deployment.

If the test depend on dev dependencies, as composer do not have a test scope dependency, a not much elegant solution could be run the test with the dev dependencies (composer.phar install), remove the vendor library, run composer.phar install --no-dev again, this will use cached dependencies so is faster. But that is a hack if you know the concept of scopes in other build tools

Automate this and forget the rest, go drink a beer :-)

PS.: As in the @Sven comment bellow, is not a good idea not checkout the composer.lock file, because this will make composer install work as composer update.

You could do that automation with http://deployer.org/ it is a simple tool.

참고URL : https://stackoverflow.com/questions/21721495/how-to-deploy-correctly-when-using-composers-develop-production-switch

반응형