0

At the moment I have a working Jenkins declarative pipeline set up as a paramterized build, and a git pre-push hook set up to build it.

I would like to migrate to a multi-branch setup, where the feature/<someCoolFeature> and development branches are configured to deploy to one server, test is configured to deploy to a different server and production to the production server.

I am comfortable with triggering builds and defining parameters for which branches are built when, but all the documentation, questions and blog posts skip over a central thing:

How do I configure the deployment variables for the different servers?

As I see it, since you don't have switch and if control structures in declarative pipelines. I have to create a library which looks up which BRANCH_NAME I am building, and set the environment variables, or choose the corresponding application-<env>.yml based on that, but there must be a best practice somewhere that I am missing.

JoSSte
  • 2,953
  • 6
  • 34
  • 54

1 Answers1

1

Though not encouraged for the sake of simplicity and purpose of the declarative pipelines, you can still use both switch and if control structures within them:

Before the pipeline {...} block

// switch or if statements here

pipeline {
    stages {
        ...
    }
}

Inside a stage by wrapping them within a script {} block:

stage('prepare-env') {
    steps {
        script {
            // switch or if statements here
        }
    }
}

See https://jenkins.io/doc/book/pipeline/syntax/#script.

Dibakar Aditya
  • 3,893
  • 1
  • 14
  • 25