Last active
December 20, 2018 20:19
-
-
Save dasjestyr/b0c8bc5577b314041cc8f7d8132f4c34 to your computer and use it in GitHub Desktop.
[netcore build scripts] Build scripts used to play well with build servers, primarily Bitbucket pipelines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/pwsh | |
Param( | |
[parameter(Mandatory, HelpMessage = "The name of the project that will be dockerized. Dockerfile must be in that directory")] | |
[Alias("Project")][String] $project_name, | |
[parameter(Mandatory, HelpMessage = "The image name that will be used to tag the image.")] | |
[Alias("Name")][String] $image_name, | |
[parameter(Mandatory, HelpMessage = "The tag that will be used to tag the image.")] | |
[Alias("Tag")][String] $image_tag, | |
[parameter(Mandatory, HelpMessage = "MyGet pre-auth'd URL")] | |
[Alias("MyGetSource")][String] $myget_source, | |
[parameter(HelpMessage = "Flag for whether or not this tag should also be tagged as latest.")] | |
[Alias("Latest")][Switch] $tag_latest, | |
[parameter(HelpMessage = "Flag for whether or not the images should also be pushed.")] | |
[Alias("Push")][Switch] $push_images, | |
[parameter(HelpMessage = "Flag to disable the publish step.")] | |
[Alias("NoBuild")][Switch] $skip_publish, | |
[parameter(HelpMessage = "Build configuration. Default 'Release'")] | |
[Alias("Config")][String] $build_config = "Release", | |
[parameter(HelpMessage = "Publish output directory. Default 'out'")] | |
[Alias("Output")][String] $output_directory = "out" | |
) | |
function attempt() { | |
try { | |
$cmd, $params = $args | |
$params = @($params) | |
$output = & $cmd @params 2>&1 | |
if (-Not $?) { | |
throw $output | |
} | |
$output | |
} catch { | |
Write-Output "COMMAND FAILED: $cmd $params" | |
exit 1 | |
} | |
} | |
function publish_project() { | |
attempt dotnet restore src -s $myget_source -s https://api.nuget.org/v3/index.json | |
attempt dotnet publish src/$project_name/$project_name.csproj -o $output_directory -c $build_config | |
} | |
function build_image() { | |
Write-Output "Building docker image called '${image_name}:${image_tag}' using binaries located in the directory '$output_directory'!" | |
attempt docker build -t "${image_name}:${image_tag}" --build-arg bin_dir=$output_directory src/$project_name/ | |
if($tag_latest) { | |
Write-Output "Tagging as latest..." | |
attempt docker tag "${image_name}:${image_tag}" "${image_name}:latest" | |
} | |
} | |
$ErrorActionPreference="Stop" | |
if(-Not $skip_publish) { | |
publish_project | |
} | |
build_image | |
if($push_images) { | |
attempt docker push "${image_name}:${image_tag}" | |
if($tag_latest) { | |
attempt docker push "${image_name}:latest" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/pwsh | |
Param( | |
[parameter(Mandatory)][Alias("Solution")][String] $project_name, | |
[parameter(Mandatory)][Alias("MyGetSource")][String] $myget_source, | |
[parameter()][Alias("BuildConfiguration")][String] $build_configuration = "Release", | |
[parameter()][Alias("SonarUrl")][String] $sonar_url, | |
[parameter()][Alias("SonarKey")][String] $sonar_key, | |
[parameter()][Alias("Test")][Switch] $run_tests, | |
[parameter()][Alias("Analyze")][Switch] $run_analysis | |
) | |
function attempt() { | |
try { | |
$cmd, $params = $args | |
$params = @($params) | |
$output = & $cmd @params 2>&1 | |
if (-Not $?) { | |
throw $output | |
} | |
$output | |
} catch { | |
Write-Output "COMMAND FAILED: $cmd $params" | |
exit 1 | |
} | |
} | |
function validate_parameters { | |
if($run_analysis) { | |
if([string]::IsNullOrEmpty($sonar_url) -or [string]::IsNullOrEmpty($sonar_key)) { | |
throw "SonarUrl and SonarKey are required to run static code analysis!" | |
} | |
} | |
} | |
function run_unit_tests { | |
dotnet tool install -g trx2junit | |
$testFailed = $false | |
New-Item -ItemType Directory -Force -Path .\test-results | |
foreach($testProject in (Get-ChildItem -Path src -File -Recurse -Filter "*.Tests.csproj" )) { | |
dotnet test $testProject.FullName ` | |
/p:CollectCoverage=true ` | |
/p:CoverletOutputFormat=opencover ` | |
/p:CoverletOutput=./test-results/coverage ` | |
--logger:trx -r ./test-results | |
if($LASTEXITCODE -ne 0) { | |
# record the fail but allow this to continue so that all tests are run | |
$testFailed = $true | |
} | |
Get-ChildItem -Path "$($testProject.Directory.FullName)\test-results" -Recurse -File -Filter "*.trx" | ` | |
Move-Item -Force -Destination .\test-results ` | |
} | |
foreach($testOutput in (Get-ChildItem -Path .\test-results -File -Filter "*.trx")) { | |
& "${toolsPath}trx2junit" $testOutput.FullName | |
Remove-Item $testOutput.FullName | |
} | |
if($testFailed) { | |
exit 1 | |
} | |
} | |
$ErrorActionPreference="Stop" | |
$toolsPath = if($IsWindows -or ($Env:OS -and $Env:OS.Contains("Windows"))) { "$env:USERPROFILE\.dotnet\tools\" } else { "$HOME/.dotnet/tools/" } | |
attempt validate_parameters | |
attempt dotnet restore src -s $myget_source -s https://api.nuget.org/v3/index.json | |
if($run_analysis) { | |
dotnet tool install -g dotnet-sonarscanner | |
& "${toolsPath}dotnet-sonarscanner" begin ` | |
/k:"$project_name" ` | |
/d:sonar.host.url="$sonar_url" ` | |
/d:sonar.login="$sonar_key" ` | |
/d:sonar.cs.opencover.reportsPaths="./test-results/*.opencover.xml" | |
if(-Not $?) { exit 1 } | |
} | |
attempt dotnet build src/${project_name}.sln -c $build_configuration | |
if($run_tests) { | |
attempt run_unit_tests | |
} | |
if($run_analysis) { | |
& "${toolsPath}dotnet-sonarscanner" end /d:sonar.login="$sonar_key" | |
if(-Not $?) { exit 1 } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/pwsh | |
Param( | |
[parameter( HelpMessage = "Names of projects to be packed.")] | |
[Alias("Projects")][String[]] $pack_projects, | |
[parameter(HelpMessage = "The build number that will be used to version the packages.")] | |
[Alias("BuildNumber")][Int32] $build_number, | |
[parameter(Mandatory, HelpMessage = "The MyGet pre-auth'd URL")] | |
[Alias("MyGetSource")][String] $myget_source, | |
[parameter(HelpMessage = "If the Symbols server URL is included, this script will push symbols to it.")] | |
[Alias("SymbolsSource")][String] $myget_symbols, | |
[parameter(HelpMessage = "The build configuration. Default 'Release'")] | |
[Alias("BuildConfiguration")][String] $build_config = "Release", | |
[parameter(HelpMessage = "Flag for whether or not the package should be pushed after packing.")] | |
[Alias("Push")][Switch] $push_package, | |
[parameter(HelpMessage = "If set, script will delete all nupkg files before beginning.")] | |
[Alias("ClearPackages")][Switch] $clear_packages, | |
[parameter(HelpMessage = "If set, the build action will be skipped")] | |
[Alias("NoBuild")][Switch] $no_build | |
) | |
function attempt() { | |
try { | |
$cmd, $params = $args | |
$params = @($params) | |
$output = & $cmd @params 2>&1 | |
if (-Not $?) { | |
throw $output | |
} | |
$output | |
} catch { | |
Write-Output "COMMAND FAILED: $cmd $params" | |
exit 1 | |
} | |
} | |
function build_project() { | |
attempt dotnet build src -c $build_config | |
} | |
function pack_projects() { | |
foreach($project in $pack_projects){ | |
if($myget_symbols) { | |
dotnet pack src/$project/$project.csproj -c $build_config /p:BuildNumber=$build_number --include-symbols | |
} else { | |
dotnet pack src/$project/${project}.csproj -c $build_config /p:BuildNumber=$build_number | |
} | |
} | |
} | |
$ErrorActionPreference="Stop" | |
if(-Not $no_build) { | |
build_project | |
} | |
if($clear_packages) { | |
foreach($package in (Get-ChildItem -Path . -File -Recurse -Filter "*.nupkg")) { | |
Remove-Item -Path $package.FullName | |
} | |
} | |
# run this explicitly because sometimes build wont run the pack even if the option is set in the csproj | |
pack_projects | |
if($push_package) { | |
foreach($package in (Get-ChildItem -Path . -File -Recurse -Filter "*.nupkg")) { | |
Write-Output $package.FullName | |
if($myget_symbols) { | |
attempt dotnet nuget push $package.FullName -s $myget_source -ss $myget_symbols | |
} else { | |
attempt dotnet nuget push $package.FullName -s $myget_source | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/pwsh | |
Param( | |
[parameter(Mandatory, HelpMessage = "Name of the project that will be built (no extension).")] | |
[Alias("Project")][String] $project_name, | |
[parameter(Mandatory, HelpMessage = "The MyGet pre-auth'd URL")] | |
[Alias("MyGetSource")][String] $myget_source, | |
[parameter(HelpMessage = "Directory to which the binaries will be published. Default 'out'")] | |
[Alias("Output")][String] $output_directory = "out", | |
[parameter(HelpMessage = "Build Configuration. Default 'Release'")] | |
[Alias("Configuration")][String] $build_config = "Release" | |
) | |
function attempt() { | |
try { | |
$cmd, $params = $args | |
$params = @($params) | |
$output = & $cmd @params 2>&1 | |
if (-Not $?) { | |
throw $output | |
} | |
$output | |
} catch { | |
Write-Output "COMMAND FAILED: $cmd $params" | |
exit 1 | |
} | |
} | |
function publish_binaries() { | |
attempt dotnet restore src -s $myget_source -s https://api.nuget.org/v3/index.json | |
attempt dotnet publish src/$project_name/$project_name.csproj -o $output_directory -c $build_config | |
} | |
$ErrorActionPreference="Stop" | |
attempt publish_binaries |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment