Skip to content

Commit

Permalink
Merge branch 'EarthlyBuild' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaykul committed Nov 12, 2023
2 parents 5d421d5 + 9ac9168 commit 838a666
Show file tree
Hide file tree
Showing 18 changed files with 452 additions and 71 deletions.
6 changes: 6 additions & 0 deletions .earthlyignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ignore output (because we put stuff there)
/output/*
assemblies/
# ignore binary output files and folders
bin/
obj/
41 changes: 41 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
env:
FORCE_COLOR: 1
steps:
- uses: earthly/actions-setup@v1
with:
# version: v0.7.21
github-token: ${{ secrets.GITHUB_TOKEN }}

- uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0

- name: earthly +all
run: earthly --strict +all

- uses: actions/upload-artifact@v3
with:
name: Pansies
path: Modules/Pansies

- uses: actions/upload-artifact@v3
with:
name: TestResults
path: Modules/Pansies-TestResults

- uses: actions/upload-artifact@v3
with:
name: Packages
path: Modules/Pansies-Packages
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# ignore packages (this is for nuget stuff)
packages/
# ignore output (because we put stuff there)
Modules/
output/

# Ignore version number folders (these are our intermediate "output" directories for testing)
Expand All @@ -10,6 +9,8 @@ output/
bin/
obj/
lib/
assemblies/

# ignore visual studio artifacts
*.suo
/.vs/
Expand Down
40 changes: 40 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
},
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Launch (current file)",
"script": "${file}",
},
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Launch (current test file)",
"script": "Invoke-Pester",
"args": [
"-Path", "${file}",
"-PesterOption", "@{ IncludeVSCodeMarker = $True }"
],
"createTemporaryIntegratedConsole": true
},
{
"type": "PowerShell",
"request": "attach",
"name": "PowerShell Attach to Host Process",
"processId": "${command:PickPSHostProcess}",
"runspaceId": 1
},
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Interactive Session",
"cwd": "${workspaceRoot}"
}
]
}
17 changes: 17 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"files.encoding": "utf8",
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"editor.tabSize": 4,
"editor.insertSpaces": true,
"powershell.codeFormatting.preset": "OTBS",
"powershell.codeFormatting.ignoreOneLineBlock": false,
"powershell.codeFormatting.useCorrectCasing": true,
"powershell.codeFormatting.alignPropertyValuePairs": true,
"powershell.codeFormatting.pipelineIndentationStyle": "IncreaseIndentationForFirstPipeline",
"powershell.scriptAnalysis.settingsPath": "ScriptAnalyzerSettings.psd1",
"files.associations": {
"*.ps1xml": "xml",
".earthlyignore": "ignore"
}
}
50 changes: 50 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"group": {
"kind": "build",
"isDefault": true
},
"type": "shell",
"command": "Invoke-Build",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
}
},
{
"label": "test",
"group": {
"kind": "test",
"isDefault": true
},
"type": "shell",
"options": {
"cwd": "${workspaceFolder}",
"env": {
"PSModulePath": "${workspaceFolder}\\Output;${env:PSModulePath}"
}
},
"command": "Invoke-Build",
"args": [
"Test"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
}
]
}
70 changes: 70 additions & 0 deletions Build.build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<#
.SYNOPSIS
./project.build.ps1
.EXAMPLE
Invoke-Build
.NOTES
0.5.0 - Parameterize
Add parameters to this script to control the build
#>
[CmdletBinding()]
param(
# dotnet build configuration parameter (Debug or Release)
[ValidateSet('Debug', 'Release')]
[string]$Configuration = 'Release',

# Add the clean task before the default build
[switch]$Clean,

# Collect code coverage when tests are run
[switch]$CollectCoverage,

# Which projects to build
[Alias("Projects")]
$dotnetProjects = @(
"Pansies"
),

# Which projects are test projects
[Alias("TestProjects")]
$dotnetTestProjects = @(),

# Further options to pass to dotnet
[Alias("Options")]
$dotnetOptions = @{
"-verbosity" = "minimal"
# "-runtime" = "linux-x64"
}
)
$InformationPreference = "Continue"
$ErrorView = 'DetailedView'

# The name of the module to publish
$script:PSModuleName = "TerminalBlocks"
# Use Env because Earthly can override it
$Env:OUTPUT_ROOT ??= Join-Path $BuildRoot Modules

$Tasks = "Tasks", "../Tasks", "../../Tasks" | Convert-Path -ErrorAction Ignore | Select-Object -First 1
Write-Information "$($PSStyle.Foreground.BrightCyan)Found shared tasks in $Tasks" -Tag "InvokeBuild"
## Self-contained build script - can be invoked directly or via Invoke-Build
if ($MyInvocation.ScriptName -notlike '*Invoke-Build.ps1') {
& "$Tasks/_Bootstrap.ps1"

Invoke-Build -File $MyInvocation.MyCommand.Path @PSBoundParameters -Result Result

if ($Result.Error) {
$Error[-1].ScriptStackTrace | Out-String
exit 1
}
exit 0
}

## The first task defined is the default task. Put the right values for your project type here...
if ($dotnetProjects -and $Clean) {
Add-BuildTask CleanBuild Clean, ($Task ?? "Test")
} elseif ($Clean) {
Add-BuildTask CleanBuild Clean, ($Task ?? "Test")
}

## Initialize the build variables, and import shared tasks, including DotNet tasks
. "$Tasks/_Initialize.ps1"
14 changes: 7 additions & 7 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ param(
[Alias("ModuleVersion","Version")]
[string]$SemVer
)
$ErrorActionPreference = "Stop"
Push-Location $PSScriptRoot -StackName BuildTestStack

if (!$SemVer -and (Get-Command gitversion -ErrorAction Ignore)) {
$SemVer = gitversion -showvariable nugetversion
}

try {
$ErrorActionPreference = "Stop"
Write-Host "## Calling Build-Module" -ForegroundColor Cyan

$Module = Build-Module -Passthru -SemVer $SemVer
$Folder = Split-Path $Module.Path

if (!$SkipBinaryBuild) {
Write-Host "## Compiling Pansies binary module" -ForegroundColor Cyan
# dotnet restore
# dotnet build -c $Configuration -o "$($folder)\lib" | Write-Host -ForegroundColor DarkGray
dotnet publish -c $Configuration -o "$($Folder)\lib" | Write-Host -ForegroundColor DarkGray
dotnet publish -c $Configuration -o "$($Folder)/lib" | Write-Host -ForegroundColor DarkGray
# We don't need to ship any of the System DLLs because they're all in PowerShell
Get-ChildItem $Folder -Filter System.* -Recurse | Remove-Item
}
Write-Host "## Calling Build-Module" -ForegroundColor Cyan

$null = $PSBoundParameters.Remove("Configuration")
$Module = Build-Module @PSBoundParameters -Passthru

Write-Host "## Compiling Documentation" -ForegroundColor Cyan
$Folder = Split-Path $Module.Path

Remove-Item "$($folder)\en-US" -Force -Recurse -ErrorAction SilentlyContinue
$null = New-ExternalHelp -Path ".\Docs" -OutputPath "$($folder)\en-US"
Expand Down
64 changes: 64 additions & 0 deletions Earthfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
VERSION 0.7
IMPORT github.com/poshcode/tasks
FROM mcr.microsoft.com/dotnet/sdk:7.0
WORKDIR /work

ARG --global EARTHLY_BUILD_SHA
ARG --global EARTHLY_GIT_BRANCH
# These are my common paths, used in my shared /Tasks repo
ARG --global OUTPUT_ROOT=/Modules
ARG --global TEST_ROOT=/tests
ARG --global TEMP_ROOT=/temp
# These are my common build args, used in my shared /Tasks repo
ARG --global MODULE_NAME=Pansies
ARG --global CONFIGURATION=Release
ARG --global PSMODULE_PUBLISH_KEY
ARG --global NUGET_API_KEY

worker:
# Dotnet tools and scripts installed by PSGet
ENV PATH=$HOME/.dotnet/tools:$HOME/.local/share/powershell/Scripts:$PATH
RUN mkdir /Tasks \
&& git config --global user.email "[email protected]" \
&& git config --global user.name "Earthly Build"
# I'm using Invoke-Build tasks from this other repo which rarely changes
COPY tasks+tasks/* /Tasks
# Dealing with dependencies first allows docker to cache packages for us
# So the dependency cach only re-builds when you add a new dependency
COPY RequiredModules.psd1 .
COPY *.csproj .
RUN ["pwsh", "--file", "/Tasks/_Bootstrap.ps1", "-RequiredModulesPath", "RequiredModules.psd1"]

build:
FROM +worker
RUN mkdir $OUTPUT_ROOT $TEST_ROOT $TEMP_ROOT
COPY . .
# make sure you have bin and obj in .earthlyignore, as their content from context might cause problems
RUN ["pwsh", "--command", "Invoke-Build", "-Task", "Build", "-File", "Build.build.ps1"]

# SAVE ARTIFACT [--keep-ts] [--keep-own] [--if-exists] [--force] <src> [<artifact-dest-path>] [AS LOCAL <local-path>]
SAVE ARTIFACT $OUTPUT_ROOT/$MODULE_NAME AS LOCAL ./Modules/$MODULE_NAME

test:
# If we run a target as a reference in FROM or COPY, it's outputs will not be produced
FROM +build
# make sure you have bin and obj in .earthlyignore, as their content from context might cause problems
RUN ["pwsh", "--command", "Invoke-Build", "-Task", "Test", "-File", "Build.build.ps1"]

# SAVE ARTIFACT [--keep-ts] [--keep-own] [--if-exists] [--force] <src> [<artifact-dest-path>] [AS LOCAL <local-path>]
SAVE ARTIFACT $TEST_ROOT AS LOCAL ./Modules/$MODULE_NAME-TestResults

pack:
BUILD +build # So that we get the build artifact too
FROM +build
RUN ["pwsh", "--command", "Invoke-Build", "-Task", "Pack", "-File", "Build.build.ps1", "-Verbose"]
SAVE ARTIFACT $OUTPUT_ROOT/publish/*.nupkg AS LOCAL ./Modules/$MODULE_NAME-Packages/

publish:
FROM +build
RUN ["pwsh", "--command", "Invoke-Build", "-Task", "Publish", "-File", "Build.build.ps1", "-Verbose"]

all:
# BUILD +build
BUILD +test
BUILD +pack
Loading

0 comments on commit 838a666

Please sign in to comment.