Skip to content

Commit 37dbea0

Browse files
author
Patrick Boos
committed
Allow setting up ssh-agent before SCM. This way git can as well use the ssh-agent settings.
1 parent eeab261 commit 37dbea0

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/main/java/com/cloudbees/jenkins/plugins/sshagent/SSHAgentBuildWrapper.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class SSHAgentBuildWrapper extends BuildWrapper {
5555
* The {@link com.cloudbees.jenkins.plugins.sshcredentials.SSHUser#getId()} of the credentials to use.
5656
*/
5757
private final String user;
58+
private final boolean runBeforeSCM;
5859

5960
/**
6061
* Constructs a new instance.
@@ -63,8 +64,9 @@ public class SSHAgentBuildWrapper extends BuildWrapper {
6364
*/
6465
@DataBoundConstructor
6566
@SuppressWarnings("unused") // used via stapler
66-
public SSHAgentBuildWrapper(String user) {
67+
public SSHAgentBuildWrapper(String user, boolean runBeforeSCM) {
6768
this.user = user;
69+
this.runBeforeSCM = runBeforeSCM;
6870
}
6971

7072
/**
@@ -77,12 +79,46 @@ public String getUser() {
7779
return user;
7880
}
7981

82+
/**
83+
* Gets the runBeforeSCM setting. If set to true, this will create the environment before SCM
84+
* (at {@link com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper#preCheckout(hudson.model.AbstractBuild, hudson.Launcher, hudson.model.BuildListener)}).
85+
* Otherwise it will return the environment in
86+
* {@link com.cloudbees.jenkins.plugins.sshagent.SSHAgentBuildWrapper#setUp(hudson.model.AbstractBuild, hudson.Launcher, hudson.model.BuildListener)}.
87+
*
88+
* @return true if SSHAgentEnvironment gets created in preCheckout(...), otherwise created in setUp(...).
89+
*/
90+
@SuppressWarnings("unused") // used via stapler
91+
public boolean getRunBeforeSCM() {
92+
return runBeforeSCM;
93+
}
94+
95+
/**
96+
* {@inheritDoc}
97+
*/
98+
@Override
99+
public void preCheckout(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
100+
if (runBeforeSCM) {
101+
build.getEnvironments().add(createSSHAgentEnvironment(build, launcher, listener));
102+
}
103+
}
104+
80105
/**
81106
* {@inheritDoc}
82107
*/
83108
@Override
84109
public Environment setUp(AbstractBuild build, final Launcher launcher, BuildListener listener)
85110
throws IOException, InterruptedException {
111+
if (runBeforeSCM) {
112+
// Jenkins needs this:
113+
// null would stop the build, and super implementation throws UnsupportedOperationException
114+
return new Environment() {
115+
};
116+
}
117+
118+
return createSSHAgentEnvironment(build, launcher, listener);
119+
}
120+
121+
private Environment createSSHAgentEnvironment(AbstractBuild build, Launcher launcher, BuildListener listener) {
86122
SSHUserPrivateKey userPrivateKey = null;
87123
for (SSHUserPrivateKey u : CredentialsProvider
88124
.lookupCredentials(SSHUserPrivateKey.class, build.getProject(), ACL.SYSTEM)) {

src/main/resources/com/cloudbees/jenkins/plugins/sshagent/SSHAgentBuildWrapper/config.jelly

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@
2727
<f:entry title="${%Credentials}" field="user">
2828
<f:select/>
2929
</f:entry>
30+
<f:entry title="${%Run before SCM}" field="runBeforeSCM">
31+
<f:checkbox />
32+
</f:entry>
3033
</j:jelly>

0 commit comments

Comments
 (0)