Passing the result back from Parameterized Trigger plugin

心不动则不痛 提交于 2020-01-01 05:54:29

问题


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.

  1. In post-build actions, choose Archive the artifacts
  2. Put a path relative to the workspace. You can use wildcards, or hardcode the name of the file if it is always same.
  3. 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
  4. 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.

  1. 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
  2. For the Project name, enter the name of your "Helper" job
  3. For Which build, select Latest successful build
  4. 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
  5. For, Target directory, specify a location in your "Main" job's workspace where this file will be copied too.
  6. 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.

  1. 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
  2. For Projects to build, enter the name of your "Main" job
  3. Click Add Parameters button
  4. Select Predefined parameters
  5. Type VarForMain=$VarFromHelper, where VarFromHelper is your environment variable from the "Helper" job that contains your IP address, and VarForMain 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:

  1. Create a main job with a shell command of

    echo "PARAMS_FILE=${WORKSPACE}/build-${BUILD_NUMBER}.params" > "${WORKSPACE}/build-${BUILD_NUMBER}.params"
    
  2. Create sub-jobs by adding them to the build steps (not steps after build)

  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!