问题
I have two jobs: Job_a and Job_b. I want to use a variable in Job_a to decide whether an email should be sent in JOb_b.
The simplified jobs:
In Job_a:
Step 1: Execute Windows batch command:
echo GO=Y > testme.txt
this will create testme.txt with GO=Y in the workspace.
Step 2: Trigger/call builds on other projects:
projects to build: Job_b
Parameters from property files: testme.txt
In Job_b:
Step 1:
echo Go = %GO%
Post-build Actions Editable Email Notification
triggers: Script - After Build
trigger script:
build.envVars["GO"] == 'Y'
Now, run Job_a, no email triggered, while step 1 in Job_b, does print Go = Y
to prove the trigger script itself is correct, I add a String Parameter in Job_a:
Name: GO1
Default Value: Y
and change the trigger script in Job_b to:
build.envVars["GO1"] == 'Y'
run Job_a, thus JOb_b as well, and email was sent as expected.
it seems that only parameters defined in the upstream job can be used in the email trigger script, parameters in the property file are not available.
I know I could inject the parameter from the property file in the fly with EnvInject Plugin, and/or with System Groovy script, I can add new parameters, but my build environment is tight controlled, there is no System groovy, no new additional plugin can be installed.
My jenkins version is 1.622
回答1:
To share with everyone. I have found what went wrong.
The property file has one line: GO=Y
The parameter 'GO' in the property file created in Job_a IS indeed passed to Job_b.
The groovy test build.envVars["GO"] == 'Y'
does not return 'true', thus no email will be sent.
I print the length of the parameters:
println build.envVars["GO"].length()
println 'Y'.length()
which prints
2
1
Apparently, there is a new line carriage appended when we create the contents of the property file with
echo GO=Y > testme.txt
I have to trim off the new line carriage, so change the trigger script for the editable mail, Script - After build to
build.envVars["GO"].trim() == 'Y'
it works as expected.
来源:https://stackoverflow.com/questions/45470021/variable-from-a-property-file-used-in-email-trigger-script-in-jenkins