Dockerfile
The primary entry point for setting up the development environment with Docker is the Dockerfile
. Below is an example of how the Dockerfile is structured:
FROM mcr.microsoft.com/dotnet/sdk:7.0
COPY . .
ENTRYPOINT [ "powershell.exe", "./Build.ps1" ]
FROM mcr.microsoft.com/dotnet/sdk:7.0: This line specifies the base image to use for the container, which is the .NET SDK version 7.0.
COPY . .: This command copies all files from the current directory into the container’s working directory.
ENTRYPOINT: This specifies the command that should be run when the container starts. In this case, it executes
Build.ps1
using PowerShell.
Build Script
The ./Build.ps1
script is crucial for defining how the build process should proceed. Below is a section of this PowerShell script that handles the building and testing of the Intellenum project:
param($verbosity = "minimal", $buildConfig = "Release")
$artifacts = ".\artifacts"
$localPackages = ".\local-global-packages"
function WriteStage([string]$message) {
Write-Host "############################################" -ForegroundColor Cyan
Write-Host "**** " $message -ForegroundColor Cyan
Write-Host "############################################" -ForegroundColor Cyan
Write-Output ""
}
WriteStage("Building $buildConfig version of Intellenum...")
if (Test-Path $artifacts) { Remove-Item $artifacts -Force -Recurse }
New-Item -Path $artifacts -ItemType Directory
New-Item -Path $localPackages -ItemType Directory -ErrorAction SilentlyContinue
WriteStage("Cleaning, restoring, and building $buildConfig version of Intellenum...")
# Cleaning the solution
exec { & dotnet clean Intellenum.sln -c $buildConfig --verbosity $verbosity }
# Restoring packages
exec { & dotnet restore Intellenum.sln --no-cache --verbosity $verbosity }
# Building the solution
exec { & dotnet build Intellenum.sln -c $buildConfig -p Thorough=true --no-restore --verbosity $verbosity }
# Running tests
WriteStage("Running tests...")
exec { & dotnet test tests/AnalyzerTests/AnalyzerTests.csproj -c $buildConfig --no-build -l trx --verbosity $verbosity }
Description of Key Sections
Parameters: The script accepts parameters for verbosity and build configuration, making it flexible for different build needs.
Directory Management: It checks if the artifacts directory exists and deletes it to ensure a clean build environment.
Exec Function: This custom function handles command execution, throwing an error if the command fails.
Build Steps: The script performs cleaning, restoring of packages, building the solution, and running tests, which are logged at each stage.
Running Tests
To facilitate testing within the Docker environment, the build script includes commands for test execution. Here is an excerpt specifically for running tests:
WriteStage("Running analyzer tests...")
exec { & dotnet test tests/AnalyzerTests/AnalyzerTests.csproj -c $buildConfig --no-build -l trx --verbosity $verbosity }
WriteStage("Running unit tests...")
exec { & dotnet test tests/Intellenum.Tests/Intellenum.Tests.csproj -c $buildConfig --no-build -l trx --verbosity $verbosity }
This captures the essence of how tests are run using dotnet test
, ensuring that all necessary parameters are meticulously defined.
Alternative Dockerfile Example
The project may also use another Dockerfile variant, which can be useful for running specific tests. Here’s a simplified view of managing small tests:
FROM mcr.microsoft.com/dotnet/sdk:7.0
WORKDIR /app
ADD ./tests/SmallTests ./smalltests
COPY ./nuget.config ./smalltests
WORKDIR smalltests
RUN dotnet restore
ENTRYPOINT bash
This Dockerfile variant sets up a different working directory for small tests, illustrating flexibility in how tests and configurations are handled in different Docker containers.
Conclusion
The Docker configuration for the Intellenum project is designed for a streamlined development experience, facilitating builds and tests efficiently through organized scripts and containerization. The provided Dockerfiles and PowerShell scripts are tailored to ensure reproducible builds and ease of collaboration among developers.
Quotations derived from source documentation and code:
- Dockerfile
- ./Build.ps1