Problem
I have an NX repository with multiple applications that I am able to build & start successfully locally. I'm now looking to deploy these applications to Google AppEngine.
In the future I will be looking at implementing this through GitHub Actions but for the moment I was just looking at deploying directly from my local machine using gcloud app deploy app.yaml --quiet. In other (non NX) repositories, I'm able to do this easily, but I'm having trouble with the best approach to use within an NX workspace.
I've played around with trying to deploy an app from the apps folder using an project target e.g. nx run api-one:deploy and also from the root of the application using an npm target npm run deploy:api-two.
Error
- Deploying using nx project target (
nx run api-one:deploy)
I get the error Service Unavailable when trying to access the deployed app and in the logs I get saying Cannot find package 'express' imported from /workspace/dist/main.js
- Deploying using npm target (
npm run deploy:api-two)
I get the error Service Unavailable when trying to access the deployed app and in the logs I get saying /workspace/dist/apps/api-two/main.js
Question
How can I deploy a single application in my nx workspace to Google AppEngine using the command gcloud app deploy ...
Code
A Sample Repository containing the code outlined below can be found here.
The code structure looks something like this
nx-workspace
|
--apps
|
-- api-one
-- src
-- main.ts
-- ...
-- package.json
-- project.json
-- app.yaml
....
-- api-two
-- src
-- main.ts
-- ...
-- package.json
-- project.json
-- app.yaml
....
-- dist
-- apps
-- api-one
-- main.js
-- ...
-- api-two
-- main.js
-- ...
...
package.json
...
api-one package.json
{
"name": "api-one",
"version": "0.0.1",
"type": "module",
"scripts": {
"start": "node ./dist/apps/api-one/main.js",
"gcp-build": ""
}
}
api-one project.json
...
"build": {
"executor": "@nx/esbuild:esbuild",
"options": {
"main": "apps/api-one/src/main.ts",
"tsConfig": "apps/api-one/tsconfig.app.json",
"outputPath": "dist/apps/api-one"
},
"configurations": {
"prod": {
"optimization": false,
"generatePackageJson": true,
"extractLicenses": true,
"inspect": false
}
}
},
"deploy": {
"executor": "nx:run-commands",
"dependsOn": [
"^build:prod"
],
"defaultConfiguration": "dev",
"options": {
"cwd": "apps/api-one",
"parallel": false
},
"configurations": {
"dev": {
"command": "gcloud app deploy app.yaml --quiet"
}
}
},
...
api-one app.yaml
runtime: nodejs18
instance_class: F1
handlers:
- url: /static
static_dir: public/static
expiration: 30d
- url: /.*
secure: always
redirect_http_response_code: 301
script: auto
root package.json
...
"scripts": {
"start": "node ./dist/apps/api-two/main.js",
"gcp-build": "",
"deploy:app-one": "gcloud app deploy ./apps/api-two/app.yaml --quiet"
},
...