Google Cloud Builds - First Attempts To Build .NET Core and Angular

· Read in about 3 min · (550 words) ·

Experience with setting up first cloud build for .NET Core & Angular.

Intro

  • Read manuals carefully - you can test your build locally using Google Cloud SDK but it doesn’t work on Windows. So if you want to give it a try setup a Linux machine for it.
  • I strongly recommend to setup Google Cloud SDK on your Linux machine manually and not thru a package manager - this may save you a lot of time since gcloud components install will work for you and thus examples can be followed easily.
  • Rule of thumb - it is simple! Really - it is simple!
    • Individual cloud builders are just docker images which have the entrypoint configured to the given tool - you can use them directly assuming that you simply specify entrypoint in your YAML (this is pretty useful for initial playing with the builds):
steps:
- name: gcr.io/$PROJECT_ID/ng:v7
  dir: './TestApp/ClientApp'
  entrypoint: 'npm'
  args: [
        'ci', '--unsafe-perm'
        ]

Angular & .NET Core build task image

  • Create a git repository in GCP or just fork this GitHub repository
  • In Google cloud console setup a build hook pointing to cloudbuild.yaml in the repository
  • Kick-off the build
  • This should prepare for you an image in your project container registry which can be used to build Angular 7 & .NET Core Application

  • Dockerfile

FROM microsoft/dotnet:sdk

CMD [ '/bin/sh', '-c' ]

RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN apt-get install -y nodejs

RUN npm install -g @angular/cli@7.2.2 --unsafe-perms

ENTRYPOINT ["dotnet"]
  • cloudbuild.yaml
    
steps:
- name: gcr.io/cloud-builders/docker
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/cloudbuilderangulardotnetcore:current', '-f', 'Dockerfile', '.' ]

images:
  - 'gcr.io/$PROJECT_ID/cloudbuilderangulardotnetcore:current'

Building ASP.NET Core & Angular image from Google Cloud Builds

Use the standard Dockerfile

  • This is easiest approach
  • Nice thing is that it can be used even locally and on Windows
  • It is simply docker build command - due to its caching mechanism & layering it is pretty efficient
  • The disadvantage is that output artifacts are just docker images
  • The Dockerfile can be simply combined with cloud build YAML so the resulting image is pushed to container registry:
  • Dockerfile
FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
  • cloudbuild.yaml
steps:
- name: gcr.io/cloud-builders/docker
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/testweb:current', '-f', 'Dockerfile', '.' ]

images:
  - 'gcr.io/$PROJECT_ID/testweb:current'

Use cloud build with steps

  • This fully leverages cloud build features
  • The main difference is that while this still uses Docker for builds it leverages docker run command
  • The source code is basically mounted to created container thru a volume located in /workspace directory which can be used to exchange content between individual steps
  • The cloud build directly supports grabbing the artifacts from /workspace so the build output is not necessarily only docker image
steps:
- name: gcr.io/cloud-builders/docker
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/testweb:current', '-f', 'Dockerfile', '.' ]

images:
  - 'gcr.io/$PROJECT_ID/testweb:current'
  • Split the build into individual steps called explicitly:
steps:

...
# Restore packages
- name: gcr.io/$PROJECT_ID/cloudbuilderangulardotnetcore:current
  args: [
          'restore'
        ]
# Build the project
- name: gcr.io/$PROJECT_ID/cloudbuilderangulardotnetcore:current
  args: [
          'build',
          '-c', 'Release',
          '--no-restore'
        ]

# Run tests
- name: gcr.io/$PROJECT_ID/cloudbuilderangulardotnetcore:current
  args: [
          'test',
          '-c', 'Release',
          '--no-restore'
        ]

# Publish build result
- name: gcr.io/$PROJECT_ID/cloudbuilderangulardotnetcore:current
  args: [
          'publish',
          '-c', 'Release',
          '--no-restore',
          '-o', '/workspace/publish'
        ]
....

Links