.NET Core Docker Image for SPA Applications

If your looking to get CI/CD up and running with a SPA Application I recommend this setup. This article is based on Aspnet Core 2.1 and up. If you read the docker docs on the 2.1 version of dotnet core you will see they removed nodejs, which breaks a bunch of stuff if your using dotnet core with either Angular2 or Reactjs. I currently use gitlab-runners but with the volumes config to publish my sites.

Notes from Dotnet
Using NodeJS in microsoft/aspnetcore-build, but this is missing from dotnet/core/sdk:2.1. What should I do?
You can either install NodeJS by adding a few lines of code to your Dockerfile that download and extract NodeJS, or you can use the multi-stage feature of Docker and the official NodeJS images.
Sample code to install NodeJS on your own:
# set up node
ENV NODE_VERSION 8.9.4
ENV NODE_DOWNLOAD_SHA 21fb4690e349f82d708ae766def01d7fec1b085ce1f5ab30d9bda8ee126ca8fc
RUN curl -SL "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.gz" --output nodejs.tar.gz \
    && echo "$NODE_DOWNLOAD_SHA nodejs.tar.gz" | sha256sum -c - \
    && tar -xzf "nodejs.tar.gz" -C /usr/local --strip-components=1 \
    && rm nodejs.tar.gz \
    && ln -s /usr/local/bin/node /usr/local/bin/nodejs
If you look at the Gitlab advanced configurations we have a place to add these environments (sweet)!
environmentAppend or overwrite environment variables
In your /etc/gitlab-runner/config.toml

[[runners]]
  name = "yourproject"
  url = "https://git.yoururl.com/"
  token = "yourtoken"
  executor = "docker"
  environment = ["NODE_VERSION=8.9.4", "NODE_DOWNLOAD_SHA=21fb4690e349f82d708ae766def01d7fec1b085ce1f5ab30d9bda8ee126ca8fc"]
  [runners.docker]
    tls_verify = false
    image = "mcr.microsoft.com/dotnet/core/sdk:2.1"
    privileged = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
  [runners.cache]

.gitlab-ci.yml 
stages:  - build  build:  stage: build  script:    - curl -SL "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.gz"        --output nodejs.tar.gz && echo "$NODE_DOWNLOAD_SHA nodejs.tar.gz" | sha256sum -c -        && tar -xzf "nodejs.tar.gz" -C /usr/local --strip-components=1        && rm nodejs.tar.gz        && ln -s /usr/local/bin/node /usr/local/bin/nodejs    - dotnet build  tags:    - aspnetcore yourproject


Update
Don't forget to check the latest version of nodejs and update your packages for the image.
https://nodejs.org/en/download/

References

Popular posts from this blog

Aspetcore: NLog with Postgresql

Mongodb Cheatsheet

Installing Windows on Acer chromebook 15 cb3-532