feat: implement a very flexible runner for "pre" and "post" scripts

This new functionality now makes it possible to execute scripts at the start or end of the build process, while also being super simple to expand to add further script stages in the future.

It also supports effortless reuse of scripts for multiple stages, since the scripts are now executed with the "current stage" as their 1st argument, to allow them to easily determine which stage they're running in.
This commit is contained in:
Arcitec 2023-05-10 06:54:34 +02:00 committed by Eino Rauhala
parent f596f4c496
commit 55ff6363be
3 changed files with 31 additions and 13 deletions

View file

@ -28,7 +28,11 @@ If you want to add custom configuration files, you can just add them in the `etc
### Custom build scripts
If you want to execute custom shell script or commands in the image build, you shouldn't edit `build.sh` or the `Containerfile` directly. Instead, you can create a shell script in the `scripts/` directory (look at the `example.sh`). After creating your script, enable it in the `scripts:` section of your `recipe.yml`.
If you want to execute custom shell scripts or commands in the image build, you shouldn't edit `build.sh` or the `Containerfile` directly.
Instead, you can create shell scripts in the `scripts/` directory (look at the `example.sh`). After creating your scripts, enable them in the `scripts:` section of your `recipe.yml`, within the specific "build stage" category where the scripts are intended to be executed.
Your scripts will be given exactly one argument when they are executed, which specifies its precise execution phase and corresponds to the name of the `scripts:` category that it was assigned to. The primary purpose of this argument is to streamline the reuse of scripts for multiple stages.
### Custom package repositories

View file

@ -32,16 +32,20 @@ if [[ ${#repos[@]} -gt 0 ]]; then
echo "---"
fi
# Run scripts.
get_yaml_array buildscripts '.scripts[]'
if [[ ${#buildscripts[@]} -gt 0 ]]; then
echo "-- Running scripts defined in recipe.yml --"
for script in "${buildscripts[@]}"; do
echo "Running: ${script}"
/tmp/scripts/"$script"
done
echo "---"
fi
# Run "pre" scripts.
run_scripts() {
script_mode="$1"
get_yaml_array buildscripts ".scripts.${script_mode}[]"
if [[ ${#buildscripts[@]} -gt 0 ]]; then
echo "-- Running [${script_mode}] scripts defined in recipe.yml --"
for script in "${buildscripts[@]}"; do
echo "Running [${script_mode}]: ${script}"
/tmp/scripts/"$script" "${script_mode}"
done
echo "---"
fi
}
run_scripts "pre"
# Remove RPMs.
get_yaml_array remove_rpms '.rpm.remove[]'
@ -77,3 +81,6 @@ if [[ ${#flatpaks[@]} -gt 0 ]]; then
done
echo "---"
fi
# Run "post" scripts.
run_scripts "post"

View file

@ -18,11 +18,18 @@ fedora-version: 38
description: A starting point for further customization of uBlue images. Make your own! https://ublue.it/making-your-own/
# These scripts will be executed during the container build.
# Place scripts in "scripts/" and put the corresponding filename here.
# Place scripts in the "scripts/" dir and put the corresponding filenames here.
# Any files that aren't listed here won't be executed automatically, which
# means that you can place "helper" or "library" scripts in the folder too.
scripts:
# - example.sh
# "Pre" scripts run very early in the build, immediately after your custom
# repos have been imported (so that you can access those repos if necessary).
pre:
# - example_pre.sh
# "Post" scripts run at the end of the build process.
post:
# - example_post.sh
# Custom RPM configuration.
# These changes will be integrated into your custom image at the "system level".