Skip to content

Commit

Permalink
Added docker deployment (#9263)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisassad authored Oct 7, 2024
1 parent 8d73237 commit 79d12ea
Show file tree
Hide file tree
Showing 40 changed files with 454 additions and 113 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ node_modules/
**/test-results
**/*.tsbuildinfo
**/.DS_Store
*.env
*.log
.idea
.vscode
Expand Down
47 changes: 47 additions & 0 deletions .github/workflows/CD.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Build Docker Image

on:
push:
branches:
- staging
- frick
- frack
- production

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Set Heroku app name based on branch
id: set-heroku-app-name
run: |
BRANCH_NAME=${GITHUB_REF##*/}
case $BRANCH_NAME in
frick)
APP_NAME="commonwealth-frick"
;;
frack)
APP_NAME="commonwealth-frack"
;;
staging)
APP_NAME="commonwealth-beta"
;;
demo)
APP_NAME="commonwealth-demo"
;;
production)
APP_NAME="commonwealthapp"
;;
*)
echo "Branch $BRANCH_NAME is not configured for deployment."
exit 1
;;
esac
echo "HEROKU_APP_NAME=$APP_NAME" >> $GITHUB_ENV
- uses: actions/checkout@v2

- name: Deploy to Heroku
run: |
chmod +x ./scripts/deploy-to-heroku.sh
./scripts/deploy-to-heroku.sh "${{ secrets.HEROKU_EMAIL }}" "${{ secrets.HEROKU_API_TOKEN }}" "${{ env.HEROKU_APP_NAME }}"
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ jobs:
- name: Run serial e2e tests
run: |
pnpm -F commonwealth bootstrap-test-db &&
(cd packages/commonwealth && NODE_OPTIONS=--max-old-space-size=$(../../scripts/get-max-old-space-size.sh) node --import=extensionless/register --enable-source-maps ./build/server.js) &
(cd packages/commonwealth && node --import=extensionless/register --enable-source-maps ./build/server.js) &
(pnpm -F commonwealth wait-server && pnpm -F commonwealth test-e2e-serial --forbid-only)
- name: Archive code coverage results
Expand Down
25 changes: 0 additions & 25 deletions Dockerfile

This file was deleted.

54 changes: 54 additions & 0 deletions Dockerfile.commonwealth_base
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
FROM datadog-base AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

FROM base AS build
## needed for node-gyp to build
RUN apt-get update && \
apt-get install -y python3 make gcc g++
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm run build

RUN pnpm -F discord-bot run build
RUN pnpm deploy -F discord-bot --prod /prod/discord-bot
RUN mv /usr/src/app/packages/discord-bot/build /prod/discord-bot/build

RUN pnpm -F snapshot-listener run build
RUN pnpm deploy -F snapshot-listener --prod /prod/snapshot-listener
RUN mv /usr/src/app/packages/snapshot-listener/build /prod/snapshot-listener/build

RUN pnpm deploy -F commonwealth --prod /prod/commonwealth
RUN mv /usr/src/app/packages/commonwealth/build /prod/commonwealth/build

FROM base AS commonwealth
ENV NODE_ENV=production
COPY --from=build /prod/commonwealth /prod/commonwealth
WORKDIR /prod/commonwealth
COPY ./scripts/datadog-entrypoint.sh /prod/commonwealth
RUN chmod +x /prod/commonwealth/datadog-entrypoint.sh
COPY ./scripts/get-max-old-space-size.sh /prod/commonwealth/scripts
RUN chmod +x /prod/commonwealth/scripts/get-max-old-space-size.sh
ENV PORT=$PORT

FROM base AS snapshot-listener
ENV NODE_ENV=production
COPY --from=build /prod/snapshot-listener /prod/snapshot-listener
WORKDIR /prod/snapshot-listener
COPY ./scripts/datadog-entrypoint.sh /prod/snapshot-listener
RUN chmod +x /prod/snapshot-listener/datadog-entrypoint.sh
COPY ./scripts/get-max-old-space-size.sh /prod/snapshot-listener/scripts
RUN chmod +x /prod/snapshot-listener/scripts/get-max-old-space-size.sh
ENV PORT=$PORT

FROM base AS discord-bot
ENV NODE_ENV=production
COPY --from=build /prod/discord-bot /prod/discord-bot
WORKDIR /prod/discord-bot
COPY ./scripts/datadog-entrypoint.sh /prod/discord-bot
RUN chmod +x /prod/discord-bot/datadog-entrypoint.sh
COPY ./scripts/get-max-old-space-size.sh /prod/discord-bot/scripts
RUN chmod +x /prod/discord-bot/scripts/get-max-old-space-size.sh
ENV PORT=$PORT
28 changes: 28 additions & 0 deletions Dockerfile.datadog
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM node:20-slim AS datadog-base

