CI/CD DevOps pipeline for microservices using VSTS –part 2
In the previous part we have seen how to setup our code repository in VSTS git. In this part we will create some build definitions.
So what exactly is a VSTS build definition?
Team Foundation Build uses build definitions to configure the conditions under which a single solution or a set of solutions in a team project will be built. To conduct a build, you must either create a new build definition or use an existing build definition.
With VSTS microsoft has just provided Team foundation server running in the cloud as a platform.
Now a single build defenition may consist of one or more than one build steps, which are also known as VSTS build tasks. Few of these tasks are already available in VSTS, there are plenty more available acccording to your need at the marketplace, and you can also write your own task and publish it to the marketplace.
For our CI/CD we will be using few inbuild task + few from the marketplace. So lets create our build defenition.
Step 1: Create new build definition
As you can see there are already configured templates for popular build definitions often used by people. In our case we will start with an empty definition and click next.
You will be prompted for a source repository which in our case will be the repository that we have created for this demo and then click on Create.
Note: For continuous integration, i.e queue a new build once a specific branch is updated check the checkbox marked in red in the picture above.
Step 2: Add build steps
Once you have created the build definition, you will get the place to add build steps.
Click on Add build steps to open this dialog.
Task 1: dotnet restore
The 1st task that we will add is .NET core preview (build, test publish using dotnet core command).
We will add this task twice. Once for doing a dotnet restore and once for doing a dotnet publish.
Task 2: dotnet publish
For dotnet publish, select the command as publish from the command drop down and in the argument we will give –o app.
This means we are expecting the output of the publish command inside an app directory. This is exactly the same that we have done manually while containerizing our application.
In the next step will be to run some docker commands. But 1st we will need to download few docker tasks from the marketplace for that.
To go to the VSTS market place click on the browse marketplace option on the top right corner of the portal.
Once you have entered the marketplace search for the docker extension. Choose the docker extension from Microsoft which I have used in this hands off.
Select the extension highlighted in the above picture and click on install. You will be prompted to choose the account for which you want to install the extension.
Select your VSTS account and click continue and if you have the necessary permission click on Confirm.
Once it is installed you will be able to see the build task in the task list.
Task 3: Docker build <image>
Now here we have few things to notice. So the first parameter is Docker Registry connection. You will need to setup the docker registry connection, for the 1st time, by clicking on the Add option in the right.
In the Connection name field give a name to your docker connection,
DockerID will be the docker id that you have created for your self. In my case I have used somakdocker as my docker id.
And password and email of your docker account.
Now in the Docker build step, select the docker connection that you just created form the connection drop down.
For action select build an image from the drop down.
Dockerfile will be the path to our docker file which in our case will be the root directory of our repository.
For the image name give any name to your image.
Note: In the next step we will push our image to our docker hub, thus please maintain the image naming format. <your-docker-id>/<image-name>:<tag if any>
In my case it will be somakdocker/demoservice
Now going to the next task.
Task 4: Docker push <image>
Our next step is to push the docker image to docker hub.
We will use the same docker task, but now we will select the action as push an image.
For more information about the Docker task and the various options available please visit here.
In the picture above we can see one additional step Copy and publish Artifact. We will explain that in the next part. For now its not required for our automated build.
Step 3: Queue build and test
So that’s it. Now save the build definition and lets queue our first build.
For agents we have several options available, we can use hosted agent or our own build agent.
Note: For the docker tasks custom build agent is recommended.
To deploy your own agent(linux) please visit here.
Once you have configured and run your own agent with docker installed in it, you can queue build and select the queue where you have your build agent running.
For more information on creating and queuing builds please visit here.
Once the build succeed, login to your docker hub to check if the image is properly pushed in the repository mentioned in the docker push step.
That completes our automated build i.e creating docker images from the .NET core api, for running in kubernetes we will use VSTS Release management in the next part.