2018-11-20: How to create your first Pipeline Azure DevOps

Motivation: Following my previous hands-on article, I will this time be doing the guide from Microsoft on how to setup a pipeline.

What guide is that? This one  https://docs.microsoft.com/en-us/azure/devops/pipelines/get-started-yaml?view=vsts

The guide is pretty straight forward, except where it is not.


Status:

ActionDescriptionStatus
Create continuous build for .NetBuild solution automatically when the source GitHub repository gets a new commit.
Create continuous build for Docker ImageCreate an automatic Docker Image when a GitHub repository changes.
Push Docker Image to DockerHub

Create deployDeploy a running Container i Azure
Create continuous deployWhen a Docker Image changes, then deploy this to Azure, run it and expose it to internet

Get the sample code

How to fork it:

  • Click on the "Fork" button

Get your first build

  • Create new project

  • Create new pipeline by navigating to the Pipelines page and then choose New pipeline

  • Select your fork

  • Click "Run"

Nice and clean feedback on every step. Click on a step to see the log. You can also follow this in real time as it builds. Try to explore the "Summary" og "Tests" tabs.

Tests ran and was 100% successful. Nice feedback. To see the output from each test, if any, expand "Outcome" filter to see the tests that have been run.

Get the status badge

https://dev.azure.com/devops-skolen/TestCreateYourFirstPipeline/_apis/build/status/geircode.pipelines-dotnet-core

Ok. Then, that's nice-to-have.


Status so far:

ActionDescriptionStatus
Create continuous build for .NetBuild solution automatically when the source GitHub repository gets a new commit.OK
Create continuous build for Docker ImageCreate an automatic Docker Image when a GitHub repository changes.
Push Docker Image to DockerHub

Create deployDeploy a running Container i Azure
Create continuous deployWhen a Docker Image changes, then deploy this to Azure, run it and expose it to internet

Next steps

Next, I want to build an Docker Image and deploy this to a Container, and the guide points me to https://docs.microsoft.com/en-us/azure/devops/pipelines/languages/docker?view=vsts&tabs=yaml

Build Docker apps with Azure Pipelines

The guide tells me to:

Define two variables in your build pipeline in the web UI.

  • dockerId: Your Docker Id for DockerHub or the admin user name for the Azure Container Registry
  • dockerPassword: Password for DockerHub or admin password for Azure Container Registry

But how do I "Define two variables in your build pipeline in the web UI"?

The Dockerfile for building and pushing the image to DockerHub is here: https://github.com/geircode/pipelines-dotnet-core/blob/master/azure-pipelines.docker.yml

# Build Docker image for this app using Azure Pipelines
# http://docs.microsoft.com/azure/devops/pipelines/languages/docker?view=vsts
pool:
  vmImage: 'Ubuntu 16.04'

variables:
  buildConfiguration: 'Release'
  imageName: 'dotnetcore:$(Build.BuildId)'
  # define two more variables dockerId and dockerPassword in the build pipeline in UI

steps:
- script: |
    dotnet build --configuration $(buildConfiguration)
    dotnet test dotnetcore-tests --configuration $(buildConfiguration) --logger trx
    dotnet publish --configuration $(buildConfiguration) --output out
    docker build -f Dockerfile -t $(dockerId)/$(imageName) .
    docker login -u $(dockerId) -p $pswd
    docker push $(dockerId)/$(imageName)
  env:
    pswd: $(dockerPassword)

- task: PublishTestResults@2
  inputs:
    testRunner: VSTest
    testResultsFiles: '**/*.trx'

In this file I see the usage of "dockerId" and "$pswd".

  • Go to your Pipelines=>Build and click "Edit"

  • Change the 'yml' file to "azure-pipelines.docker.yml". This pipeline will build the .NET CORE solution inside a Container and create a Docker Image with this build.

  • Add the variables "dockerId" and "dockerPassword" with values in the "Variables" tab. "dockerId" is your usual login user, not a number or a Guid.
  • Click on the 'padlock' to make the values secret

  • Click on "Save & Queue" to see what happens. Hopefully it should build and push to DockerHub.

