Skip to content

Commit

Permalink
Run post build hook with /bin/sh -ic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rhcarvalho committed Feb 22, 2016
1 parent 6061189 commit 46e7e91
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 16 deletions.
3 changes: 3 additions & 0 deletions examples/sample-app/application-template-dockerbuild.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@
"name": "origin-ruby-sample:latest"
}
},
"postCommit": {
"script": "bundle exec rake test"
},
"resources": {}
},
"status": {
Expand Down
3 changes: 3 additions & 0 deletions examples/sample-app/application-template-pullspecbuild.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@
"name": "origin-ruby-sample:latest"
}
},
"postCommit": {
"script": "bundle exec rake test"
},
"resources": {}
},
"status": {
Expand Down
3 changes: 3 additions & 0 deletions examples/sample-app/application-template-stibuild.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@
"name": "origin-ruby-sample:latest"
}
},
"postCommit": {
"script": "bundle exec rake test"
},
"resources": {}
},
"status": {
Expand Down
15 changes: 10 additions & 5 deletions pkg/build/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ type SourceBuildStrategy struct {
// The above is a convenient form which is equivalent to:
//
// BuildPostCommitSpec{
// Command: []string{"/bin/sh", "-c"},
// Command: []string{"/bin/sh", "-ic"},
// Args: []string{"rake test --verbose"},
// }
//
Expand Down Expand Up @@ -486,19 +486,24 @@ type SourceBuildStrategy struct {
// the fields are specified, the hook is not executed.
type BuildPostCommitSpec struct {
// Command is the command to run. It may not be specified with Script.
// This might be needed if the image doesn't have "/bin/sh", or if you
// This might be needed if the image doesn't have `/bin/sh`, or if you
// do not want to use a shell. In all other cases, using Script might be
// more convenient.
Command []string
// Args is a list of arguments that are provided to either Command,
// Script or the Docker image's default entrypoint. The arguments are
// placed immediately after the command to be run.
Args []string
// Script is a shell script to be run with `/bin/sh -c`. It may not be
// Script is a shell script to be run with `/bin/sh -ic`. It may not be
// specified with Command. Use Script when a shell script is appropriate
// to execute the post build hook, for example for running unit tests
// with "rake test". If you need control over the image entrypoint, or
// if the image does not have "/bin/sh", use Command and/or Args.
// with `rake test`. If you need control over the image entrypoint, or
// if the image does not have `/bin/sh`, use Command and/or Args.
// The `-i` flag is needed to support CentOS and RHEL images that use
// Software Collections (SCL), in order to have the appropriate
// collections enabled in the shell. E.g., in the Ruby image, this is
// necessary to make `ruby`, `bundle` and other binaries available in
// the PATH.
Script string
}

Expand Down
15 changes: 10 additions & 5 deletions pkg/build/api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ type SourceBuildStrategy struct {
// The above is a convenient form which is equivalent to:
//
// BuildPostCommitSpec{
// Command: []string{"/bin/sh", "-c"},
// Command: []string{"/bin/sh", "-ic"},
// Args: []string{"rake test --verbose"},
// }
//
Expand Down Expand Up @@ -469,19 +469,24 @@ type SourceBuildStrategy struct {
// the fields are specified, the hook is not executed.
type BuildPostCommitSpec struct {
// Command is the command to run. It may not be specified with Script.
// This might be needed if the image doesn't have "/bin/sh", or if you
// This might be needed if the image doesn't have `/bin/sh`, or if you
// do not want to use a shell. In all other cases, using Script might be
// more convenient.
Command []string `json:"command,omitempty" description:"command to be executed in a container running the build output image replacing the image's entrypoint"`
// Args is a list of arguments that are provided to either Command,
// Script or the Docker image's default entrypoint. The arguments are
// placed immediately after the command to be run.
Args []string `json:"args,omitempty" description:"arguments to command, script or the default image entrypoint"`
// Script is a shell script to be run with `/bin/sh -c`. It may not be
// Script is a shell script to be run with `/bin/sh -ic`. It may not be
// specified with Command. Use Script when a shell script is appropriate
// to execute the post build hook, for example for running unit tests
// with "rake test". If you need control over the image entrypoint, or
// if the image does not have "/bin/sh", use Command and/or Args.
// with `rake test`. If you need control over the image entrypoint, or
// if the image does not have `/bin/sh`, use Command and/or Args.
// The `-i` flag is needed to support CentOS and RHEL images that use
// Software Collections (SCL), in order to have the appropriate
// collections enabled in the shell. E.g., in the Ruby image, this is
// necessary to make `ruby`, `bundle` and other binaries available in
// the PATH.
Script string `json:"script,omitempty" description:"shell script to be executed in a container running the build output image"`
}

Expand Down
15 changes: 10 additions & 5 deletions pkg/build/api/v1beta3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ type SourceBuildStrategy struct {
// The above is a convenient form which is equivalent to:
//
// BuildPostCommitSpec{
// Command: []string{"/bin/sh", "-c"},
// Command: []string{"/bin/sh", "-ic"},
// Args: []string{"rake test --verbose"},
// }
//
Expand Down Expand Up @@ -456,19 +456,24 @@ type SourceBuildStrategy struct {
// the fields are specified, the hook is not executed.
type BuildPostCommitSpec struct {
// Command is the command to run. It may not be specified with Script.
// This might be needed if the image doesn't have "/bin/sh", or if you
// This might be needed if the image doesn't have `/bin/sh`, or if you
// do not want to use a shell. In all other cases, using Script might be
// more convenient.
Command []string `json:"command,omitempty" description:"command to be executed in a container running the build output image replacing the image's entrypoint"`
// Args is a list of arguments that are provided to either Command,
// Script or the Docker image's default entrypoint. The arguments are
// placed immediately after the command to be run.
Args []string `json:"args,omitempty" description:"arguments to command, script or the default image entrypoint"`
// Script is a shell script to be run with `/bin/sh -c`. It may not be
// Script is a shell script to be run with `/bin/sh -ic`. It may not be
// specified with Command. Use Script when a shell script is appropriate
// to execute the post build hook, for example for running unit tests
// with "rake test". If you need control over the image entrypoint, or
// if the image does not have "/bin/sh", use Command and/or Args.
// with `rake test`. If you need control over the image entrypoint, or
// if the image does not have `/bin/sh`, use Command and/or Args.
// The `-i` flag is needed to support CentOS and RHEL images that use
// Software Collections (SCL), in order to have the appropriate
// collections enabled in the shell. E.g., in the Ruby image, this is
// necessary to make `ruby`, `bundle` and other binaries available in
// the PATH.
Script string `json:"script,omitempty" description:"shell script to be executed in a container running the build output image"`
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/build/builder/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ func execPostCommitHook(client DockerClient, postCommitSpec api.BuildPostCommitS
glog.V(4).Infof("Post commit hook spec: %+v", postCommitSpec)

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

Expand Down

1 comment on commit 46e7e91

@richm
Copy link

@richm richm commented on 46e7e91 Feb 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes a problem for me: https://gist.github.com/richm/12d12aa1927b7ea955f4

I think it is because the bundle command is not in the default path, because the main build was done via scl

Please sign in to comment.