When directive is not working properly during branch indexing for multibranch pipeline?

Jenkins setup:
Version: 2.415
Plugins:

List of Plugins

build user vars plugin (build-user-vars-plugin): 1.9
Durable Task Plugin (durable-task): 507.v050055d0cb_dd
Pipeline: Stage Tags Metadata (pipeline-stage-tags-metadata): 2.2141.v5402e818a_779
Pipeline: SCM Step (workflow-scm-step): 415.v434365564324
Plain Credentials Plugin (plain-credentials): 143.v1b_df8b_d3b_e48
SCM API Plugin (scm-api): 676.v886669a_199a_a_
Autofavorite for Blue Ocean (blueocean-autofavorite): 1.2.5
Pipeline: API (workflow-api): 1219.v05cd837ea_249
Blue Ocean Pipeline Editor (blueocean-pipeline-editor): 1.27.4
HTTP Request Plugin (http_request): 1.18
JUnit Plugin (junit): 1214.va_2f9db_3e6de0
Subversion Plug-in (subversion): 2.17.2
Git Pipeline for Blue Ocean (blueocean-git-pipeline): 1.27.4
SAML Plugin (saml): 4.418.vdfa_7489a_b_a_2d
Trilead API Plugin (trilead-api): 2.84.v72119de229b_7
Pipeline: AWS Steps (pipeline-aws): 1.43
EnvInject API Plugin (envinject-api): 1.199.v3ce31253ed13
commons-text API Plugin (commons-text-api): 1.10.0-36.vc008c8fcda_7b_
Mailer Plugin (mailer): 457.v3f72cb_e015e5
Blue Ocean Core JS (blueocean-core-js): 1.27.4
Amazon Web Services SDK :: EC2 (aws-java-sdk-ec2): 1.12.481-392.v8b_291cfcda_09
Jakarta Mail API (jakarta-mail-api): 2.0.1-3
REST API for Blue Ocean (blueocean-rest): 1.27.4
Server Sent Events (SSE) Gateway Plugin (sse-gateway): 1.26
Pipeline: Declarative Extension Points API (pipeline-model-extensions): 2.2141.v5402e818a_779
Checks API plugin (checks-api): 2.0.0
Events API for Blue Ocean (blueocean-events): 1.27.4
GitHub plugin (github): 1.37.1
Workspace Cleanup Plugin (ws-cleanup): 0.45
Extensible Choice Parameter plugin (extensible-choice-parameter): 1.8.0
Pipeline Utility Steps (pipeline-utility-steps): 2.15.4
Pipeline: Input Step (pipeline-input-step): 468.va_5db_051498a_4
Amazon Web Services SDK :: SNS (aws-java-sdk-sns): 1.12.481-392.v8b_291cfcda_09
Folders Plugin (cloudbees-folder): 6.815.v0dd5a_cb_40e0e
Pipeline: Build Step (pipeline-build-step): 496.v2449a_9a_221f2
OWASP Markup Formatter Plugin (antisamy-markup-formatter): 159.v25b_c67cd35fb_
Ant Plugin (ant): 497.v94e7d9fffa_b_9
Branch API Plugin (branch-api): 2.1122.v09cb_8ea_8a_724
Pipeline: Job (workflow-job): 1316.vd2290d3341a_f
JSch dependency plugin (jsch): 0.2.8-65.v052c39de79b_2
Caffeine API Plugin (caffeine-api): 3.1.6-115.vb_8b_b_328e59d8
Pipeline: Supporting APIs (workflow-support): 839.v35e2736cfd5c
Gradle Plugin (gradle): 2.8.1
Oracle Java SE Development Kit Installer Plugin (jdk-tool): 66.vd8fa_64ee91b_d
Conditional BuildStep (conditional-buildstep): 1.4.2
Amazon Web Services SDK :: SSM (aws-java-sdk-ssm): 1.12.481-392.v8b_291cfcda_09
jQuery plugin (jquery): 1.12.4-1
Mina SSHD API :: Core (mina-sshd-api-core): 2.10.0-69.v28e3e36d18eb_
Amazon ECR plugin (amazon-ecr): 1.114.vfd22430621f5
Copy Artifact Plugin (copyartifact): 705.v5295cffec284
Pipeline: Stage View Plugin (pipeline-stage-view): 2.33
Docker Commons Plugin (docker-commons): 422.v9d1a_89cede51
Git plugin (git): 5.1.0
Amazon Web Services SDK :: CodeBuild (aws-java-sdk-codebuild): 1.12.481-392.v8b_291cfcda_09
bouncycastle API Plugin (bouncycastle-api): 2.28
Amazon Web Services SDK :: Minimal (aws-java-sdk-minimal): 1.12.481-392.v8b_291cfcda_09
Java JSON Web Token (JJWT) Plugin (jjwt-api): 0.11.5-77.v646c772fddb_0
Pipeline: Stage Step (pipeline-stage-step): 305.ve96d0205c1c6
DataTables.net API Plugin (data-tables-api): 1.13.4-3
Amazon Web Services SDK :: Elastic Beanstalk (aws-java-sdk-elasticbeanstalk): 1.12.481-392.v8b_291cfcda_09
Amazon Web Services SDK :: kinesis (aws-java-sdk-kinesis): 1.12.481-392.v8b_291cfcda_09
HTML Publisher plugin (htmlpublisher): 1.31
Pipeline: REST API Plugin (pipeline-rest-api): 2.33
Pipeline: Model API (pipeline-model-api): 2.2141.v5402e818a_779
Bitbucket Push and Pull Request Plugin (bitbucket-push-and-pull-request): 2.8.3
Amazon Web Services SDK :: Logs (aws-java-sdk-logs): 1.12.481-392.v8b_291cfcda_09
Personalization for Blue Ocean (blueocean-personalization): 1.27.4
Versions Node Monitors plugin (versioncolumn): 162.v85841b_0790d5
CloudBees Docker Build and Publish plugin (docker-build-publish): 1.4.0
Pipeline: Step API (workflow-step-api): 639.v6eca_cd8c04a_a_
Dark Theme (dark-theme): 336.v02165cd8c2ee
SSH Build Agents plugin (ssh-slaves): 2.877.v365f5eb_a_b_eec
Pipeline: Declarative (pipeline-model-definition): 2.2141.v5402e818a_779
Pipeline: Milestone Step (pipeline-milestone-step): 111.v449306f708b_7
ECharts API Plugin (echarts-api): 5.4.0-5
EC2 Fleet Jenkins Plugin (ec2-fleet): 2.7.0
Pipeline: Groovy (workflow-cps): 3697.vb_470e454c232
Common API for Blue Ocean (blueocean-commons): 1.27.4
Favorite (favorite): 2.4.2
Pub-Sub “light” Bus (pubsub-light): 1.17
LDAP Plugin (ldap): 682.v7b_544c9d1512
next-executions (next-executions): 179.vb_e011d7e3b_64
Script Security Plugin (script-security): 1251.vfe552ed55f8d
JAXB plugin (jaxb): 2.3.8-1
Text File Operations (text-file-operations): 1.3.2
Amazon Web Services SDK :: SQS (aws-java-sdk-sqs): 1.12.481-392.v8b_291cfcda_09
Bitbucket Pipeline for Blue Ocean (blueocean-bitbucket-pipeline): 1.27.4
Git server Plugin (git-server): 99.va_0826a_b_cdfa_d
JQuery3 API Plugin (jquery3-api): 3.7.0-1
SSH Credentials Plugin (ssh-credentials): 305.v8f4381501156
SnakeYAML API Plugin (snakeyaml-api): 1.33-95.va_b_a_e3e47b_fa_4
Pipeline (workflow-aggregator): 596.v8c21c963d92d
Amazon Web Services SDK :: ECS (aws-java-sdk-ecs): 1.12.481-392.v8b_291cfcda_09
Display URL for Blue Ocean (blueocean-display-url): 2.4.2
JavaBeans Activation Framework (JAF) API (javax-activation-api): 1.2.0-6
Design Language (jenkins-design-language): 1.27.4
Lockable Resources plugin (lockable-resources): 1172.v4b_8fc8eed570
Email Extension Plugin (email-ext): 2.99
Credentials Binding Plugin (credentials-binding): 604.vb_64480b_c56ca_
Ansible plugin (ansible): 240.vc26740a_625c0
disk-usage plugin (disk-usage): 1.0
Dashboard for Blue Ocean (blueocean-dashboard): 1.27.4
Material Theme (material-theme): 0.5.2-rc100.6121925fe229
Maven Integration plugin (maven-plugin): 3.22
Theme Manager (theme-manager): 193.vcef22f6c5f2b_
Amazon Web Services SDK :: IAM (aws-java-sdk-iam): 1.12.481-392.v8b_291cfcda_09
Matrix Authorization Strategy Plugin (matrix-auth): 3.1.8
Git Parameter Plug-In (git-parameter): 0.9.19
Config API for Blue Ocean (blueocean-config): 1.27.4
MapDB API Plugin (mapdb-api): 1.0.9-28.vf251ce40855d
Maven Artifact ChoiceListProvider (Nexus) (maven-artifact-choicelistprovider): 1.14
OkHttp Plugin (okhttp-api): 4.11.0-145.vcb_8de402ef81
Version Number Plugin (versionnumber): 1.11
Amazon Web Services SDK :: EFS (aws-java-sdk-efs): 1.12.481-392.v8b_291cfcda_09
commons-lang3 v3.x Jenkins API Plugin (commons-lang3-api): 3.12.0-36.vd97de6465d5b_
SSH Agent Plugin (ssh-agent): 333.v878b_53c89511
Pipeline implementation for Blue Ocean (blueocean-pipeline-api-impl): 1.27.4
Role-based Authorization Strategy (role-strategy): 633.v836e5b_3e80a_5
REST Implementation for Blue Ocean (blueocean-rest-impl): 1.27.4
GitHub Branch Source Plugin (github-branch-source): 1728.v859147241f49
Amazon Web Services SDK :: CloudFormation (aws-java-sdk-cloudformation): 1.12.481-392.v8b_291cfcda_09
Apache HttpComponents Client 5.x API Plugin (apache-httpcomponents-client-5-api): 5.2.1-1.0
Pipeline: Basic Steps (workflow-basic-steps): 1017.vb_45b_302f0cea_
Docker Pipeline (docker-workflow): 563.vd5d2e5c4007f
Amazon Web Services SDK :: All (aws-java-sdk): 1.12.481-392.v8b_291cfcda_09
Jersey 2 API (jersey2-api): 2.40-1
Javadoc Plugin (javadoc): 233.vdc1a_ec702cff
Pipeline SCM API for Blue Ocean (blueocean-pipeline-scm-api): 1.27.4
Environment Injector Plugin (envinject): 2.901.v0038b_6471582
Variant Plugin (variant): 59.vf075fe829ccb
Authentication Tokens API Plugin (authentication-tokens): 1.53.v1c90fd9191a_b_
Pipeline: GitHub Groovy Libraries (pipeline-github-lib): 42.v0739460cda_c4
Git client plugin (git-client): 4.4.0
Jackson 2 API Plugin (jackson2-api): 2.15.2-350.v0c2f3f8fc595
JavaMail API (javax-mail-api): 1.6.2-9
Ionicons API (ionicons-api): 56.v1b_1c8c49374e
Web for Blue Ocean (blueocean-web): 1.27.4
Pipeline Graph Analysis Plugin (pipeline-graph-analysis): 202.va_d268e64deb_3
Pipeline: Groovy Libraries (pipeline-groovy-lib): 656.va_a_ceeb_6ffb_f7
Resource Disposer Plugin (resource-disposer): 0.22
Build Timeout (build-timeout): 1.31
Instance Identity (instance-identity): 173.va_37c494ec4e5
Mercurial plugin (mercurial): 1260.vdfb_723cdcc81
Token Macro Plugin (token-macro): 359.vb_cde11682e0c
Bitbucket Branch Source Plugin (cloudbees-bitbucket-branch-source): 809.vc1d904b_30426
Handy Uri Templates 2.x API Plugin (handy-uri-templates-2-api): 2.1.8-22.v77d5b_75e6953
Job DSL (job-dsl): 1.84
Structs Plugin (structs): 324.va_f5d6774f3a_d
Permissive Script Security Plugin (permissive-script-security): 0.7
Authorize Project (authorize-project): 1.7.0
AnsiColor (ansicolor): 1.0.2
SSH server (sshd): 3.303.vefc7119b_ec23
CloudBees AWS Credentials Plugin (aws-credentials): 191.vcb_f183ce58b_9
Plugin Utilities API Plugin (plugin-util-api): 3.3.0
Mina SSHD API :: Common (mina-sshd-api-common): 2.10.0-69.v28e3e36d18eb_
Timestamper (timestamper): 1.25
i18n for Blue Ocean (blueocean-i18n): 1.27.4
Apache HttpComponents Client 4.x API Plugin (apache-httpcomponents-client-4-api): 4.5.14-150.v7a_b_9d17134a_5
Docker API Plugin (docker-java-api): 3.3.1-79.v20b_53427e041
PAM Authentication plugin (pam-auth): 1.10
Jira plugin (jira): 3.10
Pipeline: Multibranch (workflow-multibranch): 756.v891d88f2cd46
JWT for Blue Ocean (blueocean-jwt): 1.27.4
Groovy (groovy): 453.vcdb_a_c5c99890
Credentials Plugin (credentials): 1254.vb_96f366e7b_a_d
Run Condition Plugin (run-condition): 1.6
Build Environment Plugin (build-environment): 1.7
Font Awesome API Plugin (font-awesome-api): 6.4.0-2
Backup plugin (backup): 1.6.1
Active Choices Plug-in (uno-choice): 2.6.5
Display URL API (display-url-api): 2.3.7
External Monitor Job Type Plugin (external-monitor-job): 207.v98a_a_37a_85525
Amazon Web Services SDK :: ECR (aws-java-sdk-ecr): 1.12.481-392.v8b_291cfcda_09
Matrix Project Plugin (matrix-project): 789.v57a_725b_63c79
GitHub Pipeline for Blue Ocean (blueocean-github-pipeline): 1.27.4
Command Agent Launcher Plugin (command-launcher): 100.v2f6722292ee8
Atlassian Jira Software Cloud (atlassian-jira-software-cloud): 2.0.7
Blue Ocean (blueocean): 1.27.4
GitHub API Plugin (github-api): 1.314-431.v78d72a_3fe4c3
docker-build-step (docker-build-step): 2.9
JIRA Integration for Blue Ocean (blueocean-jira): 1.27.4
Slack Notification Plugin (slack): 664.vc9a_90f8b_c24a_
Jakarta Activation API (jakarta-activation-api): 2.0.1-3
Parameterized Trigger plugin (parameterized-trigger): 2.46
Pipeline: Nodes and Processes (workflow-durable-task-step): 1247.v7f9dfea_b_4fd0
Bootstrap 5 API Plugin (bootstrap5-api): 5.3.0-1

Hello, Jenkins community! Hope to pick your brains over this thing I’ve been banging my head on the wall about. So we have the following when directive for a test stage in the pipeline:

when {
  expression { runIntegrationTests == 'true' }
  environment name: 'DeployType', value: DeployTypeConstants.NEW_DEPLOYMENT.value      
}

It works as normal when we’re running builds on already indexed branches, but during branch indexing, the stage fails to run because of the second condition in the directive. The following works, however:

expression { 
  runUnitTests == 'true' &&
  params.DeployType == DeployTypeConstants.NEW_DEPLOYMENT.value
}

Any advise on the matter is greatly appreciated.

UPDATE

We have pinpointed the issue to the following line:

environment name: 'DeployType', value: DeployTypeConstants.NEW_DEPLOYMENT.value

Note that the above line doesn’t work whether we use hardcoded values or enums.
Strange that other lines works like branch 'master'

Hello @SammyDeena and welcome to this community. :wave:

The issue you are facing may be related to the use of the environment directive in the when block, specifically when you are trying to set an environment variable based on a value during the branch indexing process.
The environment directive is evaluated when the pipeline is being compiled, and certain values might not be available or valid during that time. :person_shrugging: