Search Sreeni's Insights

Filter by content type
cloud November 10, 2015 7 min read

How to set up continuous integration with Visual Studio Online?

Continuous integration with Visual Studio Online explained through vNext builds, package restoration, compilation, tests, and publishing validated artifacts for reliable delivery.

Share:

How to set up continuous integration with Visual Studio Online?

When a team is waiting until the end of a sprint to build and test the application, integration problems are found late. A missing package, a different configuration or a failing unit test can then block several people at once. Continuous integration (CI) is changing this behaviour by building and validating changes soon after they are committed.

Visual Studio Online provides hosted source control, work tracking and build automation. In this article, we are going to see how to create a Visual Studio Online build definition for an ASP.NET MVC or Web API solution using the vNext build system, and how to keep the build useful for the team.

We can automate builds in two common ways:

ApproachProsCons
Visual Studio Online vNext build+ Task-based and easier to compose<br>+ Good fit for a new definition− Needs agent and task configuration<br>− Existing custom activities may need migration
XAML build+ Supports existing team process<br>+ Can retain custom activities− Older and more complex to maintain<br>− Slower to change for a new workflow

TL;DR Summary

  • CI builds every relevant change using a repeatable machine process.
  • A build should restore packages, compile the solution and run tests.
  • Build variables keep environment details outside the source code.
  • A green build is evidence of checks passing, not proof that every business scenario works.
  • Build failures should be visible and owned, otherwise feedback loses its value.
  • Use XAML builds only when an existing project still depends on that process, use vNext for a new definition.

Challenges

Late feedback

If developers integrate only at the end, one failure may have many possible authors and causes. The team will spend time investigating history instead of delivering value. CI gives a shorter feedback loop and lets us take proactive action.

Machine differences

A build which works on one developer machine can fail on another because of a missing SDK or an uncommitted configuration file. The build agent exposes these assumptions. We should make the build steps explicit, otherwise the hosted agent becomes a surprise production-like environment.

False confidence

A successful compile does not mean the application is healthy. A funds search can compile while returning wrong units, and an API can start while its authentication policy is broken. CI must run the tests which give meaningful feedback and the team must still perform an integration pulse check.

What is continuous integration?

Continuous integration is the practice of integrating small changes into a shared source repository and automatically validating them with a build.

In simple terms, every change asks a neutral machine, “Can this source be restored, compiled and tested?” The answer is visible to the team before more changes are layered on top. This is like checking every page while editing a newspaper instead of discovering all printing errors after publication.

Why CI is important?

CI reduces the time between a mistake and its discovery. A failing unit test can be fixed by the developer who made the change while the context is still available. It also creates a common release gate, because the same build definition can produce the artifact used by later deployment.

How to configure a build?

The following diagram depicts a Visual Studio Online commit flowing through restore, build, test and artifact publishing,

Visual Studio Online continuous integration flow
Visual Studio Online continuous integration flow

Option1: Visual Studio Online vNext build

Steps to Implement:

Follow the following instructions to create a CI build in Visual Studio Online.

  • Create a Visual Studio Online project and connect the solution to its Git or Team Foundation Version Control repository.

  • Add a .gitignore or source-control exclusions for local binaries and secrets.

  • Open the Build hub and create a new build definition using the Visual Studio vNext template.

    Build > New build definition > Visual Studio
  • Select the repository and default branch which should trigger the build.

  • Configure the NuGet restore task.

    Solution: <solution file path>
  • Configure the Visual Studio Build task for the Release configuration.

    Solution: <solution file path>
    Platform: <platform>
    Configuration: Release
  • Add a Visual Studio Test task and select the test assemblies.

    Test assembly: **\bin\Release\*test*.dll
  • Publish test results and build artifacts.

    Artifact name: drop
    Path to publish: <artifact output folder>
  • Add a continuous integration trigger for the branch.

    Trigger: Continuous integration
    Branch: <branch name>
  • Save the definition and queue a build using a known-good commit.

  • Just to pulse check, change one test or build input in a branch and confirm that the build starts and reports the result.

    Note: Replace the input place holders with actual values. Do not store production passwords in build variables unless the variable is protected and the access is limited.

  • Configure the build policy so a failed build is visible before the change is merged.

vNext and XAML builds

Visual Studio Online had both the vNext build system and the older XAML build process. Existing teams may have XAML definitions with custom activities, and those should be migrated carefully rather than rewritten during a release. For a new build, vNext is generally simpler to compose from tasks and easier to understand.

The build agent should restore dependencies from a known package source and use the same target framework as the solution. Avoid silently depending on a developer machine’s global packages, because that hides reproducibility problems. Keep the build output versioned or traceable to the source changeset.

Treat a failing build as a team problem, not as background noise. The owner should investigate it quickly, either fixing the change or recording why the build environment is unavailable. A build which is allowed to remain red stops giving feedback, since developers begin to merge changes without trusting the result.

What should the build validate?

Let us take a closer look at one normal build sequence,

  1. Restore NuGet packages from the approved package source.
  2. Compile the solution in Release configuration.
  3. Run unit tests without depending on a developer database.
  4. Publish test results and code coverage where it is configured.
  5. Publish one deployment artifact with the source changeset.

The build should fail when the solution is not compiling or a required test is failing. It should not be hiding a failure by marking the test step as optional, otherwise the green build is not giving any confidence to the team. For integration tests which need SQL Server or another dependency, provision a known test environment and make the dependency visible in the build output.

Build variables and secure values

Use variables for solution path, platform, configuration and artifact folder. Keep passwords and signing keys as protected variables, and do not echo them in a PowerShell task. If a build needs a production value, we should normally inject it only in the deployment step as a secret variable, because every build log is becoming a long-lived record.

For example, a web deployment can use <App Service name> and <resource group> variables while the application settings are configured in the target environment. Replace the input place holders with actual values, but do not put those values into this article or into source control.

Pull request and branch feedback

CI is most useful when the team is seeing the result before merging to the main branch. A developer should be able to run the same restore and test commands locally, then push the change and receive a neutral pulse check from the build agent. When the agent is failing for infrastructure reasons, record it separately, otherwise application failures and agent failures are mixed together.

Must Do: Keep build definitions under review, use a fixed Visual Studio version on the agent where possible and delete old definitions which nobody is using. Don’t: let a broken build remain red for days, because the team will stop trusting it.

Option2: XAML build

Use an XAML build when an existing project has important custom activities or a release process which has not yet been migrated. Keep the definition visible and plan the migration rather than creating new XAML work by default. The important outcome is repeatable restore, build and test feedback, not the name of the build editor.

Wrap up

In this article, we have looked at late feedback, created a vNext build definition, restored packages, compiled an ASP.NET solution, ran tests and published artifacts. I hope this article provided you some insights into continuous integration with Visual Studio Online. In the next article, I will walk you through creating Azure resources with ARM templates.

Related Posts