From c146a0447e5b9e107440b86941940b76dd3e53ba Mon Sep 17 00:00:00 2001 From: Raphael Kabo Date: Thu, 1 Aug 2024 09:44:04 +0100 Subject: [PATCH 1/2] Switch to deploy via Docker Compose --- .github/workflows/ci.yaml | 4 +-- .github/workflows/deploy.yaml | 16 +++--------- .github/workflows/publish-ghcr.yaml | 40 +++++++++++++++++++++++++++++ Dockerfile | 10 +++++++- deploy.sh | 23 ----------------- docker-compose.yml | 4 ++- docs/running-gathio/installation.md | 28 +++++++++++++++----- package.json | 9 +++---- pnpm-lock.yaml | 6 ++--- 9 files changed, 85 insertions(+), 55 deletions(-) create mode 100644 .github/workflows/publish-ghcr.yaml delete mode 100755 deploy.sh diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 30ce676..a818c17 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -14,7 +14,7 @@ jobs: uses: actions/checkout@v4 - name: Install pnpm - uses: pnpm/action-setup@v2 + uses: pnpm/action-setup@v4 with: version: 9 @@ -44,7 +44,7 @@ jobs: uses: actions/checkout@v4 - name: Install pnpm - uses: pnpm/action-setup@v2 + uses: pnpm/action-setup@v4 with: version: 9 diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index fd2afea..9b11022 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -9,14 +9,6 @@ jobs: deploy: runs-on: ubuntu-latest steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Install pnpm - uses: pnpm/action-setup@v2 - with: - version: 9 - - name: Set up known_hosts file run: | mkdir -p ~/.ssh/ && touch ~/.ssh/known_hosts @@ -26,8 +18,6 @@ jobs: with: ssh-private-key: ${{ secrets.SSH_KEY }} - - name: Install PM2 - run: pnpm install -g pm2 - - - name: Deploy - run: pnpm run deploy + - name: Run deploy script + run: | + ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} 'cd ${{ secrets.DEPLOY_PATH }} && ./deploy.sh' diff --git a/.github/workflows/publish-ghcr.yaml b/.github/workflows/publish-ghcr.yaml new file mode 100644 index 0000000..b30dc8e --- /dev/null +++ b/.github/workflows/publish-ghcr.yaml @@ -0,0 +1,40 @@ +name: Publish to GHCR +on: + workflow_dispatch: + push: + branches: + - main + +jobs: + publish: + permissions: + packages: write + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: get-npm-version + id: package-version + uses: martinbeentjes/npm-get-version-action@v1.3.1 + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to GHCR + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Publish to GHCR + uses: docker/build-push-action@v4 + with: + context: . + file: ./Dockerfile + push: true + platforms: linux/amd64,linux/arm/v7,linux/arm64 + tags: | + ghcr.io/${{ github.repository }}:${{ github.sha }} + ghcr.io/${{ github.repository }}:${{ steps.package-version.outputs.current-version }} + ghcr.io/${{ github.repository }}:latest diff --git a/Dockerfile b/Dockerfile index 5da290e..61947a8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ -FROM node:20-alpine +# Docker builds hang in arm/v7 images, so we use Node 18 to build and Node 20 to run +# Cf. https://github.com/docker/build-push-action/issues/1071 +FROM node:18-alpine AS BUILD_IMAGE WORKDIR /app RUN apk add --no-cache python3 build-base ADD package.json pnpm-lock.yaml /app/ @@ -8,4 +10,10 @@ COPY . /app/ # Always exit 0 here because TSC will fail while we're migrating to TypeScript but # not everything uses TypeScript RUN pnpm run build; exit 0 + +# Now we run the app +FROM node:20-alpine +ENV NODE_ENV=production +WORKDIR /app +COPY --from=BUILD_IMAGE /app ./ CMD ["node", "dist/start.js"] diff --git a/deploy.sh b/deploy.sh deleted file mode 100755 index 9d41e63..0000000 --- a/deploy.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash -set -e - -# Set up and redeploy Gathio. Called by `pm2 deploy`. -# Working directory is the root of the repo. - -# PM2 doesn't load the user env, so this gets nvm into the PATH -export NVM_DIR=~/.nvm -source ~/.nvm/nvm.sh -# Set the right Node version -nvm use -# Need to use `development` here else pnpm won't install devDependencies (i.e. tsc) -NODE_ENV=development pnpm install -# This calls `tsc`, which fails due to type errors while we're transitioning -# fully to TypeScript, so we short-circuit it to true to continue running the -# deploy script. TODO: Remove this short-circuit when we've migrated to -# TypeScript and don't expect any more errors. -pnpm build || true -pm2 reload ecosystem.config.cjs production -pm2 save - -# Build the docs and deploy -mkdocs build -d /var/www/docs.gath.io \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index a176800..5a8a0d9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,8 @@ volumes: services: gathio: - build: . + container_name: gathio-app + image: ghcr.io/lowercasename/gathio:latest links: - mongo ports: @@ -14,6 +15,7 @@ services: # The path to Gathio's user-uploaded event images folder - change to match your system - ./gathio-docker/images:/app/public/events mongo: + container_name: gathio-db image: mongo:latest volumes: - mongodb_data_db:/data/db diff --git a/docs/running-gathio/installation.md b/docs/running-gathio/installation.md index 9b9191e..cdb3f44 100644 --- a/docs/running-gathio/installation.md +++ b/docs/running-gathio/installation.md @@ -91,7 +91,8 @@ the subject](https://www.linode.com/docs/web-servers/nginx/use-nginx-reverse-pro ## Docker The easiest way to run Gathio using Docker is by using the provided -`docker-compose` configuration. +`docker-compose` configuration. We provide a Docker image at [GitHub +Container Repository](https://github.com/lowercasename/gathio/pkgs/container/gathio). Create a directory on your system where you'll keep the Gathio configuration file and another where Gathio can store user-uploaded event images. Copy the @@ -112,19 +113,32 @@ volumes: ``` Adjust any settings in the config file, especially the MongoDB URL, which should -read as follows for the standard Dockerfile config, and the email service if you +read as follows for the standard Docker Compose config, and the email service if you want to enable it: ```ini -mail_service = "nodemailer" mongodb_url = "mongodb://mongo:27017/gathio" +mail_service = "nodemailer" +``` + +You can copy the `docker-compose.yml` file into that same `gathio-docker` directory +you created - you don't need any of the source code. Once you're done, your directory +should look something like this: + +``` +gathio-docker +├── config +│ └── config.toml +├── docker-compose.yml +└── images ``` -Finally, start the Docker stack: +Finally, from wherever you've put your `docker-compose.yml` file, start the Docker stack: ```bash -docker-compose up -d --build +cd gathio-docker +docker-compose up -d ``` -Gathio should now be running on `http://localhost:3000`, and storing data in a -Docker volume. +Gathio should now be running on `http://localhost:3000`, storing data in a +Docker volume, and storing images on your filesystem. diff --git a/package.json b/package.json index a6e3035..26d1d00 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,7 @@ "start": "node dist/start.js", "dev": "nodemon -e ts,js --watch src --exec \"pnpm run build ; pnpm run start\"", "test:dev": "CYPRESS=true pnpm run dev & wait-on http://localhost:3000 && cypress open --e2e --browser chrome", - "test": "pnpm run build || true && CYPRESS=true pnpm run start & wait-on http://localhost:3000 && cypress run --e2e --browser chrome", - "deploy": "pm2 deploy ecosystem.config.cjs production" + "test": "pnpm run build || true && CYPRESS=true pnpm run start & wait-on http://localhost:3000 && cypress run --e2e --browser chrome" }, "engines": { "node": ">=16.16.0" @@ -47,6 +46,7 @@ "request": "^2.88.2", "sanitize-html": "^2.13.0", "toml": "^3.0.0", + "typescript": "^5.4.5", "wait-on": "^7.2.0" }, "devDependencies": { @@ -60,7 +60,6 @@ "cypress": "^13.10.0", "eslint": "^8.57.0", "nodemon": "^2.0.22", - "prettier": "^3.2.5", - "typescript": "^5.4.5" + "prettier": "^3.2.5" } -} +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ee7f920..5139a4a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -92,6 +92,9 @@ importers: toml: specifier: ^3.0.0 version: 3.0.0 + typescript: + specifier: ^5.4.5 + version: 5.4.5 wait-on: specifier: ^7.2.0 version: 7.2.0 @@ -129,9 +132,6 @@ importers: prettier: specifier: ^3.2.5 version: 3.2.5 - typescript: - specifier: ^5.4.5 - version: 5.4.5 packages: From cd2c4ee2a0e330b44245a2893b4842d0bb1d8cae Mon Sep 17 00:00:00 2001 From: Raphael Kabo Date: Thu, 1 Aug 2024 09:58:38 +0100 Subject: [PATCH 2/2] Fix workflow name --- .github/workflows/publish-ghcr.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/publish-ghcr.yaml b/.github/workflows/publish-ghcr.yaml index b30dc8e..0b762b2 100644 --- a/.github/workflows/publish-ghcr.yaml +++ b/.github/workflows/publish-ghcr.yaml @@ -1,4 +1,4 @@ -name: Publish to GHCR +name: CI / Publish to GHCR on: workflow_dispatch: push: