From 94da9551856c99b0c61d47fcb20ff343c5a8b483 Mon Sep 17 00:00:00 2001 From: MarcUs7i <96580944+MarcUs7i@users.noreply.github.com> Date: Mon, 24 Mar 2025 10:44:29 +0100 Subject: [PATCH] Added docker support --- .dockerignore | 7 +++ .github/workflows/build-and-deploy.yml | 65 ++++++++++++++++++++++++++ .github/workflows/build.yml | 36 ++++++++++++++ .github/workflows/deploy.yml | 27 ----------- Dockerfile | 15 ++++++ docker-compose-build.yml | 13 ++++++ docker-compose.yml | 11 +++++ ecosystem.config.cjs | 2 +- 8 files changed, 148 insertions(+), 28 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/build-and-deploy.yml create mode 100644 .github/workflows/build.yml delete mode 100644 .github/workflows/deploy.yml create mode 100644 Dockerfile create mode 100644 docker-compose-build.yml create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..9e7c524 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +node_modules +.git +.github +.vscode +.nuxt +.output +.data \ No newline at end of file diff --git a/.github/workflows/build-and-deploy.yml b/.github/workflows/build-and-deploy.yml new file mode 100644 index 0000000..a897f19 --- /dev/null +++ b/.github/workflows/build-and-deploy.yml @@ -0,0 +1,65 @@ +name: Build and Deploy + +on: + push: + branches: [ main ] + workflow_dispatch: + release: + types: [created] + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push Docker image + uses: docker/build-push-action@v4 + with: + context: . + push: true + platforms: linux/amd64, linux/arm64/v8, linux/arm/v7 + tags: ghcr.io/marcus7i/marcus7i.net:${{ github.ref_name }},ghcr.io/marcus7i/marcus7i.net:latest + + deploy: + needs: build + runs-on: ubuntu-latest + + steps: + - name: SSH and deploy + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.SSH_HOST }} + username: ${{ secrets.SSH_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + port: ${{ secrets.SSH_PORT }} + script: | + cd /var/www/marcus7i + + # Create .env file with secrets + echo "DISCORD_WEBHOOK=${{ secrets.DISCORD_WEBHOOK }}" > .env + + # Pull the latest image + docker pull ghcr.io/marcus7i/marcus7i.net:latest + + # Stop and start the container + docker-compose down + docker-compose up -d + + # Clean up unused images + docker image prune -f \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..0a5e49a --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,36 @@ +name: Build Docker Image + +on: + workflow_dispatch: + #release: + # types: [created] + +jobs: + build: + runs-on: ubuntu-latest + + permissions: + contents: read + packages: write + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push Docker image + uses: docker/build-push-action@v2 + with: + context: . + push: true + platforms: linux/amd64, linux/arm64/v8, linux/arm/v7 + tags: ghcr.io/marcus7i/marcus7i:${{ github.ref_name }},ghcr.io/marcus7i/marcus7i:latest \ No newline at end of file diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml deleted file mode 100644 index a7cbc4a..0000000 --- a/.github/workflows/deploy.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Deploy to Production - -on: - push: - branches: [ main ] - workflow_dispatch: - -jobs: - deploy: - runs-on: ubuntu-latest - steps: - - name: SSH and deploy - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.SSH_HOST }} - username: ${{ secrets.SSH_USERNAME }} - key: ${{ secrets.SSH_PRIVATE_KEY }} - port: ${{ secrets.SSH_PORT }} - script: | - cd /var/www/marcus7i.net - git pull - npm ci - npm run build - pm2 reload ecosystem.config.cjs || pm2 start ecosystem.config.cjs --env production --env-path=".env" - - # For system restarts - pm2 save \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b4c8ccd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM node:20-alpine as build +WORKDIR /app +COPY package*.json ./ +RUN npm ci +COPY . . +RUN npm run build + +FROM node:20-alpine +WORKDIR /app +COPY --from=build /app/.output /app/.output +ENV NODE_ENV=production +ENV PORT=3000 + +EXPOSE 3000 +CMD ["node", ".output/server/index.mjs"] \ No newline at end of file diff --git a/docker-compose-build.yml b/docker-compose-build.yml new file mode 100644 index 0000000..121583a --- /dev/null +++ b/docker-compose-build.yml @@ -0,0 +1,13 @@ +services: + nuxt-app: + build: + context: . + dockerfile: Dockerfile + container_name: marcus7i + restart: unless-stopped + ports: + - "9031:3000" + env_file: + - .env + volumes: + - ./logs:/app/logs \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2f8c588 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +services: + nuxt-app: + image: ghcr.io/marcus7i/marcus7i.net + container_name: marcus7i + restart: unless-stopped + ports: + - "9031:3000" + env_file: + - .env + volumes: + - ./logs:/app/logs \ No newline at end of file diff --git a/ecosystem.config.cjs b/ecosystem.config.cjs index 9c3bf11..4e9fd6b 100644 --- a/ecosystem.config.cjs +++ b/ecosystem.config.cjs @@ -6,7 +6,7 @@ module.exports = { exec_mode: 'cluster', env: { NODE_ENV: 'production', - PORT: 9031 + PORT: 3000 }, watch: false, max_memory_restart: '1G'