@@ -908,6 +908,143 @@ public class SCMSourceRetrieverTest {
908
908
r .assertLogContains ("something very special" , b0 );
909
909
}
910
910
911
+ @ Issue ("JENKINS-69731" )
912
+ @ Test public void checkDefaultVersion_singleBranch_BRANCH_NAME_after_staticStrings () throws Exception {
913
+ // Test that using @Library('branchylib@static')
914
+ // in one build of a job definition, and then a
915
+ // @Library('branchylib@${BRANCH_NAME}') next,
916
+ // both behave well.
917
+ // For context see e.g. WorkflowJob.getSCMs():
918
+ // https://github.com/jonsten/workflow-job-plugin/blob/master/src/main/java/org/jenkinsci/plugins/workflow/job/WorkflowJob.java#L539
919
+ // https://issues.jenkins.io/browse/JENKINS-40255
920
+ // how it looks at a history of EARLIER builds
921
+ // (preferring successes) and not at the current
922
+ // job definition.
923
+ // Note: being a piece of test-driven development,
924
+ // this test does not fail as soon as it gets an
925
+ // "unexpected" log message (so far expected due
926
+ // to the bug being hunted), but counts the faults
927
+ // and asserts in the end whether there were none.
928
+ assumeFalse ("An externally provided BRANCH_NAME envvar interferes with tested logic" ,
929
+ System .getenv ("BRANCH_NAME" ) != null );
930
+
931
+ sampleRepo .init ();
932
+ sampleRepo .write ("vars/myecho.groovy" , "def call() {echo 'something special'}" );
933
+ sampleRepo .git ("add" , "vars" );
934
+ sampleRepo .git ("commit" , "--message=init" );
935
+ sampleRepo .git ("checkout" , "-b" , "feature" );
936
+ sampleRepo .write ("vars/myecho.groovy" , "def call() {echo 'something very special'}" );
937
+ sampleRepo .git ("add" , "vars" );
938
+ sampleRepo .git ("commit" , "--message=init" );
939
+ sampleRepo .git ("checkout" , "-b" , "stable" );
940
+ sampleRepo .write ("vars/myecho.groovy" , "def call() {echo 'something reliable'}" );
941
+ sampleRepo .git ("add" , "vars" );
942
+ sampleRepo .git ("commit" , "--message=init" );
943
+ SCMSourceRetriever scm = new SCMSourceRetriever (new GitSCMSource (null , sampleRepo .toString (), "" , "*" , "" , true ));
944
+ LibraryConfiguration lc = new LibraryConfiguration ("branchylib" , scm );
945
+ lc .setDefaultVersion ("master" );
946
+ lc .setIncludeInChangesets (false );
947
+ lc .setAllowVersionOverride (false );
948
+ lc .setAllowBRANCH_NAME (true );
949
+ lc .setTraceDefaultedVersion (true );
950
+ GlobalLibraries .get ().setLibraries (Collections .singletonList (lc ));
951
+
952
+ // Inspired in part by tests like
953
+ // https://github.com/jenkinsci/workflow-multibranch-plugin/blob/master/src/test/java/org/jenkinsci/plugins/workflow/multibranch/NoTriggerBranchPropertyWorkflowTest.java#L132
954
+ sampleRepo2 .init ();
955
+ sampleRepo2 .write ("Jenkinsfile" , "@Library('branchylib@${BRANCH_NAME}') import myecho; myecho()" );
956
+ sampleRepo2 .write ("Jenkinsfile-static" , "@Library('branchylib@stable') import myecho; myecho()" );
957
+ sampleRepo2 .git ("add" , "Jenkinsfile*" );
958
+ sampleRepo2 .git ("commit" , "--message=init" );
959
+ sampleRepo2 .git ("branch" , "feature" );
960
+ sampleRepo2 .git ("branch" , "bogus" );
961
+
962
+ // Get a non-default branch loaded for this single-branch build:
963
+ GitSCM gitSCM = new GitSCM (
964
+ GitSCM .createRepoList (sampleRepo2 .toString (), null ),
965
+ Collections .singletonList (new BranchSpec ("*/feature" )),
966
+ null , null , Collections .emptyList ());
967
+
968
+ sampleRepo2 .notifyCommit (r );
969
+ r .waitUntilNoActivity ();
970
+
971
+ // First run a job definition with a fixed library version,
972
+ // e.g. like a custom Replay might in the field, or before
973
+ // redefining an "inline" pipeline to one coming from SCM.
974
+ // Pepper job history with successes and faults:
975
+ long failCount = 0 ;
976
+ WorkflowJob p1 = r .jenkins .createProject (WorkflowJob .class , "p1" );
977
+ p1 .setDefinition (new CpsFlowDefinition ("@Library('branchylib@stable') import myecho; myecho()" , true ));
978
+ WorkflowRun b1 = r .buildAndAssertStatus (Result .FAILURE , p1 );
979
+ r .assertLogContains ("ERROR: Version override not permitted for library branchylib" , b1 );
980
+ r .assertLogContains ("WorkflowScript: Loading libraries failed" , b1 );
981
+
982
+ // Use default version:
983
+ p1 .setDefinition (new CpsFlowDefinition ("@Library('branchylib') import myecho; myecho()" , true ));
984
+ WorkflowRun b2 = r .buildAndAssertSuccess (p1 );
985
+ r .assertLogContains ("Loading library branchylib@master" , b2 );
986
+ r .assertLogContains ("something special" , b2 );
987
+
988
+ // Now redefine the same job to come from SCM and use a
989
+ // run-time resolved library version (WorkflowJob getSCMs
990
+ // behavior should not be a problem):
991
+ p1 .setDefinition (new CpsScmFlowDefinition (gitSCM , "Jenkinsfile" ));
992
+ WorkflowRun b3 = r .buildAndAssertSuccess (p1 );
993
+ try {
994
+ // In case of misbehavior this loads "master" version:
995
+ r .assertLogContains ("Loading library branchylib@feature" , b3 );
996
+ r .assertLogContains ("something very special" , b3 );
997
+ } catch (AssertionError ae ) {
998
+ failCount ++;
999
+ // Make sure it was not some other problem:
1000
+ r .assertLogContains ("Loading library branchylib@master" , b3 );
1001
+ r .assertLogContains ("something special" , b3 );
1002
+ }
1003
+
1004
+ // Override with a static version:
1005
+ lc .setAllowVersionOverride (true );
1006
+ p1 .setDefinition (new CpsFlowDefinition ("@Library('branchylib@stable') import myecho; myecho()" , true ));
1007
+ WorkflowRun b4 = r .buildAndAssertSuccess (p1 );
1008
+ r .assertLogContains ("Loading library branchylib@stable" , b4 );
1009
+ r .assertLogContains ("something reliable" , b4 );
1010
+
1011
+ // Dynamic version again:
1012
+ p1 .setDefinition (new CpsScmFlowDefinition (gitSCM , "Jenkinsfile" ));
1013
+ WorkflowRun b5 = r .buildAndAssertSuccess (p1 );
1014
+ try {
1015
+ // In case of misbehavior this loads "stable" version:
1016
+ r .assertLogContains ("Loading library branchylib@feature" , b5 );
1017
+ r .assertLogContains ("something very special" , b5 );
1018
+ } catch (AssertionError ae ) {
1019
+ failCount ++;
1020
+ // Make sure it was not some other problem:
1021
+ r .assertLogContains ("Loading library branchylib@stable" , b5 );
1022
+ r .assertLogContains ("something reliable" , b5 );
1023
+ }
1024
+
1025
+ // SCM source pointing at static version
1026
+ p1 .setDefinition (new CpsScmFlowDefinition (gitSCM , "Jenkinsfile-static" ));
1027
+ WorkflowRun b6 = r .buildAndAssertSuccess (p1 );
1028
+ r .assertLogContains ("Loading library branchylib@stable" , b6 );
1029
+ r .assertLogContains ("something reliable" , b6 );
1030
+
1031
+ // Dynamic version again; seems with the change of filename it works okay:
1032
+ p1 .setDefinition (new CpsScmFlowDefinition (gitSCM , "Jenkinsfile" ));
1033
+ WorkflowRun b7 = r .buildAndAssertSuccess (p1 );
1034
+ try {
1035
+ // In case of misbehavior this loads "stable" version:
1036
+ r .assertLogContains ("Loading library branchylib@feature" , b7 );
1037
+ r .assertLogContains ("something very special" , b7 );
1038
+ } catch (AssertionError ae ) {
1039
+ failCount ++;
1040
+ // Make sure it was not some other problem:
1041
+ r .assertLogContains ("Loading library branchylib@stable" , b7 );
1042
+ r .assertLogContains ("something reliable" , b7 );
1043
+ }
1044
+
1045
+ assertEquals ("All BRANCH_NAME resolutions are expected to checkout feature" ,0 , failCount );
1046
+ }
1047
+
911
1048
@ Issue ("JENKINS-69731" )
912
1049
@ Test public void checkDefaultVersion_singleBranch_BRANCH_NAME_doubleQuotes () throws Exception {
913
1050
// Similar to above, the goal of this test is to
0 commit comments