Looking into the logs:

Checking DockerHub/Cloud: https://hub.docker.com/r/geircode/dotnetcore/

Yep, it's there and it's automatically set to "public".

  • Click on "Tags".

Here we can see that the Azure pipeline has created several Docker Images and tagged them with a buildnumber. This makes less sense when doing development, so I am changing the Azure pipeline to create at least one 'latest', which is closer to Best Pratice in the Docker world-of-things.

# Build Docker image for this app using Azure Pipelines
# http://docs.microsoft.com/azure/devops/pipelines/languages/docker?view=vsts
pool:
  vmImage: 'Ubuntu 16.04'

variables:
  buildConfiguration: 'Release'
  imageName: 'dotnetcore:latest'
  # define two more variables dockerId and dockerPassword in the build pipeline in UI

steps:
- script: |
    dotnet build --configuration $(buildConfiguration)
    dotnet test dotnetcore-tests --configuration $(buildConfiguration) --logger trx
    dotnet publish --configuration $(buildConfiguration) --output out
    docker build -f Dockerfile -t $(dockerId)/$(imageName) .
    docker login -u $(dockerId) -p $pswd
    docker push $(dockerId)/$(imageName)
  env:
    pswd: $(dockerPassword)

- task: PublishTestResults@2
  inputs:
    testRunner: VSTest
    testResultsFiles: '**/*.trx'

Here I only changed the imageName to be more developer-friendly when working with Containers.

  • Save changes and commit.

Hmm, hopefully this will build the pipeline automatically when I comitted the pipeline config to GitHub?

Yep. "Pipeline-as-code" is pretty nice. This way anyone with access to the pipeline-config can change it without the need to use the Azure website.

And the Container Image is pushed to Docker Hub.


Status so far:

ActionDescriptionStatus
Create continuous build for .NetBuild solution automatically when the source GitHub repository gets a new commit.OK
Create continuous build for Docker ImageCreate an automatic Docker Image when a GitHub repository changes.OK
Push Docker Image to DockerHub
OK
Create deployDeploy a running Container i Azure
Create continuous deployWhen a Docker Image changes, then deploy this to Azure, run it and expose it to internet


Next I need create a place for this container image to run and I follow this guide:

https://docs.microsoft.com/en-us/azure/devops/pipelines/apps/cd/deploy-docker-webapp?view=vsts

Deploy to an Azure Web App for Containers

This will create a Web App, using a VM as Docker Host and run the Docker Image.

Create an Azure Web App to host a container

  • Create a "Web App for Containers"

The "App Service plan" defaults to a way too expensive infrastructure for just testing purposes, so you should change it first.

  • Click on "App Service plan"

  • Change the "Pricing tier" to use "B1"


  • Select "Dev/Test" and "B1"

  • Name the new Service Plan i.e. "ServicePlan-NorthEurope-B1" and select it.

  • Click on "Configure container"

  • Select "Docker Hub" and enter your public Docker image repository i.e. "geircode/dotnetcore" and click "Apply"

  • Click on "Create"





Status:

ActionDescriptionStatus
Create continuous build for .NetBuild solution automatically when the source GitHub repository gets a new commit.OK
Create continuous build for Docker ImageCreate an automatic Docker Image when a GitHub repository changes.OK
Push Docker Image to DockerHub
OK
Create deployDeploy a running Container i AzureOK
Create continuous deployWhen a Docker Image changes, then deploy this to Azure, run it and expose it to internet

Create continuous deploy

  • Navigate to "Container settings"

  • Copy the Webhook URL to clipboard
  • Navigate to your Docker Hub repository and go to "Webhooks"


  • Paste the webhook url and save


Test deploy by checking in new code.


Status: Success!

ActionDescriptionStatus
Create continuous build for .NetBuild solution automatically when the source GitHub repository gets a new commit.OK
Create continuous build for Docker ImageCreate an automatic Docker Image when a GitHub repository changes.OK
Push Docker Image to DockerHub
OK
Create deployDeploy a running Container i AzureOK
Create continuous deployWhen a Docker Image changes, then deploy this to Azure, run it and expose it to internetOK