LoginSignup
4
0

CloudFunctionsをデプロイするとGCP上でビルドが走るためにビルドエラーになってしまってデプロイできない問題の解決策

Last updated at Posted at 2023-05-30

こんにちは。virapture株式会社でCEOしながらラグナロク株式会社でもCKOとして働いている@mogmetです。

本日はトラブルシュート記事になります。

結論からいうと、package.jsonのscriptsに空のgcp-buildを追加する! ことで問題が解決できます。

エラー内容

i  functions: updating Node.js 16 function xxx(us-central1)...
i  functions: updating Node.js 16 function xxx(us-central1)...
i  functions: updating Node.js 16 function xxx(us-central1)...
i  functions: updating Node.js 16 function xxx(us-central1)...
Build failed with status: FAILURE and message: > @app/functions@1.0.0 build
> tsc

error TS2688: Cannot find type definition file for '@types/jest'.
  The file is in the program because:
    Entry point of type library '@types/jest' specified in compilerOptions. For more details see the logs at https://console.cloud.google.com/cloud-build/builds;region=us-central1/d07d35e3-9dc0-48e0-bbdc-1184bf0ed04f?project=103424089072.
Build failed: > @app/functions@1.0.0 build
> tsc

error TS2688: Cannot find type definition file for '@types/jest'.
  The file is in the program because:
    Entry point of type library '@types/jest' specified in compilerOptions; Error ID: 1a2262f3
Build failed: > @app/functions@1.0.0 build
> tsc

...

Error: There was an error deploying functions:

...

- Error Failed to update function xxx in region asia-northeast1
- Error Failed to update function xxx in region asia-northeast1
Error: Process completed with exit code 2.

原因

エラーが起こったプロジェクトはyarnによるmonorepo構成でプロジェクトを構成しておりました。
cf:

事前にローカルでビルドを実施し、成果物のみをアップロードしている形になります。
それが、ある時を堺にGCP上でビルドが走るようになり、devDependencyのものはないためエラーが起こるようになりました。

調べてみるとどうやら原因と思わしきbreakingな変更があることを発見しました。

All 1st and 2nd gen Google Cloud Functions using the Node.js runtime now automatically run the npm run build command during deployment if they define an npm build script in their package.json file. This feature is rolling out over the next few days.

つまり、要約すると、デプロイ時にnpm run buildコマンドを自動的に実行します。 ということになります。
キイテナイヨーーー!!!
(情報収集不足なだけ?)

対策

buildコマンドを動かさないようにすれば解決できるため、release notesにも記載がある2つの方法のうち、一つを実施するだけになります。

方法1

functionsのpackage.jsonのscriptディレクトリに"gcp-build":""を追加すると今まで通りデプロイ時に何も実行しなくなります。

package.json
{
  "name": "@app/functions",
  "scripts": {
    ...
    "gcp-build": ""
  }
}

今回もぐめっとはこちらの方法で対策させていだきました。

方法2

環境変数としてGOOGLE_NODE_RUN_SCRIPTSを空でセットしてあげまると今まで通りデプロイ時に何も実行しなくなります。
gcloud functions deployを使っている場合は--set-build-env-vars=GOOGLE_NODE_RUN_SCRIPTS=""のオプションをセットしてあげます。

まとめ

package.jsonのscriptsに空のgcp-buildを追加する! ことでデプロイ時のビルドが走らなくなり、問題が解決できます。

最後に、スノボの楽しく滑るコツを掴める cotsume というアプリを作ってます!よかったらDLしてみてね!

また、ワンナイト人狼オンラインというゲームを作ってます!よかったら遊んでね!

他にもCameconOffcha、問い合わせ対応が簡単にできるCSmart、フリーランスのコミュニティのNextFreelanceといったサービスも作ってるのでよかったら使ってね!

また、チームビルディングや技術顧問、Firebaseの設計やアドバイスといったお話も受け付けてますので御用の方は弊社までお問い合わせください。

ラグナロクでもエンジニアやデザイナーのメンバーを募集しています!!楽しくぶち上げたい人はぜひお話ししましょう!!

4
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
4
0