# Install Datadog dependencies
RUN apt-get update && apt-get install -y gnupg apt-transport-https gpg-agent curl ca-certificates

# Add Datadog repository and signing keys
ENV DATADOG_APT_KEYRING="/usr/share/keyrings/datadog-archive-keyring.gpg"
ENV DATADOG_APT_KEYS_URL="https://keys.datadoghq.com"
RUN sh -c "echo 'deb [signed-by=${DATADOG_APT_KEYRING}] https://apt.datadoghq.com/ stable 7' > /etc/apt/sources.list.d/datadog.list"
RUN touch ${DATADOG_APT_KEYRING}
RUN curl -o /tmp/DATADOG_APT_KEY_CURRENT.public "${DATADOG_APT_KEYS_URL}/DATADOG_APT_KEY_CURRENT.public" && \
gpg --ignore-time-conflict --no-default-keyring --keyring ${DATADOG_APT_KEYRING} --import /tmp/DATADOG_APT_KEY_CURRENT.public
RUN curl -o /tmp/DATADOG_APT_KEY_06462314.public "${DATADOG_APT_KEYS_URL}/DATADOG_APT_KEY_06462314.public" && \
gpg --ignore-time-conflict --no-default-keyring --keyring ${DATADOG_APT_KEYRING} --import /tmp/DATADOG_APT_KEY_06462314.public
RUN curl -o /tmp/DATADOG_APT_KEY_C0962C7D.public "${DATADOG_APT_KEYS_URL}/DATADOG_APT_KEY_C0962C7D.public" && \
gpg --ignore-time-conflict --no-default-keyring --keyring ${DATADOG_APT_KEYRING} --import /tmp/DATADOG_APT_KEY_C0962C7D.public
RUN curl -o /tmp/DATADOG_APT_KEY_F14F620E.public "${DATADOG_APT_KEYS_URL}/DATADOG_APT_KEY_F14F620E.public" && \
gpg --ignore-time-conflict --no-default-keyring --keyring ${DATADOG_APT_KEYRING} --import /tmp/DATADOG_APT_KEY_F14F620E.public
RUN curl -o /tmp/DATADOG_APT_KEY_382E94DE.public "${DATADOG_APT_KEYS_URL}/DATADOG_APT_KEY_382E94DE.public" && \
gpg --ignore-time-conflict --no-default-keyring --keyring ${DATADOG_APT_KEYRING} --import /tmp/DATADOG_APT_KEY_382E94DE.public

# Install Datadog Agent
RUN apt-get update && apt-get -y install --reinstall datadog-agent

# Expose DogStatsD and trace-agent ports
EXPOSE 8125/udp 8126/tcp

COPY datadog-config/ /etc/datadog-agent/
31 changes: 31 additions & 0 deletions common_knowledge/Deployment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Deployment Flow
In order to perform a deployment, we need to push to a branch that corresponds with a heroku application. The branches
are frick, frack, beta. This then runs a github action that takes care of the deployment.

# Deployment Details
We use Heroku for deploying our applications. The deployment process is automated via GitHub Actions using a Continuous Deployment (CD) pipeline. Every time we push changes to a branch associated with a specific app (e.g., frick, frack, beta, etc.), the CD.yml GitHub action is triggered, which initiates the deployment process.

## Deployment Workflow Overview
### Base Container Build:

First, our base container image is built using the following command:
bash
```shell
docker build -f Dockerfile.commonwealth_base -t commonwealth_base .
```
This image contains the core setup required by all of our apps, ensuring consistency and reducing redundancy across different services.
### Individual Process Builds:

After building the base container, the individual process-specific Dockerfiles (e.g., Dockerfile.web, Dockerfile.evm_ce) are built and pushed to Heroku using the following command:
bash
```shell
heroku container:push --recursive -a ${heroku_app_name}
```
The --recursive flag ensures that all defined processes (web, consumer, evm_ce, etc.) are built and pushed to the appropriate Heroku app.
### Release on Heroku:

After the container images are successfully pushed, the next step is to release these images to Heroku:
```shell
heroku container:release web evm_ce consumer message_relayer knock -a ${heroku_app_name}
```
This command releases the defined processes to their respective dynos on Heroku, making the application live.
Empty file added datadog-config/datadog.yaml
Empty file.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ services:
- with-app
container_name: commonwealth
build:
context: . # Assuming Dockerfile is in the same directory as this docker-compose.yml
dockerfile: Dockerfile # Name of the Dockerfile
context: .
dockerfile: ./Dockerfile.web
ports:
- '8080:8080'
environment:
Expand Down
16 changes: 13 additions & 3 deletions libs/model/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import { buildDb } from './models';

