How can I execute code everytime before my Jenkins pipeline runs

I wanted to be able to add stage based variables for my Jenkins pipeline. Therefore I added a function which checks for a yaml named buildVars.yaml to exist, and if so, loop through the vars per stage and set them in the env. The buildVars.yaml looks something like:

    id: a
    id: b
    id: c

However, my pipeline now looks like this:

stages {
        stage ('Prepare') {
            steps {
                script {
        stage('Starting docker build') {

I have to call buildUtils.loadEnvVarsFromYaml() everytime as the first step of any pipeline I have.

I would like that this function is called everytime, any pipeline starts. I don’t want to set an explicit Prepare stage in every pipeline I have, since it will be the same code over 100 times.

I have tried looking for plugins to accomplish this but I haven’t found any. I need something like a pre build step, which is executed everytime before the stages of my pipeline execute. Does anyone have any suggestions or ideas for this?

I looked for plugins that have a pre build step and I could not find any.

If you want to execute a specific function or code block before every stage in your Jenkins pipeline without explicitly adding it to each pipeline, you can make use of the Jenkins Shared Libraries feature. Shared Libraries allow you to define reusable code that can be used across multiple pipelines.