Skip to content

Build missing images (after new windows updates) #606

Build missing images (after new windows updates)

Build missing images (after new windows updates) #606

name: Build missing images (after new windows updates)
on:
workflow_dispatch:
inputs:
GenericTag:
description: Generic Tag (leave empty to use value in generic/tag.txt)
required: false
default: ''
PushToProd:
description: Push to production (Y/N)
type: boolean
permissions:
contents: read
defaults:
run:
shell: PowerShell
jobs:
AnalyzeImages:
runs-on: [ windows-latest ]
outputs:
missing: ${{ steps.Analyze.outputs.missing }}
missingcount: ${{ steps.Analyze.outputs.missingcount }}
steps:
- uses: actions/checkout@v4
- name: Analyze
id: Analyze
env:
genericTag: ${{ github.event.inputs.GenericTag }}
run: |
$erroractionpreference = "STOP"
try {
$servercoretags = @('ltsc2016','ltsc2019','ltsc2022')
Set-Location "generic"
$genericTag = "$env:genericTag"
if ($genericTag -eq '') { $genericTag = Get-Content -path 'tag.txt' }
[System.Version]$genericTag | Out-Null
Write-Host "Using generic Tag $genericTag"
$webclient = New-Object System.Net.WebClient
$webclient.Headers.Add('Accept', "application/json")
$neededBcTags = $serverCoreTags | ForEach-Object {
$osVersion = [System.Version](($webclient.DownloadString("https://mcr.microsoft.com/v2/dotnet/framework/runtime/manifests/4.8-windowsservercore-$_") | ConvertFrom-Json).history[0].v1Compatibility | ConvertFrom-Json)."os.version"
"$osVersion-$genericTag|mcr.microsoft.com/dotnet/framework/runtime:4.8-windowsservercore-$_|$_"
"$osVersion-$genericTag-filesonly|mcr.microsoft.com/dotnet/framework/runtime:4.8-windowsservercore-$_|$_"
}
Write-Host "Needed Tags ($($neededBcTags.Count))"
$neededBcTags | ForEach-Object { Write-Host "- $_" }
$alltags = (($webclient.DownloadString("https://mcr.microsoft.com/v2/businesscentral/tags/list") | ConvertFrom-Json)).tags
$missingBcTags = @($neededBcTags | Where-Object { $alltags -notcontains $_ })
Write-Host "Missing Tags ($($missingBcTags.Count))"
if ($missingBcTags) {
$missingBcTags | ForEach-Object { Write-Host "- $_" }
}
else {
Write-Host '- none'
}
$json = ConvertTo-Json -InputObject $missingBcTags -Compress
$json | Out-Host
Add-Content -encoding utf8 -Path $ENV:GITHUB_OUTPUT -Value "missing=$json"
Write-Host "missing=$json"
Add-Content -encoding utf8 -Path $ENV:GITHUB_OUTPUT -Value "missingcount=$($missingBcTags.Count)"
Write-Host "missingcount=$($missingBcTags.Count)"
}
catch {
Write-Host "::Error::Error analyzing images. Error was $($_.Exception.Message)"
$host.SetShouldExit(1)
}
Missing:
runs-on: [ Windows-Latest ]
needs: [ AnalyzeImages ]
if: ${{ needs.AnalyzeImages.outputs.missingcount > 0 }}
strategy:
matrix:
tag: ${{fromJson(needs.AnalyzeImages.outputs.missing)}}
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Build Image
env:
PushToProd: ${{ github.event.inputs.PushToProd }}
GenericTag: ${{ github.event.inputs.GenericTag }}
run: |
$erroractionpreference = "STOP"
Set-StrictMode -version 2.0
try {
$pushRegistry = "mcrbusinesscentral.azurecr.io"
$job = start-job -ScriptBlock { Param($username, $token, $registry)
Write-Output $token | docker login --username $username --password-stdin $registry
} -ArgumentList '${{ secrets.PushUsername }}', '${{ secrets.PushToken }}', $pushRegistry
$job | Wait-Job -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null
$result = Receive-Job -ErrorAction SilentlyContinue -WarningAction SilentlyContinue $job 2> $NULL
if ($result -ne 'Login Succeeded') {
throw "docker login failed"
}
Set-Location "generic"
$genericTag = ''
$pushToProd = $true
if ($env:GITHUB_EVENT_NAME -eq "workflow_dispatch") {
$genericTag = $env:GenericTag
$pushToProd = $env:PushToProd -eq 'True'
}
if ($genericTag -eq '') { $genericTag = Get-Content -path 'tag.txt' }
[System.Version]$genericTag | Out-Null
$osversion = '${{ matrix.tag }}'.split('|')[0].split('-')[0]
$filesonly = ('${{ matrix.tag }}' -like '*-filesonly|*')
$only24 = ('${{ matrix.tag }}' -like '*-24|*' -or '${{ matrix.tag }}' -like '*-24-filesonly|*')
$baseImage = '${{ matrix.tag }}'.split('|')[1]
$ltscTag = '${{ matrix.tag }}'.split('|')[2]
$rootPath = Get-Location
$dockerfile = Join-Path $rootPath "DOCKERFILE"
$strFilesOnly = ''
$str24 = ''
if ($only24) {
$str24 = "-24"
}
if ($filesOnly) {
$strFilesOnly = "-filesonly"
$dockerfile += '-filesonly'
}
$image = "my:$osversion-$genericTag$str24$strFilesOnly"
$newtags = @(
"$pushRegistry/public/businesscentral:$osversion$str24$strFilesonly-dev"
"$pushRegistry/public/businesscentral:$ltscTag$str24$strFilesonly-dev"
)
if ($pushToProd) {
$newtags += @(
"$pushRegistry/public/businesscentral:$osversion$str24$strFilesonly"
"$pushRegistry/public/businesscentral:$osversion-$genericTag$str24$strFilesonly"
"$pushRegistry/public/businesscentral:$ltscTag$str24$strFilesonly"
)
}
$newTags | out-host
$created = [DateTime]::Now.ToUniversalTime().ToString("yyyyMMddHHmm")
docker pull $baseimage
$inspect = docker inspect $baseimage | ConvertFrom-Json
$success = $false
docker build --build-arg baseimage=$baseimage `
--build-arg created=$created `
--build-arg tag="$genericTag" `
--build-arg osversion="$osversion" `
--build-arg filesonly="$filesonly" `
--build-arg only24="$only24" `
--isolation=hyperv `
--memory 8G `
--tag $image `
--file $dockerfile `
$RootPath | % {
$_ | Out-Host
if ($_ -like "Successfully built*") {
$success = $true
}
}
if (!$success) {
throw "Error building image"
}
$newtags | ForEach-Object {
Write-Host "Push $_"
docker tag $image $_
docker push $_
}
}
catch {
Write-Host "::Error::Error building images. Error was $($_.Exception.Message)"
$host.SetShouldExit(1)
}