From 55ff6363be7a783a5949ede05575d2936a4c6e29 Mon Sep 17 00:00:00 2001 From: Arcitec <38923130+Arcitec@users.noreply.github.com> Date: Wed, 10 May 2023 06:54:34 +0200 Subject: [PATCH] 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. --- README.md | 6 +++++- build.sh | 27 +++++++++++++++++---------- recipe.yml | 11 +++++++++-- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 3ead5ba9e7..af10a70abb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/build.sh b/build.sh index 7771846bf4..639b609294 100644 --- a/build.sh +++ b/build.sh @@ -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" diff --git a/recipe.yml b/recipe.yml index f0b773f995..b64a703805 100644 --- a/recipe.yml +++ b/recipe.yml @@ -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".