Building ASP.NET Core app with Buddy and Docker
In my last article, I presented how to run Aurelia unit tests inside Bitbucket pipeline. What’s quite funny (or may be scary for some of you) is that few hours after publishing, Facebook presented to me an add with „great CI tool” called Buddy. Without much thought, I navigated to the project’s site and I felt in love immediately. Why? There’s one reason – it’s dead simple.
Creating ASP.NET Core app
Before building the pipeline using Buddy we need two things: app and idea what we’d like to do during the whole build process. Let’s start with the second one. Our pipeline will process three steps:
- Restore dotnet dependencies and build project
- Build Docker image and publish it to the DockerHub
- Send notification to the Slack
Having this done, we can move forward to the app. I created and repository on Bitbucket and cloned it in my computer. Then, I ran the following command in order to create a default ASP.NET Core WebAPI app:
dotnet new webapi
Because our app will be published to DockerHub wee need to add a Dockerfile to the project. In my case, it was called DockerFile.develop:
FROM microsoft/dotnet:latest COPY . /app WORKDIR /app RUN dotnet restore RUN dotnet build EXPOSE 5000/tcp ENTRYPOINT dotnet run
As you can see, there’s nothing special here. What it does is:
- Pulls the latest image of .NET Core
- Copies the project directory to the app folder
- Restores dependencies
- Builds project
- Exposes Kestrel’s 5000 port
- Sets entry point
Having that, I pushed the project to the Bitbucket on master branch.
Creating the project inside Buddy
If you’re having some project pushed to the repository as I did, you can start defining the pipeline. Go to the official Buddy site and create an account or if it suits you more sign up using GitHub or Bitbucket. Then you need to create a project for the app. There are few different Git providers available:
Since I had pushed my app to the Bitbucket I chose it as a provider and the selected my project from the list. After few seconds your project should be created and ready for creating a pipeline.
Step 1. Building ASP.NET Core app
Now, here’s an interesting thing. The pipeline can be created either using GUI on the website or using YAML file included to your project (so very similar approach to the Bitbucket). I stuck to the GUI so it’d be way simpler and more intuitive:
As presented above, the very first thing I had to specify was the name for my new pipeline, trigger mode, and branch on which the whole thing will run. After saving my changes, I was asked to select the initial step. Because I wanted to build an app at first, I selected .NET Core icon:
That’s the thing that impressed me the most. Buddy uses Docker by default, so there’s no need to enable it. As you can see, because I chose .NET Core, a proper Docker image was selected and basics commands were generated automatically to build and app. I clicked „Add this action” button to submit my changes.
Step 2. Publishing Docker image
After creating my first step, I immediately added the second one, simply by clicking „+” icon below. Then I navigated to the „DevOps” section and selected „Docker Image”. All I had to do was specify the Dockerfile in my project and enter credentials/repository name in DockerHub:
Having this done I saved my changes.
Step 3. Send notification to Slack
Last but not least – sending a notification to the Slack. Once again, I added a step by clicking „+” icon and I found Slack. After authorizing an app, I left default message and selected target channel:
The whole pipeline looked as follows:
Testing the pipeline
In order to test the pipeline, I did some changes in my application and pushed it to the Bitbucket. Almost immediately Buddy triggered the pipeline:
Here are the details:
And Slack notification:
As you see, this whole DevOps thing doesn’t have to be that complicated. Although, if you’re not a big „GUI” enthusiast, I’ve got good news for you. Even if you created a pipeline using this approach you can still add YAML file to the project. To do that, go to the „Pipelines” tab and navigate to the sidebar where you can find „YAML Configuration” section. Then flick the switch and copy generated YAML file (based on your GUI pipe) to your project:
Push your changes and you’re done. Simple is that. One more thing here – the YAML file will contain your DockerHub credentials as a plain text. If you don’t want to keep them in the file, create Environment variables (can be also found in the sidebar) and use them inside the YAML. Of course, the above article presented just a small fraction of Buddy’s capabilities. If you want to explore more, go try it yourself!