KBEC-00171 - Referencing the user who started a job

Article ID:360033193451
2 minute readKnowledge base

Description

Jobs have a launchedByUser defined as a property reference.

Common use cases for launchedByUser include:

  1. User to send a notifier

  2. In the notifier template

  3. Resource allocation which is workflow or job sensitiver

  4. In a runCondition

  5. In reports

There are considerations when using launchedByUser:

  1. Only launchedByUser is set - this means the user called runProcedure and passed projectName and procedureName. In this case, the UI shows a link to the user’s details page.

  2. Only scheduleName is set - this means it was a scheduled job, not launched by a user at all. In this case, the UI shows a link to the schedule’s details page.

  3. Both launchedByUser and scheduleName are set - this means the user called runProcedure and passed projectName and scheduleName (a manual run of a schedule). In this case, the UI shows a link to the user’s details page.

If you want to, you can figure out whether it’s the first or third case by calling getJobDetails to see if a scheduleName is set, or checking the summary at the top of the job details page to see if a schedule is displayed underneath the project and procedure.

Job Solution

  1. Be sure that you can handle the case where there is no launchedByUser. If a procedure runs off of a timed schedule, launchedByUser will be empty. Use JavaScript to handle this case.

  2. The object the launchedByUser is referring to is the full User object. To make the user explicit, add the "userName" property.

  3. Use the launchedByUser to index into the users object to find the login user.

  4. The job launchedByUser is associated with the job. In workflows and in workflow notifiers the job launchedByUser is not set.

Workflow Solution

Use the same approach in workflows as in the job. To have the launchedByUser reference work in a workflow or in a job with these following additional considerations:

  1. If you don’t have a workflow launchedByUser it is because you are on a subworkflow, walk up the workflow to find the parents launchedByUser if it exists

Additional considerations

If a schedule runs a job, then the launchedByUser field is blank. CloudBees CD (CloudBees Flow) displays the name of the schedule in the scheduleName property.

The confusion comes from the "launched by" column on the jobs page in the UI, which is either the launchedByUser (if launched by a user) or the scheduleName (if launched by a schedule).

The intrinsic property /myUser is available only when not in a step, i.e. from the ectool commandline after login. See the OLH for more details. launchedByUser is the proper way to reference a user in a workflow or subprocedure or step.