From 4c7b007a281af6ee0540b87a3b2d36758f51f186 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Wed, 22 Nov 2023 10:29:40 +0100 Subject: [PATCH 1/4] docs: update deployment guide --- docs/README.md | 106 ++++++++++++------------------------------------- package.json | 2 +- 2 files changed, 27 insertions(+), 81 deletions(-) diff --git a/docs/README.md b/docs/README.md index 8aedd0367..dbd01deaa 100644 --- a/docs/README.md +++ b/docs/README.md @@ -43,16 +43,9 @@ If you're managing PostgreSQL manually (and don't need mailhog), you can skip this section. Otherwise: ```sh -cd tools -docker compose up -d -cd .. +npm run run-tools ``` -OR - -Alternatively, you can use `npm run run-tools` in the root directory to start up -these services. - ### Run initial setup Install dependencies. @@ -82,7 +75,7 @@ account to login to the frontend app. Once this is complete you will see the seeded data in the admin panel and you can use the seeded users to login in the Next.js app. Email and password of the seeded users are in `src/seed/index.js` file. One account of each -role(Contributor and Editor) is seeded. +role (Contributor and Editor) is seeded. Go to http://localhost:3000/ to see the frontend app and login with a seeded user. @@ -102,7 +95,7 @@ user. under `config/sync` directory. Include those files in your Git commit. For more details see: https://market.strapi.io/plugins/strapi-plugin-config-sync -- API docs is available at: http://localhost:1337/documentation/v1.0.0 . The +- API docs are available at: http://localhost:1337/documentation/v1.0.0. The Strapi app needs to be running to view the docs. #### How to use Admin Panel @@ -121,7 +114,7 @@ user. ### frontend (Next.js) - We will be using Auth0 as a sign-in method in production - + - TODO: update this section when the invitation workflow is implemented. - You have to create the user on `backend` (Strapi) app **by signing in through the `frontend` (Next.js) app for the first time**. If you manually create the user of the same email address from the Strapi Admin panel @@ -130,88 +123,41 @@ user. ## How to run the apps in production mode with Docker -### backend (Strapi) - -To build the docker image: - -``` -docker compose -f docker-compose.prod.yml build -``` - -To start the docker container: (Replace `***` with the values you want to set -for the environment variables.) - -``` -VARIABLE1_NAME=*** VARIABLE2_NAME=*** ... \ -docker compose -f docker-compose.prod.yml up -d -``` - -Alternatively, using .env file: (Replace .env with the path to the .env file you -want to use) - -``` -docker compose --env-file .env -f docker-compose.prod.yml up -d -``` - -Refer to `.env.example` for the list of environment variables. - -To stop the docker container: +### Build -``` -docker compose -f docker-compose.prod.yml down -``` - -#### Initial setup +The 'build' workflow will build the docker images for all the apps. To trigger a build, go to [the workflow](https://github.com/freeCodeCamp/publish/actions/workflows/build.yml) and click on the 'Run workflow' button. -The app requires some configurations for full functionality. +### Deploy (VM) -- Import config - -``` -# Go into the strapi-production container -docker exec -it strapi-production sh +After the build has completed, pull the latest images from Docker Hub: -# Import config -npm run cs import +```sh +docker image pull registry.digitalocean.com/fcc-cr/dev/publish-frontend:latest +docker image pull registry.digitalocean.com/fcc-cr/dev/publish-backend:latest +docker image pull registry.digitalocean.com/fcc-cr/dev/publish-cron:latest ``` -- Visit admin panel and create the first admin account - -- In Content Manager, create `Invited User` entry for the first user. This will - allow the first user on the frontend app to login using Auth0. Set the - following values: - - email: valid email you can login using Auth0 - - role: choose "Editor" (unless you can't invite other users) - - accepted: false -- In Settings > Providers, enable `auth0`. -- (If you want to access endpoints that uses API token) In Settings > API - Tokens, generate API tokens - -### frontend (Next.js) +Update the three environment files (`.env-frontend`, `.env-backend` and `.env-cron`) if there are any changes to the environment variables (compare them with the `apps/*/sample.env` files). -To build the docker image: +Then run the containers (stoppping any existing containers first): -``` -docker compose -f docker/production/docker-compose.yml build +```sh +docker run -d --env-file .env-backend -p 127.0.0.1:1337:1337 registry.digitalocean.com/fcc-cr/dev/publish-backend +docker run -d --env-file .env-frontend -p 127.0.0.1:3000:3000 registry.digitalocean.com/fcc-cr/dev/publish-frontend ``` -To start the docker container: (Replace `***` with the values you want to set -for the environment variables) +To start the cron container, the `.env-cron` file's `STRAPI_ACCESS_TOKEN` must have an api token. If there isn't one, create an api token in the [admin panel](https://publish-backend-anhgw.ondigitalocean.app/admin/settings/api-tokens/create) with the token type "custom" and one permission: `post.checkAndPublish`. Then run the cron container: -``` -NEXTAUTH_SECRET=*** AUTH0_CLIENT_ID=*** AUTH0_CLIENT_SECRET=*** \ -NEXT_PUBLIC_STRAPI_BACKEND_URL=*** NEXTAUTH_URL=*** AUTH0_DOMAIN=*** \ -docker compose -f docker/production/docker-compose.yml up -d +```sh +docker run -d --env-file .env-cron registry.digitalocean.com/fcc-cr/dev/publish-cron ``` -Alternatively, using .env file: +#### Logs -``` -docker compose --env-file .env -f docker/production/docker-compose.yml up -d -``` - -To stop the docker container: +The container logs are piped to log files in the home directory. To set this up: -``` -docker compose -f docker/production/docker-compose.yml down +```sh +docker logs -f publish-backend >> publish-backend.log & +docker logs -f publish-frontend >> publish-frontend.log & +docker logs -f publish-cron >> publish-cron.log & ``` diff --git a/package.json b/package.json index 13969d149..2a96e56ce 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "seed": "turbo seed", "start": "turbo start", "develop": "turbo develop", - "run-tools": "cd tools && docker compose up -d && cd ..", + "run-tools": "cd tools && docker compose up -d", "test": "turbo test", "turbo": "turbo" }, From ca3800591d0bdc1cbdd1cc81f14e6bb48a5840a1 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Wed, 22 Nov 2023 11:04:05 +0100 Subject: [PATCH 2/4] chore: update sample.env with more instructions --- apps/backend/sample.env | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/apps/backend/sample.env b/apps/backend/sample.env index 38c36b6f2..e5f65484a 100644 --- a/apps/backend/sample.env +++ b/apps/backend/sample.env @@ -1,3 +1,4 @@ +# Optional in production HOST=localhost PORT=1337 @@ -12,12 +13,18 @@ TRANSFER_TOKEN_SALT=tobemodified JWT_SECRET=tobemodified # Database (Postgres) -DATABASE_HOST=localhost # You don't need this value if you are using Docker Compose +DATABASE_HOST=localhost DATABASE_PORT=5432 DATABASE_NAME=strapi DATABASE_USERNAME=strapi DATABASE_PASSWORD=password DATABASE_CLIENT=postgres +# true in production: +DATABASE_SSL= +# true in production: +DATABASE_SSL_REJECT_UNAUTHORIZED= +# CA certificate value, e.g. ${db.CA_CERT} in DigitalOcean App Platform +DATABASE_SSL_CA= # Environment NODE_ENV=development From b1458051918fb32ac9ab669bf7b25af2d9d739ec Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Wed, 22 Nov 2023 11:04:41 +0100 Subject: [PATCH 3/4] docs: add DigitalOcean app platform guide --- docs/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/README.md b/docs/README.md index dbd01deaa..18c82bffc 100644 --- a/docs/README.md +++ b/docs/README.md @@ -161,3 +161,15 @@ docker logs -f publish-backend >> publish-backend.log & docker logs -f publish-frontend >> publish-frontend.log & docker logs -f publish-cron >> publish-cron.log & ``` + +### Deploy (DigitalOcean App Platform) + +Almost everything is standard, but there are a few things to note: + +App types: the `backend` and `frontend` apps need to be deployed as a `Web Service` and + the `cron` app needs to be deployed as a `Worker`. + +Environment variables + +- `frontend` needs to have `NEXTAUTH_URL=${APP_URL}` otherwise follow the instructions in sample.env +- `backend` needs to have `APP_URL=${APP_URL}`, `NODE_ENV=production`, `HOST` can be omitted or set to `0.0.0.0`, `PORT` can also be omitted \ No newline at end of file From 16b25a079e99acc0cf2430527645122e494cfff7 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Wed, 22 Nov 2023 15:58:24 +0100 Subject: [PATCH 4/4] docs: clarify env var language. Co-authored-by: Niraj Nandish --- docs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 18c82bffc..6853414c2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -172,4 +172,4 @@ App types: the `backend` and `frontend` apps need to be deployed as a `Web Servi Environment variables - `frontend` needs to have `NEXTAUTH_URL=${APP_URL}` otherwise follow the instructions in sample.env -- `backend` needs to have `APP_URL=${APP_URL}`, `NODE_ENV=production`, `HOST` can be omitted or set to `0.0.0.0`, `PORT` can also be omitted \ No newline at end of file +- `backend` needs to have `APP_URL=${APP_URL}` and `NODE_ENV=production`. `HOST` can be omitted or set to `0.0.0.0` and `PORT` can also be omitted. \ No newline at end of file