From f4b21d4386a120c49d2a3cdff54fce5c7313b7e2 Mon Sep 17 00:00:00 2001 From: Martin Almvik Date: Thu, 21 Nov 2024 13:55:16 +0100 Subject: [PATCH] Konverterer til standalone og med cdn --- .github/workflows/build_deploy_gcp.yml | 24 ++++++++- .github/workflows/build_deploy_mock.yml | 19 ++++++++ .github/workflows/build_dev_image.yml | 19 ++++++++ .github/workflows/build_prod_image.yml | 65 ++++++++++++++++--------- Dockerfile | 46 +++++------------ next.config.mjs | 5 +- 6 files changed, 115 insertions(+), 63 deletions(-) diff --git a/.github/workflows/build_deploy_gcp.yml b/.github/workflows/build_deploy_gcp.yml index f5d58c747..5196e28f0 100644 --- a/.github/workflows/build_deploy_gcp.yml +++ b/.github/workflows/build_deploy_gcp.yml @@ -23,10 +23,30 @@ jobs: permissions: contents: read id-token: write - - steps: + env: + NEXT_PUBLIC_DIGISOS_ENV: ${{ matrix.environment }} + steps: - uses: actions/checkout@v4 + - name: 'Build npm project' + uses: navikt/sosialhjelp-ci/actions/build-npm@main + with: + node-version: 20 + build-less: 'false' + reader-token: ${{ secrets.READER_TOKEN }} + run-test: 'false' + run-orval: 'true' + + - name: 'Upload static files to cdn' + id: upload + uses: nais/deploy/actions/cdn-upload/v2@master + with: + team: teamdigisos + source: ./build/static + destination: "/sosialhjelp-soknad/_next" + project_id: ${{ vars.NAIS_MANAGEMENT_PROJECT_ID }} + identity_provider: ${{ secrets.NAIS_WORKLOAD_IDENTITY_PROVIDER }} + - name: Build and push docker image to GAR uses: nais/docker-build-push@v0 id: docker-build-push diff --git a/.github/workflows/build_deploy_mock.yml b/.github/workflows/build_deploy_mock.yml index 69ec95d2f..969dc0393 100644 --- a/.github/workflows/build_deploy_mock.yml +++ b/.github/workflows/build_deploy_mock.yml @@ -41,6 +41,25 @@ jobs: run: | echo "IMAGE_TAG=${{ env.DOCKER_IMAGE }}:${{ needs.build-image.outputs.docker-tag }}" >> $GITHUB_ENV + - name: 'Build npm project' + uses: navikt/sosialhjelp-ci/actions/build-npm@main + with: + node-version: 20 + build-less: 'false' + reader-token: ${{ secrets.READER_TOKEN }} + run-test: 'false' + run-orval: 'true' + + - name: 'Upload static files to cdn' + id: upload + uses: nais/deploy/actions/cdn-upload/v2@master + with: + team: teamdigisos + source: ./build/static + destination: "/sosialhjelp-soknad/_next" + project_id: ${{ vars.NAIS_MANAGEMENT_PROJECT_ID }} + identity_provider: ${{ secrets.NAIS_WORKLOAD_IDENTITY_PROVIDER }} + - name: Deploy til ${{ env.NEXT_PUBLIC_DIGISOS_ENV }} uses: nais/deploy/actions/deploy@v2 env: diff --git a/.github/workflows/build_dev_image.yml b/.github/workflows/build_dev_image.yml index 1105a76bc..92ed73d7b 100644 --- a/.github/workflows/build_dev_image.yml +++ b/.github/workflows/build_dev_image.yml @@ -16,6 +16,25 @@ jobs: READER_TOKEN: ${{ secrets.READER_TOKEN }} WORKFLOW_TOKEN: ${{ secrets.GITHUB_TOKEN }} steps: + - name: 'Build npm project' + uses: navikt/sosialhjelp-ci/actions/build-npm@main + with: + node-version: 20 + build-less: 'false' + reader-token: ${{ secrets.READER_TOKEN }} + run-test: 'false' + run-orval: 'true' + + - name: 'Upload static files to cdn' + id: upload + uses: nais/deploy/actions/cdn-upload/v2@master + with: + team: teamdigisos + source: ./build/static + destination: "/sosialhjelp-soknad/_next" + project_id: ${{ vars.NAIS_MANAGEMENT_PROJECT_ID }} + identity_provider: ${{ secrets.NAIS_WORKLOAD_IDENTITY_PROVIDER }} + - name: "Build Docker Image" uses: navikt/sosialhjelp-soknad/.github/actions/build-image@master with: diff --git a/.github/workflows/build_prod_image.yml b/.github/workflows/build_prod_image.yml index f0c7e6d21..ebe2f42bc 100644 --- a/.github/workflows/build_prod_image.yml +++ b/.github/workflows/build_prod_image.yml @@ -1,28 +1,45 @@ name: Build prod-sbs image - HUSK MANUELL DEPLOY TIL PROD! on: - workflow_run: - workflows: ["Build code and run test"] - branches: - - master - - 'pre-next' - types: - - completed + workflow_run: + workflows: [ "Build code and run test" ] + branches: + - master + - 'pre-next' + types: + - completed jobs: - build-image: - name: "Build prod-sbs Image" - runs-on: ubuntu-latest - permissions: - contents: write - packages: write - actions: write - env: - NEXT_PUBLIC_DIGISOS_ENV: prod-sbs - DOCKER_IMAGE: ghcr.io/${{ github.repository }}/${{ github.event.repository.name }} - READER_TOKEN: ${{ secrets.READER_TOKEN }} - WORKFLOW_TOKEN: ${{ secrets.GITHUB_TOKEN }} - steps: - - name: "Build Docker Image" - uses: navikt/sosialhjelp-soknad/.github/actions/build-image@master - with: - prefix: ${{ env.NEXT_PUBLIC_DIGISOS_ENV }} + build-image: + name: "Build prod-sbs Image" + runs-on: ubuntu-latest + permissions: + contents: write + packages: write + actions: write + env: + NEXT_PUBLIC_DIGISOS_ENV: prod-sbs + DOCKER_IMAGE: ghcr.io/${{ github.repository }}/${{ github.event.repository.name }} + READER_TOKEN: ${{ secrets.READER_TOKEN }} + WORKFLOW_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: + - name: 'Build npm project' + uses: navikt/sosialhjelp-ci/actions/build-npm@main + with: + node-version: 20 + build-less: 'false' + reader-token: ${{ secrets.READER_TOKEN }} + run-test: 'false' + run-orval: 'true' + - name: 'Upload static files to cdn' + id: upload + uses: nais/deploy/actions/cdn-upload/v2@master + with: + team: teamdigisos + source: ./build/static + destination: "/sosialhjelp-soknad/_next" + project_id: ${{ vars.NAIS_MANAGEMENT_PROJECT_ID }} + identity_provider: ${{ secrets.NAIS_WORKLOAD_IDENTITY_PROVIDER }} + - name: "Build Docker Image" + uses: navikt/sosialhjelp-soknad/.github/actions/build-image@master + with: + prefix: ${{ env.NEXT_PUBLIC_DIGISOS_ENV }} diff --git a/Dockerfile b/Dockerfile index 27d74cd3b..8fecdb012 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,45 +1,21 @@ -# NB: Når Node-versjon endres, bør samme endring også gjøres i: -# - .nvmrc -# - .github/dependabot.yml (fjern versjonspin for docker) -# - packages.json under "engines" (her leter dependabot npm) -# - .ncurc.js (automatiske oppdateringer for node-types) -FROM node:20-alpine AS dependencies - -WORKDIR /app -COPY package.json . -COPY package-lock.json . -COPY .npmrc.dockerbuild .npmrc - -RUN --mount=type=secret,id=NODE_AUTH_TOKEN NODE_AUTH_TOKEN=$(cat /run/secrets/NODE_AUTH_TOKEN) \ - npm ci --prefer-offline --no-audit - -FROM node:20-alpine AS builder +FROM gcr.io/distroless/nodejs20-debian11 AS runtime ARG DIGISOS_ENV + ENV NEXT_PUBLIC_DIGISOS_ENV=${DIGISOS_ENV} -WORKDIR /app -COPY --from=dependencies /app/node_modules/ node_modules/ -COPY . . -RUN npm run orval -RUN npm run build -RUN npm prune --production +WORKDIR /app +COPY package.json /app/ +COPY next-logger.config.js /app/ +COPY .next/standalone /app/ +COPY public /app/public/ -FROM node:20-slim AS release +EXPOSE 8080 -ARG DIGISOS_ENV - -ENV NEXT_PUBLIC_DIGISOS_ENV=${DIGISOS_ENV} +ENV NODE_ENV=production ENV PORT=8080 ENV HOSTNAME=0.0.0.0 -ENV NODE_ENV=production - -WORKDIR /app - -COPY --from=builder --chown=1069:1069 /app/build build -COPY --from=builder /app/node_modules/ node_modules/ -COPY package.json . -COPY . . +#ENV NODE_OPTIONS '-r next-logger' -CMD ["npm", "start"] +CMD ["server.js"] diff --git a/next.config.mjs b/next.config.mjs index 8196660ad..3bfda4d91 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -6,11 +6,12 @@ const nextConfig = { // For å unngå issues med nginx trailingSlash: true, swcMinify: false, + output: "standalone", + assetPrefix: process.env.DIGISOS_ENV === "localhost" ? undefined : "https://cdn.nav.no/teamdigisos/sosialhjelp-soknad", experimental: { instrumentationHook: true, - optimizePackageImports: ["@navikt/ds-react", "@navikt/aksel-icons"] + optimizePackageImports: ["@navikt/ds-react", "@navikt/aksel-icons"], }, - }; export default nextConfig;