Some CommandDuringBuild implementations, such as SetBuildResultCommand, check permissions. This is necessary to prevent a random user (with at least Item.READ permission) from running one of these commands on someone else's build from an external command shell. (Cf.
Yet Jenkins provides no assistance in authenticating such commands. So if you are running in a properly secured instance and want to run set-build-result, your job would have to provide an SSH private key or password for a user with permissions on the job. Managing such credentials safely could be tricky.
It would be better if this were automatic, when the CLI command is indeed run from inside the build (say in a shell step). Perhaps $JENKINS_SERVER_COOKIE could be inspected on the CLI side and compared to Run.getCharacteristicEnvVars? Assuming there is a match, you could either
- Set the transport authentication for the command to the authentication in effect in Run.getExecutor, if not just SYSTEM. (Executor would need to have a method reporting its current authentication. Running workUnit.context.item.authenticate() again and assuming that the result is the same is unsafe since a job configuration might have changed since it started running.)
- Return null from getCurrentlyBuilding in case there is not a match, and removing permission checks from command implementations. This is arguably better because it will make the job work correctly even if the authentication in effect during the build is for a user (for example anonymous) who would not otherwise generally have those permissions on the job; or when there is no authentication in effect during the build at all.