const log = logger(import.meta);

console.log(
'dialect options are',
config.NODE_ENV !== 'production' || config.DB.NO_SSL
? { requestTimeout: 40000 }
: config.DB.URI ===
'postgresql://commonwealth:edgeware@localhost/commonwealth'
? { requestTimeout: 40000, ssl: false }
: { requestTimeout: 40000, ssl: { rejectUnauthorized: false } },
);

export const sequelize = new Sequelize(config.DB.URI, {
// disable string operators (https://github.com/sequelize/sequelize/issues/8417)
// operatorsAliases: false,
Expand All @@ -13,9 +23,9 @@ export const sequelize = new Sequelize(config.DB.URI, {
config.NODE_ENV !== 'production' || config.DB.NO_SSL
? { requestTimeout: 40000 }
: config.DB.URI ===
'postgresql://commonwealth:edgeware@localhost/commonwealth'
? { requestTimeout: 40000, ssl: false }
: { requestTimeout: 40000, ssl: { rejectUnauthorized: false } },
'postgresql://commonwealth:edgeware@localhost/commonwealth'
? { requestTimeout: 40000, ssl: false }
: { requestTimeout: 40000, ssl: { rejectUnauthorized: false } },
pool: {
max: 10,
min: 0,
Expand Down
2 changes: 2 additions & 0 deletions libs/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"@canvas-js/gossiplog": "^0.10.10",
"@canvas-js/interfaces": "^0.10.10",
"@canvas-js/signatures": "^0.10.10",
"@cosmjs/encoding": "0.32.3",
"@ipld/dag-json": "^10.2.0",
"@polkadot/util": "12.6.2",
"@libp2p/peer-id-factory": "^4.2.4",
"moment": "^2.23.0",
"safe-stable-stringify": "^2.4.2"
Expand Down
24 changes: 23 additions & 1 deletion libs/shared/src/canvas/runtime/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
createEd25519PeerId,
createFromProtobuf,
} from '@libp2p/peer-id-factory';
import { ConnectionConfig } from 'pg';

import { getSessionSigners } from '../signers';
import { contract, contractTopic } from './contract';
Expand All @@ -24,10 +25,31 @@ export const startCanvasNode = async (config: { PEER_ID?: string }) => {
? await createFromProtobuf(Buffer.from(config.PEER_ID, 'base64'))
: await createEd25519PeerId();

let pgConnectionConfig: ConnectionConfig = {};

if (path) {
const url = new URL(path);

pgConnectionConfig = {
user: url.username,
host: url.hostname,
database: url.pathname.slice(1), // remove the leading '/'
password: url.password,
port: url.port ? parseInt(url.port) : 5432,
ssl: false,
};
}

if (process.env.NODE_ENV === 'production') {
pgConnectionConfig.ssl = {
rejectUnauthorized: false,
};
}

const app = await Canvas.initialize({
peerId,
topic: contractTopic,
path,
path: pgConnectionConfig,
contract,
signers: getSessionSigners(),
bootstrapList: [],
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"packageManager": "[email protected]",
"scripts": {
"build": "chmod u+x scripts/build.sh && ./scripts/build.sh",
"docker-build-commonwealth": "docker build . --target commonwealth --tag commonwealth:latest",
"docker-build-commonwealth": "docker build -f Dockerfile.commonwealth_base --tag commonwealth:latest .",
"chromatic": "chromatic --exit-zero-on-changes",
"clean": "chmod u+x scripts/clean-modules.sh && ./scripts/clean-modules.sh",
"db-all": "pnpm -F commonwealth db-all",
Expand Down Expand Up @@ -125,7 +125,6 @@
"prettier-plugin-organize-imports": "^4.0.0",
"process": "^0.11.10",
"readline-sync": "^1.4.10",
"sequelize-cli": "^6.2.0",
"sharp": "^0.31.2",
"sinon": "^15.0.4",
"source-map-support": "^0.5.21",
Expand Down
6 changes: 0 additions & 6 deletions packages/commonwealth/Procfile

This file was deleted.

2 changes: 2 additions & 0 deletions packages/commonwealth/deploy/dockerfiles/Dockerfile.consumer
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM commonwealth
CMD NODE_ENV=production ./datadog-entrypoint.sh node --import=extensionless/register --max_old_space_size=$(./scripts/get-max-old-space-size.sh) build/server/workers/commonwealthConsumer/commonwealthConsumer.js
2 changes: 2 additions & 0 deletions packages/commonwealth/deploy/dockerfiles/Dockerfile.evm_ce
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM commonwealth
CMD NODE_ENV=production ./datadog-entrypoint.sh node --import=extensionless/register --max_old_space_size=$(./scripts/get-max-old-space-size.sh) build/server/workers/evmChainEvents/startEvmPolling.js
2 changes: 2 additions & 0 deletions packages/commonwealth/deploy/dockerfiles/Dockerfile.knock
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM commonwealth
CMD NODE_ENV=production ./datadog-entrypoint.sh node --import=extensionless/register --max_old_space_size=$(./scripts/get-max-old-space-size.sh) build/server/workers/knock/knockWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM commonwealth
CMD NODE_ENV=production ./datadog-entrypoint.sh node --import=extensionless/register --max_old_space_size=$(./scripts/get-max-old-space-size.sh) build/server/workers/messageRelayer/messageRelayer.js
5 changes: 5 additions & 0 deletions packages/commonwealth/deploy/dockerfiles/Dockerfile.release
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM commonwealth
RUN apt-get update && apt-get install -y curl # Needed for heroku
CMD node --import=extensionless/register build/server/scripts/releasePhaseEnvCheck.js && \
npx sequelize-cli db:migrate --config server/sequelize.json && \
node --import=extensionless/register build/server/scripts/purgeCloudflareCache.js
2 changes: 2 additions & 0 deletions packages/commonwealth/deploy/dockerfiles/Dockerfile.web
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM commonwealth
CMD NODE_ENV=production ./datadog-entrypoint.sh node --import=extensionless/register --max_old_space_size=$(./scripts/get-max-old-space-size.sh) --enable-source-maps ./build/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
UNLEASH_FRONTEND_SERVER_URL=https://flag.commonwealth.im/api/frontend
UNLEASH_FRONTEND_API_TOKEN=*:development.309ede2a4ac83b15b51c0cd6b55acdc9f4edefeda5b6325e2414f9b0
HEROKU_APP_NAME=commonwealth-beta
APP_ENV=beta
NODE_ENV=production
KNOCK_PUBLIC_API_KEY=pk_RLg22EIJ6jsuci6c7VvBU59gDQJZeFoeBKlOkgJLWvA
KNOCK_IN_APP_FEED_ID=fc6e68e5-b7b9-49c1-8fab-6dd7e3510ffb
SERVER_URL=https://qa.commonwealth.im
MIXPANEL_DEV_TOKEN=312b6c5fadb9a88d98dc1fb38de5d900
MAGIC_PUBLISHABLE_KEY=pk_live_B0604AA1B8EEFDB4
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
UNLEASH_FRONTEND_SERVER_URL=https://flag.commonwealth.im/api/frontend
UNLEASH_FRONTEND_API_TOKEN=*:development.309ede2a4ac83b15b51c0cd6b55acdc9f4edefeda5b6325e2414f9b0
HEROKU_APP_NAME=commonwealth-demo
APP_ENV=demo
NODE_ENV=production
MIXPANEL_DEV_TOKEN=312b6c5fadb9a88d98dc1fb38de5d900
MAGIC_PUBLISHABLE_KEY=pk_live_EF89AABAFB87D6F4
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
UNLEASH_FRONTEND_SERVER_URL=https://flag.commonwealth.im/api/frontend
UNLEASH_FRONTEND_API_TOKEN=*:development.309ede2a4ac83b15b51c0cd6b55acdc9f4edefeda5b6325e2414f9b0
HEROKU_APP_NAME=commonwealth-frack
APP_ENV=frack
NODE_ENV=production
SERVER_URL=https://commonwealth-frack.herokuapp.com
KNOCK_PUBLIC_API_KEY=pk_EkjqgrIByZo85tIqdBkCmihVBtTB_ixY_37oTG_Au1Y
KNOCK_IN_APP_FEED_ID=fc6e68e5-b7b9-49c1-8fab-6dd7e3510ffb
MIXPANEL_DEV_TOKEN=312b6c5fadb9a88d98dc1fb38de5d900
MAGIC_PUBLISHABLE_KEY=pk_live_B0604AA1B8EEFDB4
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
UNLEASH_FRONTEND_SERVER_URL=https://flag.commonwealth.im/api/frontend
UNLEASH_FRONTEND_API_TOKEN=*:development.309ede2a4ac83b15b51c0cd6b55acdc9f4edefeda5b6325e2414f9b0
HEROKU_APP_NAME=commonwealth-frick
APP_ENV=frick
NODE_ENV=production
SERVER_URL=https://commonwealth-frick.herokuapp.com
MIXPANEL_DEV_TOKEN=312b6c5fadb9a88d98dc1fb38de5d900
MAGIC_PUBLISHABLE_KEY=pk_live_EF89AABAFB87D6F4
Loading

0 comments on commit 79d12ea

Please sign in to comment.