particle-os-modules/build-individual.nu
xyny ea2720ba4e
feat: support versioning modules (#240)
* feat: add nu script that generates build matrix for github action

* chore: push initial version of new github action for testing

* fix: properly close github template sequence

* chore: screw this i'll write the whole build in nu (nu build script started, continue later)

* fix: individual misspellings and such

(yeah, i'm tired)

* fix: containerfile path

* fix: docker arg syntax

* fix: docker build path?

* feat: code structure, buildx, pushing to registry

* fix: tag image properly

* fix: split arguments properly

* fix: use registry properly

* refactor: move docker build to a separate par-each

* feat: correctly tag versioned modules

* feat: cosign signing, better logging

* fix: cosign syntax

* fix: differentiate log types with more expressive colors

* chore: fix cosign syntax in logs

* fix: capture errors, colocate logs while running paraller

* chore: partly revert "fix: capture errors, colocate logs while running paraller"

This reverts commit 9238a0f1d68183e712b567fc50849964cc964c78.

* chore: bring back capture errors, keep order in logs

apparently do --capture-errors is required for nushell to catch external commands errors

* chore: Revert "chore: bring back capture errors, keep order in logs"

These changes didn't do anything...
This reverts commit 020b9a1bce9456f2167397b49aa24a65f6bec8e6.

* fix: properly tag images in PRs and secondary branches

* fix: use tags-variable instead of "tags" string

* chore: change default-flatpaks module folder structure to be versioned

* fix: also log generated tags for versioned modules

* fix: don't add tags meant for latest image for every version of versioned module

* fix: better logging and inline docs

* fix: better logging pt2

* feat: build-unified for building the legacy modules container with just the latest versions

* fix: correct workflow names

* fix: add missing ansi resets

* chore: add nushell extension to recommendations

* fix: update unified job name

Co-authored-by: Gerald Pinder <gmpinder@gmail.com>

* chore: remove matrix output left over from a previous version

---------

Co-authored-by: Gerald Pinder <gmpinder@gmail.com>
2024-08-11 11:20:10 +00:00

97 lines
No EOL
3.7 KiB
Text

#!/usr/bin/env nu
# build separate images for each module in the repo
print $"(ansi green_bold)Gathering images"
let images = ls modules | each { |moduleDir|
cd $moduleDir.name
# module is unversioned
if ($"($moduleDir.name | path basename).sh" | path exists) {
print $"(ansi cyan)Found(ansi reset) (ansi cyan_bold)unversioned(ansi reset) (ansi cyan)module:(ansi reset) ($moduleDir.name | path basename)"
let tags = (
if ($env.GH_EVENT_NAME != "pull_request" and $env.GH_BRANCH == "main") {
["latest", "v1"]
} else if ($env.GH_EVENT_NAME != "pull_request") {
[$env.GH_BRANCH, $"v1-($env.GH_BRANCH)"]
} else {
[$"pr-($env.GH_PR_NUMBER)", $"v1-pr-($env.GH_PR_NUMBER)"]
}
)
print $"(ansi cyan)Generated tags:(ansi reset) ($tags | str join ' ')"
{
name: ($moduleDir.name | path basename)
directory: ($moduleDir.name)
tags: $tags
}
} else { # module is versioned
print $"(ansi cyan)Found(ansi reset) (ansi blue_bold)versioned(ansi reset) (ansi cyan)module:(ansi reset) ($moduleDir.name | path basename)"
let versioned = ls v*/
| get name | str substring 1.. | into int | sort # sort versions properly
| each {|version|
let tags = (
if ($env.GH_EVENT_NAME != "pull_request" and $env.GH_BRANCH == "main") {
[$"v($version)"]
} else if ($env.GH_EVENT_NAME != "pull_request") {
[$"v($version)-($env.GH_BRANCH)"]
} else {
[$"v($version)-pr-($env.GH_PR_NUMBER)"]
}
)
print $"(ansi cyan)Generated tags:(ansi reset) ($tags | str join ' ')"
{
name: ($moduleDir.name | path basename)
directory: $"($moduleDir.name)/v($version)"
tags: $tags
}
}
let latest_tag = (
if ($env.GH_EVENT_NAME != "pull_request" and $env.GH_BRANCH == "main") {
"latest"
} else if ($env.GH_EVENT_NAME != "pull_request") {
$env.GH_BRANCH
} else {
$"pr-($env.GH_PR_NUMBER)"
}
)
print $"(ansi cyan)Extra tag for latest image:(ansi reset) ($latest_tag)"
let latest = ($versioned | last)
($versioned
| update (($versioned | length) - 1) # update the last / latest item in list
($latest | update "tags" ($latest.tags | append latest_tag)) # append tag which should only be given to the latest version
)
}
} | flatten directory
print $"(ansi green_bold)Starting image build(ansi reset)"
$images | par-each { |img|
print $"(ansi cyan)Building image:(ansi reset) modules/($img.name)"
(docker build .
-f ./individual.Containerfile
...($img.tags | each { |tag| ["-t", $"($env.REGISTRY)/modules/($img.name):($tag)"] } | flatten) # generate and spread list of tags
--build-arg $"DIRECTORY=($img.directory)"
--build-arg $"NAME=($img.name)")
print $"(ansi cyan)Pushing image:(ansi reset) ($env.REGISTRY)/modules/($img.name)"
let digest = (
docker push --all-tags $"($env.REGISTRY)/modules/($img.name)"
| split row "\n" | last | split row " " | get 2 # parse push output to get digest for signing
)
print $"(ansi cyan)Signing image:(ansi reset) ($env.REGISTRY)/modules/($img.name)@($digest)"
cosign sign -y --key env://COSIGN_PRIVATE_KEY $"($env.REGISTRY)/modules/($img.name)@($digest)"
}
print $"(ansi green_bold)DONE!(ansi reset)"