diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..b512c09d --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index aa18c1f6..d3d20894 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,38 +6,46 @@ on: pull_request: branches: [main] +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + jobs: - build-and-test: + build-and-push-docker-image: runs-on: ubuntu-latest strategy: matrix: node-version: [20.x] steps: - uses: actions/checkout@v3 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3 + - name: Log into registry ${{ env.REGISTRY }} + uses: docker/login-action@v2 with: - node-version: ${{ matrix.node-version }} - - run: npm ci - - run: npm run lint - - run: npm run typecheck - - auto-merge: - if: github.event_name == 'pull_request' && github.actor == 'dependabot[bot]' - runs-on: ubuntu-latest - needs: build-and-test - permissions: - contents: write - pull-requests: write - steps: - - name: Dependabot metadata - id: metadata - uses: dependabot/fetch-metadata@v2 + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Setup Docker buildx + uses: docker/setup-buildx-action@v2 + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v4 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + - name: Build and Push Versioned Docker Image + id: build-and-push + uses: docker/build-push-action@v4 + if: ${{ github.ref != 'refs/heads/main' }} + with: + context: . + push: ${{ !github.event.pull_request.head.repo.fork }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + - name: Build and Push Latest Docker Image + id: build-and-push-latest + uses: docker/build-push-action@v4 + if: ${{ github.ref == 'refs/heads/main' }} with: - github-token: '${{ secrets.GITHUB_TOKEN }}' - - name: Auto-merge - if: steps.metadata.outputs.update-type != 'version-update:semver-major' - run: gh pr merge --auto --squash "$PR_URL" - env: - PR_URL: ${{github.event.pull_request.html_url}} - GH_TOKEN: ${{secrets.GITHUB_TOKEN}} + context: . + push: true + tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..a2caa967 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,35 @@ +FROM node:lts-slim AS base + +# Create app directory +WORKDIR /usr/src + +FROM base AS builder + +# Files required by npm install +COPY package*.json ./ + +# Install app dependencies +RUN npm ci + +# Bundle app source +COPY . . + +# Type check app +RUN npm run typecheck + +FROM base AS runner + +# Files required by npm install +COPY package*.json ./ + +# Install only production app dependencies +RUN npm ci --omit=dev + +# Bundle app source +COPY . . + +USER node + +# Start the app +EXPOSE 80 +CMD ["npm", "run", "start:force"] \ No newline at end of file diff --git a/README.md b/README.md index 06e9df56..cbd2f8a4 100644 --- a/README.md +++ b/README.md @@ -34,10 +34,15 @@ Follow these steps to set up and run your bot using this template: 2. **Environment Variables Setup** Create an environment variables file by copying the provided example file: - ```bash - cp .env.example .env - ``` - Open the newly created `.env` file and set the `BOT_TOKEN` environment variable. + ```bash + # development + cp .env.example .env.bot.dev + + # production + cp .env.example .env.bot.prod + ``` + + Open the newly created `.env.bot.dev` and `.env.bot.prod` files and set the `BOT_TOKEN` environment variable. 3. **Launching the Bot** @@ -51,7 +56,7 @@ Follow these steps to set up and run your bot using this template: ``` Start the bot in watch mode (auto-reload when code changes): ```bash - npm run dev + docker compose up ``` **Production Mode:** @@ -65,9 +70,7 @@ Follow these steps to set up and run your bot using this template: Start the bot in production mode: ```bash - npm run start:force # skip type checking and start - # or - npm start # with type checking (requires development dependencies) + docker compose -f compose.yml -f compose.prod.yml up ``` ### List of Available Commands diff --git a/compose.override.yml b/compose.override.yml new file mode 100644 index 00000000..2b835b88 --- /dev/null +++ b/compose.override.yml @@ -0,0 +1,10 @@ +services: + + bot: + ports: + - '3000:80' + volumes: + - '.:/usr/src' + env_file: + - .env.bot.dev + command: npm run dev diff --git a/compose.prod.yml b/compose.prod.yml new file mode 100644 index 00000000..210b771a --- /dev/null +++ b/compose.prod.yml @@ -0,0 +1,5 @@ +services: + + bot: + env_file: + - .env.bot.prod diff --git a/compose.yml b/compose.yml new file mode 100644 index 00000000..c0b0b235 --- /dev/null +++ b/compose.yml @@ -0,0 +1,5 @@ +services: + + bot: + build: + context: .