New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FIXED JENKINS-44456] Add STAGE_NAME to environment #10
Conversation
This pull request originates from a CloudBees employee. At CloudBees, we require that all pull requests be reviewed by other CloudBees employees before we seek to have the change accepted. If you want to learn more about our process please see this explanation. |
this.stageName = stageName; | ||
} | ||
@Override public void expand(EnvVars env) throws IOException, InterruptedException { | ||
env.override("STAGE_NAME", stageName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eek. It is time to introduce an API
public static EnvironmentExpander constant(Map<String, String> env);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeaaaaah. On it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Override public void evaluate() throws Throwable { | ||
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p"); | ||
p.setDefinition(new CpsFlowDefinition("stage('foo-stage') {echo \"STAGE_NAME is ${STAGE_NAME}\"}", true)); | ||
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could add another step
to prove that the serialization works, but probably overkill here.
Gonna leave this for a day to see if anyone else has any feedback before merging. |
Feel free to release—just be sure to update the changelog in the wiki if so. |
Can't yet since I don't have the rights, but opening a PR for that now. |
@svanoort as in, pipeline {
stages {
stage('build') {…}
stage('test') {…}
}
post {
failure {
mail …, subject: "Failure in $STAGE_NAME"
}
}
} ? I am not following. |
🐛 Because I feel we're generally following the wrong path here. Need to explain more fully, but I do not consider this appropriate to release until we can have a more full discussion. |
As I've said offline, I am very confused as to how adding an environment variable is the wrong path for, well, adding an environment variable. If you've got a better approach for delivering the end result of having |
My issue here is that there is not another good way provided to obtain the Stage information for APIs (as opposed to within pipeline code). This encourages people to (mis) use environment variables within plugins if we need stage info, rather than replying on a proper API. If we do one without the other, then you're going to get some horrifying code. |
Huh? You just scan up for the nearest enclosing |
So...can we release this? I know it's not optimal for @svanoort but I really don't think it's a risk in terms of plugin devs using it instead of other methodologies. |
This is actually both very inefficient and nonintuitive for people. It's fine from time to time but can potentially trigger iteration to the first flownode every time it is run. If we're binding the stage name into the EnvVars, why not bind it in the StepContext for in-progress flows as well? If you expose it there (via something like a StageName class), I'm satisfied. Otherwise my original concerns still have not been addressed. |
@svanoort Only hassle I'd see with that is that we'd need a distinct class for it, since |
Well then optimize it please. There is no inherent reason for this to be anything but instantaneous.
How so? Seems very intuitive to me that if you want to know what stage something is running in, you call the APIs which list enclosing blocks and, well, search for a stage block.
OK, so?
Because it is redundant. The flow graph already contains this information, so we would be providing an API for accessing something that you can already access. I still see no reason why release of this enhancement should be held up. |
I've tried, but you felt the FlowStorage engine is the only place to retain the needed info -- which means a much deeper change, with additional testing needs. The arguments for this were reasonable from an architectural standpoint; however this means that it is not solvable by the easier solution I'd proposed such as caching or retaining a sketch of the flow structure to satisfy block/parallel structural queries.
The flow graph should be consider the system of record for completed runs, but for in-progress flows it is completely fair to store some runtime information (as we already do). Implementing it that way is a fairly trivial change that follows the same pattern laid down here -- and I see no reason why StageAction shouldn't be able to simplify our lives by offering something like |
Wait, so |
@abayer Stored in the StepContext, StageAction just returns it from context if the FlowExecution is not completed (or null if absent). |
@svanoort But |
Or do you mean a static method? |
...all told, while I do understand that there could be value in easier API access to the current stage's name, I'm with @jglick in the end - I don't see how that is a requirement for adding an environment variable. @svanoort, if it's something you think should be there, I'd happily review and +1 it, but it's orthogonal to the need that drove this change. |
Static method. The method is probably a 3-5 liner once you add the object to the StepContext. |
@svanoort Can we release this? Getting some pressure on JIRA to get this rolled out. |
I'm probably that pressure :) |
@abayer I still feel that it is inappropriate to have this as an environment variable. I feel that the instant we have users writing pipeline code that changes depending on what stage they are in, we're opening up a nasty can of worms just for limited value add. Printing stage name on the other hand, I think is pretty okay (for notifications and so forth), but unfortunately we can't gain one without the other. |
@svanoort There are valid use cases for, say, being able to have shell scripts detect what stage they're on via said env var, etc... I really honestly don't see how this is problematic. |
@abayer To capture out of band discussion -- now that there's a pretty easy way for plugin authors to fetch enclosing stages (jenkinsci/workflow-api-plugin#53) I think it's safe to release this -- If you can add a warning in the Javadocs NOT to use the Env Var for plugin code (and a pointer to the workflow-api use to do) -- my main concern was people abusing this API in plugins (no no no no). That takes care of my big objections (pending a PR adding that note) and once done I'm happy to see it released and all. |
And for everyone else's benefit, I've released this plugin's version 2.3, which includes this change. =) |
JENKINS-44456
cc @reviewbybees