Skip to content

Commit 46e7e91

Browse files
committed
Run post build hook with /bin/sh -ic
We need either `/bin/bash -c` or `/bin/sh -ic` to make our supported SCL-powered images to work properly. That's due to a hack to auto-enable SCLs: https://github.com/openshift/sti-base/blob/8d95148/Dockerfile#L23-L29 In CentOS and RHEL images, `/bin/sh` is a symlink to `/bin/bash`. In that case, the ENV environment variable is only evaluated when the shell is started in interactive mode, which happens when there's and attached tty (not the case for post build hook) or when the `-i` argument is passed explicitly. Making the shell interactive with `-i` is ugly, but is the only solution we know so far without requiring Bash.
1 parent 6061189 commit 46e7e91

7 files changed

+45
-16
lines changed

examples/sample-app/application-template-dockerbuild.json

+3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@
144144
"name": "origin-ruby-sample:latest"
145145
}
146146
},
147+
"postCommit": {
148+
"script": "bundle exec rake test"
149+
},
147150
"resources": {}
148151
},
149152
"status": {

examples/sample-app/application-template-pullspecbuild.json

+3
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@
136136
"name": "origin-ruby-sample:latest"
137137
}
138138
},
139+
"postCommit": {
140+
"script": "bundle exec rake test"
141+
},
139142
"resources": {}
140143
},
141144
"status": {

examples/sample-app/application-template-stibuild.json

+3
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@
146146
"name": "origin-ruby-sample:latest"
147147
}
148148
},
149+
"postCommit": {
150+
"script": "bundle exec rake test"
151+
},
149152
"resources": {}
150153
},
151154
"status": {

pkg/build/api/types.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ type SourceBuildStrategy struct {
441441
// The above is a convenient form which is equivalent to:
442442
//
443443
// BuildPostCommitSpec{
444-
// Command: []string{"/bin/sh", "-c"},
444+
// Command: []string{"/bin/sh", "-ic"},
445445
// Args: []string{"rake test --verbose"},
446446
// }
447447
//
@@ -486,19 +486,24 @@ type SourceBuildStrategy struct {
486486
// the fields are specified, the hook is not executed.
487487
type BuildPostCommitSpec struct {
488488
// Command is the command to run. It may not be specified with Script.
489-
// This might be needed if the image doesn't have "/bin/sh", or if you
489+
// This might be needed if the image doesn't have `/bin/sh`, or if you
490490
// do not want to use a shell. In all other cases, using Script might be
491491
// more convenient.
492492
Command []string
493493
// Args is a list of arguments that are provided to either Command,
494494
// Script or the Docker image's default entrypoint. The arguments are
495495
// placed immediately after the command to be run.
496496
Args []string
497-
// Script is a shell script to be run with `/bin/sh -c`. It may not be
497+
// Script is a shell script to be run with `/bin/sh -ic`. It may not be
498498
// specified with Command. Use Script when a shell script is appropriate
499499
// to execute the post build hook, for example for running unit tests
500-
// with "rake test". If you need control over the image entrypoint, or
501-
// if the image does not have "/bin/sh", use Command and/or Args.
500+
// with `rake test`. If you need control over the image entrypoint, or
501+
// if the image does not have `/bin/sh`, use Command and/or Args.
502+
// The `-i` flag is needed to support CentOS and RHEL images that use
503+
// Software Collections (SCL), in order to have the appropriate
504+
// collections enabled in the shell. E.g., in the Ruby image, this is
505+
// necessary to make `ruby`, `bundle` and other binaries available in
506+
// the PATH.
502507
Script string
503508
}
504509

pkg/build/api/v1/types.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ type SourceBuildStrategy struct {
424424
// The above is a convenient form which is equivalent to:
425425
//
426426
// BuildPostCommitSpec{
427-
// Command: []string{"/bin/sh", "-c"},
427+
// Command: []string{"/bin/sh", "-ic"},
428428
// Args: []string{"rake test --verbose"},
429429
// }
430430
//
@@ -469,19 +469,24 @@ type SourceBuildStrategy struct {
469469
// the fields are specified, the hook is not executed.
470470
type BuildPostCommitSpec struct {
471471
// Command is the command to run. It may not be specified with Script.
472-
// This might be needed if the image doesn't have "/bin/sh", or if you
472+
// This might be needed if the image doesn't have `/bin/sh`, or if you
473473
// do not want to use a shell. In all other cases, using Script might be
474474
// more convenient.
475475
Command []string `json:"command,omitempty" description:"command to be executed in a container running the build output image replacing the image's entrypoint"`
476476
// Args is a list of arguments that are provided to either Command,
477477
// Script or the Docker image's default entrypoint. The arguments are
478478
// placed immediately after the command to be run.
479479
Args []string `json:"args,omitempty" description:"arguments to command, script or the default image entrypoint"`
480-
// Script is a shell script to be run with `/bin/sh -c`. It may not be
480+
// Script is a shell script to be run with `/bin/sh -ic`. It may not be
481481
// specified with Command. Use Script when a shell script is appropriate
482482
// to execute the post build hook, for example for running unit tests
483-
// with "rake test". If you need control over the image entrypoint, or
484-
// if the image does not have "/bin/sh", use Command and/or Args.
483+
// with `rake test`. If you need control over the image entrypoint, or
484+
// if the image does not have `/bin/sh`, use Command and/or Args.
485+
// The `-i` flag is needed to support CentOS and RHEL images that use
486+
// Software Collections (SCL), in order to have the appropriate
487+
// collections enabled in the shell. E.g., in the Ruby image, this is
488+
// necessary to make `ruby`, `bundle` and other binaries available in
489+
// the PATH.
485490
Script string `json:"script,omitempty" description:"shell script to be executed in a container running the build output image"`
486491
}
487492

pkg/build/api/v1beta3/types.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ type SourceBuildStrategy struct {
411411
// The above is a convenient form which is equivalent to:
412412
//
413413
// BuildPostCommitSpec{
414-
// Command: []string{"/bin/sh", "-c"},
414+
// Command: []string{"/bin/sh", "-ic"},
415415
// Args: []string{"rake test --verbose"},
416416
// }
417417
//
@@ -456,19 +456,24 @@ type SourceBuildStrategy struct {
456456
// the fields are specified, the hook is not executed.
457457
type BuildPostCommitSpec struct {
458458
// Command is the command to run. It may not be specified with Script.
459-
// This might be needed if the image doesn't have "/bin/sh", or if you
459+
// This might be needed if the image doesn't have `/bin/sh`, or if you
460460
// do not want to use a shell. In all other cases, using Script might be
461461
// more convenient.
462462
Command []string `json:"command,omitempty" description:"command to be executed in a container running the build output image replacing the image's entrypoint"`
463463
// Args is a list of arguments that are provided to either Command,
464464
// Script or the Docker image's default entrypoint. The arguments are
465465
// placed immediately after the command to be run.
466466
Args []string `json:"args,omitempty" description:"arguments to command, script or the default image entrypoint"`
467-
// Script is a shell script to be run with `/bin/sh -c`. It may not be
467+
// Script is a shell script to be run with `/bin/sh -ic`. It may not be
468468
// specified with Command. Use Script when a shell script is appropriate
469469
// to execute the post build hook, for example for running unit tests
470-
// with "rake test". If you need control over the image entrypoint, or
471-
// if the image does not have "/bin/sh", use Command and/or Args.
470+
// with `rake test`. If you need control over the image entrypoint, or
471+
// if the image does not have `/bin/sh`, use Command and/or Args.
472+
// The `-i` flag is needed to support CentOS and RHEL images that use
473+
// Software Collections (SCL), in order to have the appropriate
474+
// collections enabled in the shell. E.g., in the Ruby image, this is
475+
// necessary to make `ruby`, `bundle` and other binaries available in
476+
// the PATH.
472477
Script string `json:"script,omitempty" description:"shell script to be executed in a container running the build output image"`
473478
}
474479

pkg/build/builder/common.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,12 @@ func execPostCommitHook(client DockerClient, postCommitSpec api.BuildPostCommitS
135135
glog.V(4).Infof("Post commit hook spec: %+v", postCommitSpec)
136136

137137
if script != "" {
138-
command = []string{"/bin/sh", "-c"}
138+
// The `-i` flag is needed to support CentOS and RHEL images
139+
// that use Software Collections (SCL), in order to have the
140+
// appropriate collections enabled in the shell. E.g., in the
141+
// Ruby image, this is necessary to make `ruby`, `bundle` and
142+
// other binaries available in the PATH.
143+
command = []string{"/bin/sh", "-ic"}
139144
args = append([]string{script, command[0]}, args...)
140145
}
141146

0 commit comments

Comments
 (0)