Configuring AppVeyor is done via a yaml file appveyor.yml at the root of the repository. Then from the site https://www.appveyor.com/, we can sign in with our GitHub credentials and add the repository to the AppVeyor projects. All the settings in the settings tab can be configured in the yaml file.

appveyor.yml settings can be seen in multiple sections:

global environment

build

test

artifact

deploy

The global configuration is where we configure the context of the build,

Here we set the version to the build number, image to the Visual Studio 2017 image to have access to msbuild 15, set the build to not build branches with PR as PR themselves are built with skip_branch_with_pr, skip the build when commits in only on certain files, and lastly we set some global environment variables like the the build configuration to be release.

ps: choco install gitversion.portable -pre -y is used to install Gitversion which we talked about few weeks ago and figure out the current version of the application or library to release. ps: .\fake run build.fsx -t All runs the FAKE build script which we will define in the second part.

Next we can define how we run tests, for this example we don’t have any.

1
2

# 3.test:off

Next we define where the build can find the artifacts, our build script will package the library in a NuGet package .nupkg and place them in a artifacts folder and we tell AppVeyor that it can find them and upload them from that folder.

1
2
3

# 4.artifacts:-path:.\artifacts\**\*.nupkg

Lastly we setup two deployment, one to NuGet and the other one to GitHub releases. Each deployment requires a secure key which can be encrypted using the Encrypt data tool from AppVeyor.

FAKE is a build tool which allows us to define targets and a pipeline fo building and packaging our .NET projects. Here we use it together with AppVeyor to build and package the application and trigger it in the build stage as we seen earlier using the command ps: .\fake run build.fsx -t All.

We start first by installing FAKE by running the following command (assuming that dotnet is already installed):

1

dotnet tool install fake-cli --tool-path .\.fake

This command installs FAKE on a local path under .fake. Then we install the dotnet FAKE template:

1

dotnet new -i"fake-template::*"

And lastly we bootstrap FAKE scripts:

1

dotnet new fake

This creates build.fsx, fake.cmd, fake.sh and files under .fake. Then we need to run the script first before starting to modify it so that all packages are downloaded via paket .\fake.cmd build.
Next we can build the four targets which constitute the build script:

Clean

UpdateBuildVersion

Build

Pack

Clean target

Clean target is used to clean all folders which need to be cleared before generating the files:

UpdateBuildVersion target

Next we deduce the version using GitVersion and update the AppVeyor build version using AppVeyor API. We start first by defining some utility functions to execute a command and return a result, those will be used to execute the gitversion /showvariable command which prints the variable as result.

showVariable will get the current commit by either looking into the environment variable provided by AppVeyor or deducing the current commit by using git rev-parse HEAD and return a function as result which given the variable to show will use gitversion process either on a remote repository for branches or on the local repository for pull requests.

The command executed for gitversion on a remote repostiory is gitversion /showvariable fullSemVer /url [my-repo.git] /b b-[my-branch] /dynamicRepoLocation .\gitversion /c [sha1]. This will download locally the repository under .\gitversion, and create a branch b-branch which will allow gitversion to calculate the version based on that branch. Next we define a getter which when invoked declares a final target which cleans the gitversion folder and activate it when getter is invoked for the first time.

Build

For building the application we use the DotNet.build from FAKE and specify the Version and FileVersion parameter for the dll. Provided that we are using the SDK-Style project files, the version and file version can be set via parameter of the build instead of AssemblyInfo.

Conclusion

Today we saw how we can setup AppVeyor to build and release a dotnet library. We started by setting up the appveyor.yml configuration file to prepare the build agent, build the library, pack it and deploy to NuGet and GitHub. We also saw how to configure the whole FAKE build script with the different targets. Now every time a commit is pushed to any branch on the repository, the AppVeyor build will be triggered and the FAKE build script will run entirely and whenever a tag is pushed, it will trigger a release to NuGet and GitHub release. Hope you liked this post, see you next time!