Skip to content

Instantly share code, notes, and snippets.

@kuznero
Last active March 10, 2016 15:16
Show Gist options
  • Save kuznero/886d1f7a532864d653bd to your computer and use it in GitHub Desktop.
Save kuznero/886d1f7a532864d653bd to your computer and use it in GitHub Desktop.
How to build .Net projects with Docker

How to build .Net projects with Docker

Certain folder structure is assumed

FAKE build script (build.fsx)

// include Fake libs
#r "./packages/FAKE/tools/FakeLib.dll"

open Fake
open Fake.Testing
open System.IO

// Directories
let curDir = Directory.GetCurrentDirectory()
let appDir = "build/app/"
let testDir = "build/test/"
let deployDir = "deploy/"

// Filesets
let appReferences = !! "app/*/*.csproj" ++ "app/*/*.fsproj" ++ "app/*/app/*/*.csproj" ++ "app/*/app/*/*.fsproj"
let testReferences = !! "test/**/*.csproj" ++ "test/**/*.fsproj"

// info
let applicationName = "TOFTradingAPI"
let version = "0.1"  // or retrieve from CI server

// Properties
let buildProps = [("SolutionDir", curDir)]

// Targets
Target "Clean" (fun _ ->
  CleanDirs [appDir; testDir; deployDir]
)

Target "BuildApp" (fun _ ->
  // compile all projects below app/
  MSBuildReleaseExt appDir buildProps "Build" appReferences
  |> Log "AppBuild-Output: "
)

Target "BuildTest" (fun _ ->
  // compile all projects below test/
  MSBuildReleaseExt testDir buildProps "Build" testReferences
  |> Log "TestBuild-Output: "
)

Target "UnitTest" (fun _ ->
  !! (testDir @@ "*Test*.dll")
  |> NUnit3 (fun p -> { p with ResultSpecs = [testDir @@ "UnitTestResults.xml"]; Where = "cat == Unit" })
)

Target "IntegrationTest" (fun _ ->
  !! (testDir @@ "*Test*.dll")
  |> NUnit3 (fun p -> { p with ResultSpecs = [testDir @@ "IntegrationTestResults.xml"]; Where = "cat == Integration" })
)

Target "Deploy" (fun _ ->
  !! (appDir + "/**/*.*")
  -- "*.zip"
  |> Zip appDir (deployDir + applicationName + "." + version + ".zip")
)

// Build order
"Clean"
  ==> "BuildApp"
  ==> "BuildTest"
  ==> "UnitTest"
  ==> "Deploy"
  ==> "IntegrationTest"

// start build
RunTargetOrDefault "UnitTest"

build.sh

#!/bin/bash
if test "$OS" = "Windows_NT"
then
  # use .Net

  .paket/paket.bootstrapper.exe
  exit_code=$?
  if [ $exit_code -ne 0 ]; then
  	exit $exit_code
  fi

  .paket/paket.exe restore
  exit_code=$?
  if [ $exit_code -ne 0 ]; then
  	exit $exit_code
  fi

  packages/FAKE/tools/FAKE.exe $@ --fsiargs build.fsx
else
  # use mono
  mono .paket/paket.bootstrapper.exe
  exit_code=$?
  if [ $exit_code -ne 0 ]; then
  	exit $exit_code
  fi

  mono .paket/paket.exe restore
  exit_code=$?
  if [ $exit_code -ne 0 ]; then
  	exit $exit_code
  fi
  mono packages/FAKE/tools/FAKE.exe $@ --fsiargs -d:MONO build.fsx
fi

build.cmd

@echo off
cls

.paket\paket.bootstrapper.exe
if errorlevel 1 (
  exit /b %errorlevel%
)

.paket\paket.exe restore
if errorlevel 1 (
  exit /b %errorlevel%
)

packages\FAKE\tools\FAKE.exe build.fsx %*

Dockerfile

FROM mono
ADD . /src
VOLUME /src/build/app
WORKDIR /src
CMD ["./build.sh", "Deploy"]

build-docker.sh

#!/bin/bash

IMAGE_NAME="myprojectname"
HTTP_PROXY="http://127.0.0.1:3128"
HTTPS_PROXY="http://127.0.0.1:3128"

RED='\033[1;31m'
YELLOW='\033[1;33m'
GREEN='\033[1;32m'
NC='\033[0m'

docker build -t $IMAGE_NAME .
rc=$?; if [[ $rc != 0 ]]; then echo -e "\n${RED}ERROR${NC}: cannot assemble build image\n"; exit $rc; fi

docker run --rm --volume $(pwd)/deploy:/src/build/app --net host --env http_proxy=$HTTP_PROXY --env https_proxy=$HTTPS_PROXY $IMAGE_NAME
rcbuild=$?

docker rmi $IMAGE_NAME
rc=$?; if [[ $rc != 0 ]]; then echo -e "\n${YELLOW}WARN${NC}: cannot remove build image\n"; fi

if [[ $rcbuild != 0 ]]; then echo -e "\n${RED}ERROR${NC}: build/test failed\n"; exit $rcbuild; fi

echo -e "\n${GREEN}Build succeeded${NC}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment