@@ -55,6 +55,7 @@ public class SSHAgentBuildWrapper extends BuildWrapper {
55
55
* The {@link com.cloudbees.jenkins.plugins.sshcredentials.SSHUser#getId()} of the credentials to use.
56
56
*/
57
57
private final String user ;
58
+ private final boolean runBeforeSCM ;
58
59
59
60
/**
60
61
* Constructs a new instance.
@@ -63,8 +64,9 @@ public class SSHAgentBuildWrapper extends BuildWrapper {
63
64
*/
64
65
@ DataBoundConstructor
65
66
@ SuppressWarnings ("unused" ) // used via stapler
66
- public SSHAgentBuildWrapper (String user ) {
67
+ public SSHAgentBuildWrapper (String user , boolean runBeforeSCM ) {
67
68
this .user = user ;
69
+ this .runBeforeSCM = runBeforeSCM ;
68
70
}
69
71
70
72
/**
@@ -77,12 +79,46 @@ public String getUser() {
77
79
return user ;
78
80
}
79
81
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
+
80
105
/**
81
106
* {@inheritDoc}
82
107
*/
83
108
@ Override
84
109
public Environment setUp (AbstractBuild build , final Launcher launcher , BuildListener listener )
85
110
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 ) {
86
122
SSHUserPrivateKey userPrivateKey = null ;
87
123
for (SSHUserPrivateKey u : CredentialsProvider
88
124
.lookupCredentials (SSHUserPrivateKey .class , build .getProject (), ACL .SYSTEM )) {
0 commit comments