I have a gitlabcomponents file, and would like to add the possibility that before_script can be enhanced by the projects that use the component.
In the following example, I would want to offer other projects to execute additional before_script commands in between what I defined in the component.
You want to define a GitLab CI/CD component that has a default before_script, but also allows projects that use the component to inject their own extra before_script lines in between your steps.
Solution (Using inputs and extends)
GitLab CI does not support injecting arrays directly like this:
- $[[ inputs.before_script_additional ]] # ❌ This won't work
But you can achieve your goal using a combination of:
inputs (for user-defined commands)
script: blocks instead of before_script for full control
Template extending, if more flexibility is needed
Component Example (using script)
# .gitlab-ci-component.yml
spec:
inputs:
extra_before:
type: array
default: []
---
docker_compose:deploy:
stage: deploy
script:
- echo before
- for cmd in "${{ inputs.extra_before[@] }}"; do eval "$cmd"; done
- echo after
In this example, you loop over the array of extra commands and run each one between your fixed steps.