diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 000000000..83966b86c --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,76 @@ +name: ๐Ÿณ Create and publish a Docker image + +on: + push: + branches: + - mainline + +permissions: + actions: write + contents: read + packages: write + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build: + name: ๐Ÿณ Build + if: ${{ github.ref == 'refs/heads/mainline' }} + runs-on: ubuntu-latest + steps: + - name: ๐Ÿ›‘ Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.12.0 + + - name: โฌ‡๏ธ Checkout repo + uses: actions/checkout@v4 + + - name: โœ–๏ธ Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: โšก๏ธ Cache Docker layers + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + + - name: ๐Ÿ‘‹ Log in to the Container registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: ๐Ÿงช Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + labels: | + maintainer=thundersquared + tags: | + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'mainline') }} + type=raw,value={{sha}},enable=${{ github.ref_type != 'tag' }} + + - name: ๐Ÿ›  Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + file: image.Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + BUILDTIME=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }} + REVISION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }} + COMMIT_SHA=${{ github.sha }} + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new + + - name: ๐Ÿšš Move cache + run: | + rm -rf /tmp/.buildx-cache + mv /tmp/.buildx-cache-new /tmp/.buildx-cache diff --git a/image.Dockerfile b/image.Dockerfile new file mode 100644 index 000000000..93cbbda34 --- /dev/null +++ b/image.Dockerfile @@ -0,0 +1,56 @@ +# base node image +FROM node:18-bullseye-slim as base + +# set for base and all layer that inherit from it +ENV NODE_ENV production + +# Install openssl for Prisma +RUN apt-get update && apt-get install -y openssl + +# Install all node_modules, including dev dependencies +FROM base as deps + +WORKDIR /myapp + +ADD package.json ./ +RUN npm install --production=false + +# Setup production node_modules +FROM base as production-deps + +WORKDIR /myapp + +COPY --from=deps /myapp/node_modules /myapp/node_modules +ADD package.json ./ +RUN npm prune --production + +# Build the app +FROM base as build + +WORKDIR /myapp + +COPY --from=deps /myapp/node_modules /myapp/node_modules + +ADD /app/database/schema.prisma . +RUN npx prisma generate + +ADD . . +RUN npm run build + +# Finally, build the production image with minimal footprint +FROM base + +ENV PORT="8080" +ENV NODE_ENV="production" + +WORKDIR /myapp + +COPY --from=production-deps /myapp/node_modules /myapp/node_modules +COPY --from=build /myapp/node_modules/.prisma /myapp/node_modules/.prisma + +COPY --from=build /myapp/build /myapp/build +COPY --from=build /myapp/public /myapp/public +COPY --from=build /myapp/package.json /myapp/package.json +COPY --from=build /myapp/start.sh /myapp/start.sh + +ENTRYPOINT [ "sh", "./start.sh" ]