From 9ff2755e23fef9ea7cdeacd1b585048f44f5fd8e Mon Sep 17 00:00:00 2001 From: Douglas Duteil Date: Fri, 5 Jan 2024 18:49:43 +0100 Subject: [PATCH] ci(github): publish docker image (#47) --- .dockerignore | 16 +++++++++++ .github/workflows/publish.yml | 54 +++++++++++++++++++++++++++++++++++ Dockerfile | 46 +++++++++++++++++++++++++++++ back/Dockerfile | 21 -------------- back/entrypoint.sh | 6 ---- back/package.json | 2 +- front/public/.gitkeep | 0 7 files changed, 117 insertions(+), 28 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/publish.yml create mode 100644 Dockerfile delete mode 100644 back/Dockerfile delete mode 100644 back/entrypoint.sh create mode 100644 front/public/.gitkeep diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..4e218d4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,16 @@ +# +* + +# +!**/package.json +!/back/nest-cli.json +!/back/src +!/back/tsconfig.build.json +!/back/tsconfig.json +!/front/index.html +!/front/public/.gitkeep +!/front/src +!/front/tsconfig.json +!/front/tsconfig.node.json +!/front/vite.config.ts +!/package-lock.json diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..657c977 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,54 @@ +# + +name: Publish + +on: + push: + branches: + - main + - alpha + + workflow_dispatch: + +permissions: + packages: write + +jobs: + build: + name: Publich Docker Image + runs-on: ubuntu-latest + steps: + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Github Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ github.token }} + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: ghcr.io/betagouv/espace-partenaire + tags: | + type=sha,format=long,prefix= + type=ref,event=branch + type=ref,event=tag + flavor: | + latest=${{ github.ref == 'refs/heads/main' }} + + - uses: actions/checkout@v4 + - name: Build image + uses: docker/build-push-action@v3 + with: + build-args: | + SOURCE_COMMIT=${{ github.sha }} + cache-from: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache + cache-to: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache,mode=max + labels: ${{ steps.meta.outputs.labels }} + platforms: linux/amd64 + push: true + tags: ${{ steps.meta.outputs.tags }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cdea04c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,46 @@ +# + +FROM node:lts-buster-slim as base + +ENV TZ Europe/Paris +RUN cp /usr/share/zoneinfo/Europe/Paris /etc/localtime + +RUN --mount=type=cache,target=/root/.npm,sharing=locked \ + npm install -g npm@latest + +WORKDIR /home/node/app + +COPY ./package.json ./ +COPY ./back/package.json ./back/ +COPY ./front/package.json ./front/ +COPY ./front/public/ ./front/public/ + +RUN --mount=type=cache,target=/root/.npm,sharing=locked \ + npm install --omit=dev + +# + +FROM base AS build + +RUN --mount=type=cache,target=/root/.npm,sharing=locked \ + npm install + +COPY ./back/ ./back/ +COPY ./front/ ./front/ + +ENV NODE_ENV=production + +RUN npm run build + +# + +FROM base + +COPY --from=build /home/node/app/back/dist/ ./back/dist/ +COPY --from=build /home/node/app/front/dist/ ./front/dist/ + +# + +USER node +EXPOSE 3000/tcp +ENTRYPOINT [ "npm", "start" ] diff --git a/back/Dockerfile b/back/Dockerfile deleted file mode 100644 index 5fca630..0000000 --- a/back/Dockerfile +++ /dev/null @@ -1,21 +0,0 @@ -FROM node:current-buster-slim - -# cf https://stackoverflow.com/questions/65076483/node-nestjs-watch-always-break -RUN apt-get update; \ - apt-get install --no-install-recommends --no-install-suggests -q -y procps - -# Create app directory -RUN mkdir -p /srv/espace-partenaire/back - -WORKDIR /srv/espace-partenaire/back - -# Install global dependencies -COPY package.json /srv/espace-partenaire/back -RUN npm install -g npm - -# Bundle app source -COPY . /srv/espace-partenaire/back - -ENTRYPOINT [ "/bin/sh", "entrypoint.sh" ] - -CMD ["npm", "run", "start:dev"] diff --git a/back/entrypoint.sh b/back/entrypoint.sh deleted file mode 100644 index 7c25906..0000000 --- a/back/entrypoint.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -npm install --quiet --save-dev - -# this next line runs the docker command -exec "$@" diff --git a/back/package.json b/back/package.json index bc07637..956e472 100644 --- a/back/package.json +++ b/back/package.json @@ -9,7 +9,7 @@ "scalingo-prebuild": "cd ../front && npm install && npm run build && rm -rf ../back/front && cp -r dist ../back/front", "build": "nest build", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", - "start": "nest start", + "start": "node dist/main.js", "start:dev": "nest start --watch", "start:debug": "nest start --debug --watch", "start:prod": "node dist/main", diff --git a/front/public/.gitkeep b/front/public/.gitkeep new file mode 100644 index 0000000..e69de29