Skip to content

Commit

Permalink
Logging and validation improvements (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthalman authored Apr 21, 2024
1 parent 24725ca commit 665f1d2
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 24 deletions.
23 changes: 15 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ runs:
$baseStage = "${{ inputs.base-stage-name }}"
$arch = "${{ inputs.arch }}"
if (-not $baseImage -and -not $dockerfile) {
throw "'dockerfile' input not provided. This is required when 'base-image-name' is not provided."
}
$dockerBumpCheckerVersion = "0.2.0"
$containerName = "docker-bump-checker"
Expand All @@ -58,24 +62,27 @@ runs:
-DockerfilePath `"$dockerfile`" `
-BaseStageName `"$baseStage`" `
-Architecture `"$arch`"
if ($LASTEXITCODE -ne 0) {
throw "command failed"
}
$dockerRunExitCode = $LASTEXITCODE
docker cp ${containerName}:/home/app/log.txt log.txt
if ($LASTEXITCODE -ne 0) {
throw "command failed"
throw "Unable to retrieve log"
}
Get-Content log.txt
if ($dockerRunExitCode -ne 0) {
throw "command failed"
}
docker rm $containerName
if ($LASTEXITCODE -ne 0) {
throw "command failed"
}
echo "TRIGGER_WORKFLOW=$result" >> "$env:GITHUB_ENV"
echo "SEND_DISPATCH=$result" >> "$env:GITHUB_ENV"
- name: Repository Dispatch
if: env.TRIGGER_WORKFLOW == 'true'
if: env.SEND_DISPATCH == 'true'
uses: peter-evans/repository-dispatch@v3
with:
token: ${{ inputs.token }}
Expand All @@ -85,7 +92,7 @@ runs:
id: report-status
shell: pwsh
run: |
if ($env:TRIGGER_WORKFLOW -eq "true") {
if ($env:SEND_DISPATCH -eq "true") {
echo "A repository dispatch was sent to '${{ inputs.repository }}' with event type '${{ inputs.event-type }}'."
} else {
echo "No repository dispatch was sent."
Expand Down
22 changes: 17 additions & 5 deletions container/check-image.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@ $ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
Set-StrictMode -Version 2.0

$result = Invoke-Expression "dredge image compare layers --output json $BaseImage $TargetImage --os linux --arch $Architecture" | ConvertFrom-Json
if ($LASTEXITCODE -ne 0) {
function LogMessage ($Message) {
Write-Output $Message | Out-File $env:HOME/log.txt -Append
}

$expr = "dredge image compare layers --output json $BaseImage $TargetImage --os linux --arch $Architecture"
LogMessage "Invoke: $expr"
$result = Invoke-Expression $expr
$dredgeExitCode = $LASTEXITCODE
LogMessage "Result: $result"
if ($dredgeExitCode -ne 0) {
throw "dredge image compare failed"
}

$imageUpToDate = [bool]$($result.Summary.TargetIncludesAllBaseLayers)
$triggerWorkflow = ([string](-not $imageUpToDate)).ToLower()
$result = $result | ConvertFrom-Json

$imageUpToDate = [bool]$($result.summary.targetIncludesAllBaseLayers)
$sendDispatch = ([string](-not $imageUpToDate)).ToLower()

LogMessage "Send dispatch: $sendDispatch"

return $triggerWorkflow
return $sendDispatch
7 changes: 4 additions & 3 deletions container/entrypoint.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ $ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
Set-StrictMode -Version 2.0

if (-not $baseImage) {
$baseImage = $(& $PSScriptRoot/get-base-image.ps1 -DockerfilePath $DockerfilePath -BaseStageName $BaseStageName)
if (-not $BaseImage) {
$BaseImage = $(& $PSScriptRoot/get-base-image.ps1 -DockerfilePath $DockerfilePath -BaseStageName $BaseStageName)
}

$triggerWorkflow = $(& $PSScriptRoot/check-image.ps1 -TargetImage $targetImage -BaseImage $baseImage -Architecture $Architecture)
$triggerWorkflow = $(& $PSScriptRoot/check-image.ps1 -TargetImage $targetImage -BaseImage $BaseImage -Architecture $Architecture)

return $triggerWorkflow
19 changes: 12 additions & 7 deletions container/get-base-image.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[cmdletbinding()]
param(
[Parameter(Mandatory = $True)]
[string]$DockerfilePath,
[string]$BaseStageName
)
Expand All @@ -9,11 +10,7 @@ $ProgressPreference = 'SilentlyContinue'
Set-StrictMode -Version 2.0

function LogMessage ($Message) {
Write-Output $Message | Out-File $env:HOME/log.txt
}

if (-not $DockerfilePath) {
throw "'dockerfile' input not provided. This is required when 'base-image-name' is not provided."
Write-Output $Message | Out-File $env:HOME/log.txt -Append
}

if (-not (Test-Path $DockerfilePath)) {
Expand All @@ -39,13 +36,21 @@ if (-not $BaseStageName) {

$dfspyArgsString = $dfspyArgs -join ' '

$fromOutput = Invoke-Expression "dfspy $dfspyArgsString" | ConvertFrom-Json
if ($LASTEXITCODE -ne 0) {
$expr = "dfspy $dfspyArgsString"
LogMessage "Invoke: $expr"
$fromOutput = Invoke-Expression $expr
$dfspyExitCode = $LASTEXITCODE
LogMessage "Result: $fromOutput"
if ($dfspyExitCode -ne 0) {
throw "dfspy failed"
}
$fromOutput = $fromOutput | ConvertFrom-Json

if ($BaseStageName) {
$baseImage = $fromOutput | Where-Object { $_.PSObject.Properties.Name -contains "stageName" -and $_.stageName -eq $BaseStageName } | Select-Object -ExpandProperty imageName
if (-not $baseImage) {
throw "Could not find stage with name '$BaseStageName'."
}
} else {
# Find the last stage of the Dockerfile and walk its parent chain to find the base image
$currentNode = $fromOutput | Select-Object -Last 1
Expand Down
3 changes: 2 additions & 1 deletion container/tests/check-image.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
BeforeAll {
$global:LASTEXITCODE = 0
Mock Invoke-Expression { $global:LASTEXITCODE = 0 } -ParameterFilter { $Command -like "dotnet *" }

Mock Out-File { }

$targetImage = "foo"
$baseImage = "bar"
$architecture = "amd64"
Expand Down
15 changes: 15 additions & 0 deletions container/tests/get-base-image.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,19 @@ Describe 'Get base image' {

{ & $targetScript -DockerfilePath 'Dockerfile' } | Should -Throw "dfspy failed"
}

It 'Given an unknown base stage name, it throws an error' {
$LASTEXITCODE = 0
Mock Invoke-Expression {
$fromOutput = @(
@{
imageName = "foo"
stageName = "stage1"
}
)
$fromOutput | ConvertTo-Json
} -ParameterFilter { $Command -eq "dfspy query from -f Dockerfile" }

{ & $targetScript -DockerfilePath 'Dockerfile' -BaseStageName stage2 } | Should -Throw "Could not find stage with name 'stage2'."
}
}

0 comments on commit 665f1d2

Please sign in to comment.