Skip to content

feat: expose the credential username via an optional usernameVariable - #285

Merged
ogulcanaydogan merged 1 commit into
jenkinsci:masterfrom
ogulcanaydogan:feature/username-variable
Jul 20, 2026
Merged

feat: expose the credential username via an optional usernameVariable#285
ogulcanaydogan merged 1 commit into
jenkinsci:masterfrom
ogulcanaydogan:feature/username-variable

Conversation

@ogulcanaydogan

Copy link
Copy Markdown
Contributor

The sshagent step only exposed SSH_AUTH_SOCK and SSH_AGENT_PID, so there was no way to pass the credential's SSH user to ssh -l without hardcoding it (JENKINS-45312).

This adds an optional usernameVariable parameter that binds the first resolved credential's username to that environment variable inside the block:

sshagent(credentials: ['my-key'], usernameVariable: 'SSH_USER') {
  sh 'ssh -l $SSH_USER example.org uname -a'
}

It mirrors the withCredentials usernameVariable convention, survives agent resume (it is stored on the step execution rather than the transient step), and is unset by default so existing pipelines are unaffected. With multiple credentials it uses the first.

Testing

Added exposesCredentialUsernameViaUsernameVariable to SSHAgentStepWorkflowTest, which runs sshagent(..., usernameVariable: 'SSH_USER') and asserts $SSH_USER resolves to the credential's username. mvn test -Dtest=SSHAgentStepWorkflowTest#exposesCredentialUsernameViaUsernameVariable passes.

Closes #234

@ogulcanaydogan
ogulcanaydogan requested a review from a team as a code owner July 19, 2026 13:31
The sshagent step only exposed SSH_AUTH_SOCK and SSH_AGENT_PID, so there was no
way to pass the credential's SSH user to ssh -l without hardcoding it.

Add an optional usernameVariable parameter that binds the first resolved
credential's username to the given environment variable inside the block,
mirroring the withCredentials usernameVariable convention. It survives agent
resume and is unset by default, so existing pipelines are unaffected.

Closes jenkinsci#234

Signed-off-by: Ogulcan Aydogan <[email protected]>
@ogulcanaydogan
ogulcanaydogan force-pushed the feature/username-variable branch from 63627a6 to d28da61 Compare July 20, 2026 08:32
@ogulcanaydogan
ogulcanaydogan merged commit eb3e5e0 into jenkinsci:master Jul 20, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[JENKINS-45312] expose the user of the credentialId used by sshagent

1 participant