@@ -121,6 +121,7 @@ type WrapperOpt func(*Wrapper)
121
121
type Wrapper struct {
122
122
hookPath string
123
123
os string
124
+ tempDir string
124
125
wrapperPath string
125
126
beforeEnvPath string
126
127
afterEnvPath string
@@ -202,19 +203,33 @@ func NewWrapper(opts ...WrapperOpt) (*Wrapper, error) {
202
203
templateType = PosixShellTemplateType
203
204
}
204
205
205
- if wrap .beforeEnvPath , err = tempfile .NewClosed (
206
- tempfile .WithDir (hookWrapperDir ),
206
+ // On systems where multiple buildkite-agents are running under different
207
+ // users, a shared path could be owned by a different user.
208
+ // Creating a new temporary directory to hold the temporary files avoids
209
+ // this issue and makes cleanup easier.
210
+ tempDir , err := os .MkdirTemp (os .TempDir (), hookWrapperDir )
211
+ if err != nil {
212
+ return nil , fmt .Errorf ("creating temporary directory for hook wrapper: %w" , err )
213
+ }
214
+ wrap .tempDir = tempDir
215
+
216
+ beforeEnvPath , err := tempfile .NewClosed (
217
+ tempfile .WithDir (tempDir ),
207
218
tempfile .WithName ("hook-before-env" ),
208
- ); err != nil {
219
+ )
220
+ if err != nil {
209
221
return nil , err
210
222
}
223
+ wrap .beforeEnvPath = beforeEnvPath
211
224
212
- if wrap . afterEnvPath , err = tempfile .NewClosed (
213
- tempfile .WithDir (hookWrapperDir ),
225
+ afterEnvPath , err : = tempfile .NewClosed (
226
+ tempfile .WithDir (tempDir ),
214
227
tempfile .WithName ("hook-after-env" ),
215
- ); err != nil {
228
+ )
229
+ if err != nil {
216
230
return nil , err
217
231
}
232
+ wrap .afterEnvPath = afterEnvPath
218
233
219
234
absolutePathToHook , err := filepath .Abs (wrap .hookPath )
220
235
if err != nil {
@@ -234,13 +249,16 @@ func NewWrapper(opts ...WrapperOpt) (*Wrapper, error) {
234
249
PathToHook : absolutePathToHook ,
235
250
}
236
251
237
- if wrap . wrapperPath , err = WriteHookWrapper (
252
+ wrapperPath , err : = WriteHookWrapper (
238
253
templateType ,
239
254
templateInput ,
255
+ tempDir ,
240
256
scriptFileName ,
241
- ); err != nil {
257
+ )
258
+ if err != nil {
242
259
return nil , err
243
260
}
261
+ wrap .wrapperPath = wrapperPath
244
262
245
263
return wrap , nil
246
264
}
@@ -251,10 +269,11 @@ func NewWrapper(opts ...WrapperOpt) (*Wrapper, error) {
251
269
func WriteHookWrapper (
252
270
templateType TemplateType ,
253
271
input WrapperTemplateInput ,
272
+ dir string ,
254
273
hookWrapperName string ,
255
274
) (string , error ) {
256
275
f , err := tempfile .New (
257
- tempfile .WithDir (hookWrapperDir ),
276
+ tempfile .WithDir (dir ),
258
277
tempfile .WithName (hookWrapperName ),
259
278
tempfile .KeepingExtension (),
260
279
tempfile .WithPerms (0o700 ),
@@ -291,9 +310,7 @@ func (w *Wrapper) Path() string {
291
310
// Close cleans up the wrapper script and the environment files. Ignores errors, in
292
311
// particular the error from os.Remove if the file doesn't exist.
293
312
func (w * Wrapper ) Close () {
294
- _ = os .Remove (w .wrapperPath )
295
- _ = os .Remove (w .beforeEnvPath )
296
- _ = os .Remove (w .afterEnvPath )
313
+ _ = os .RemoveAll (w .tempDir )
297
314
}
298
315
299
316
// Changes returns the changes in the environment and working dir after the hook script runs
0 commit comments