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:
Action | Description | Status |
---|---|---|
Create continuous build for .Net | Build solution automatically when the source GitHub repository gets a new commit. | |
Create continuous build for Docker Image | Create an automatic Docker Image when a GitHub repository changes. | |
Push Docker Image to DockerHub | ||
Create deploy | Deploy a running Container i Azure | |
Create continuous deploy | When a Docker Image changes, then deploy this to Azure, run it and expose it to internet |
Get the sample code
- Get your sample code from https://github.com/MicrosoftDocs/pipelines-dotnet-core
- Copy or Fork it to your own GirHub account
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
Ok. Then, that's nice-to-have.
Status so far:
Action | Description | Status |
---|---|---|
Create continuous build for .Net | Build solution automatically when the source GitHub repository gets a new commit. | OK |
Create continuous build for Docker Image | Create an automatic Docker Image when a GitHub repository changes. | |
Push Docker Image to DockerHub | ||
Create deploy | Deploy a running Container i Azure | |
Create continuous deploy | When 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.
|
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:
Action | Description | Status |
---|---|---|
Create continuous build for .Net | Build solution automatically when the source GitHub repository gets a new commit. | OK |
Create continuous build for Docker Image | Create an automatic Docker Image when a GitHub repository changes. | OK |
Push Docker Image to DockerHub | OK | |
Create deploy | Deploy a running Container i Azure | |
Create continuous deploy | When 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:
Action | Description | Status |
---|---|---|
Create continuous build for .Net | Build solution automatically when the source GitHub repository gets a new commit. | OK |
Create continuous build for Docker Image | Create an automatic Docker Image when a GitHub repository changes. | OK |
Push Docker Image to DockerHub | OK | |
Create deploy | Deploy a running Container i Azure | OK |
Create continuous deploy | When 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!
Action | Description | Status |
---|---|---|
Create continuous build for .Net | Build solution automatically when the source GitHub repository gets a new commit. | OK |
Create continuous build for Docker Image | Create an automatic Docker Image when a GitHub repository changes. | OK |
Push Docker Image to DockerHub | OK | |
Create deploy | Deploy a running Container i Azure | OK |
Create continuous deploy | When a Docker Image changes, then deploy this to Azure, run it and expose it to internet | OK |