问题
I have 2 jobs: "Helper" and "Main" and the single jenkins instance (which is the host and the executor).
The helper manages 3rd party resource and makes the preparation for the Main job (to be precise - it creates the environment for the application to be deployed for testing).
The only artifact for the helper job is a single file with IP of the environment prepared especially for the Main job.
How would I pass back the build from the Helper to the Main in this case?
回答1:
You are saying that you only need to pass a file with an IP to the "Main" job. If all you need is that IP, there are easier ways of doing it (without files), I will describe both.
To pass an artifact from one job to another
In the "Helper" job, you need to archive that file from your workspace.
- In post-build actions, choose Archive the artifacts
- Put a path relative to the workspace. You can use wildcards, or hardcode the name of the file if it is always same.
- Configure this job to automatically trigger your "Main" job using Trigger/Call builds on other projects build step. If you don't have this plugin, you can get it here
- For Projects to build, enter the name of your "Main" job
Now, in the "Main" job, you need to copy this artifact from the previous ("Helper") job.
- For the first build step, select Copy artifacts from another project build step. If you don't have this plugin, you can get it here
- For the Project name, enter the name of your "Helper" job
- For Which build, select Latest successful build
- For Artifacts to copy, use
**/yourartifactname*.*
Your artifact name will be what you configured in "Helper" job. Using**/
in front makes sure it will ignore any directory structure before getting to the artifact - For, Target directory, specify a location in your "Main" job's workspace where this file will be copied too.
- Checkmark Flatten directories, so the file goes directly to the location specified in Step 5, else it will retain the directory structure that it was archived under (in "Helper" job)
Now, your "Main" job has the file from "Helper" job in it's workspace. Use it like you would any other file in your workspace
To pass a variable from one job to another
Like I mentioned, if all you need is that one IP address, that you have as a variable at one point in time in "Helper" job, you just send it to "Main" job using the Trigger/Call builds on other projects step that you configured in steps 3 and 4 of the "Helper" job. In this case, you don't need any special configuration on "Main" job.
- Configure "Helper" job to automatically trigger your "Main" job using Trigger/Call builds on other projects build step. If you don't have this plugin, you can get it here
- For Projects to build, enter the name of your "Main" job
- Click Add Parameters button
- Select Predefined parameters
- Type
VarForMain=$VarFromHelper
, whereVarFromHelper
is your environment variable from the "Helper" job that contains your IP address, andVarForMain
is the environment variable that will be set in your "Main" job to this value. There is no reason why these can't have the same name.
Now, in your "Main" job, you can reference $VarForMain
as you would any other environment variable
回答2:
The accepted answer wasn't helpful in my case, but I've just came up with a trick:
Create a main job with a shell command of
echo "PARAMS_FILE=${WORKSPACE}/build-${BUILD_NUMBER}.params" > "${WORKSPACE}/build-${BUILD_NUMBER}.params"
Create sub-jobs by adding them to the build steps (not steps after build)
Pass the file as a parameters source to the sub-builds and have the builds updated the file with a line in their scripts like:
echo "MY_VAR=some_value" >> "$PARAMS_FILE"
That way all subsequent jobs have environment updated with the results of their predecessors.
来源:https://stackoverflow.com/questions/14515015/passing-the-result-back-from-parameterized-trigger-plugin