4.3.2. 如何编写 Source-to-image 脚本
您可以使用任何编程语言编写 S2I 脚本,只要脚本可在构建器镜像中执行。S2I 支持多种提供 assemble/run/save-artifacts 脚本的选项。每次构建时按以下顺序检查所有这些位置:
- 构建配置中指定的脚本。
-
在应用程序源
.s2i/bin目录中找到的脚本。 -
在默认镜像 URL 中找到的带有
io.openshift.s2i.scripts-url标签的脚本。
镜像中指定的 io.openshift.s2i.scripts-url 标签和构建配置中指定的脚本都可以采用以下形式之一:
-
image:///path_to_scripts_dir:镜像中 S2I 脚本所处目录的绝对路径 -
file:///path_to_scripts_dir:主机上 S2I 脚本所处目录的相对或绝对路径 -
http(s)://path_to_scripts_dir:S2I 脚本所处目录的 URL
| 脚本 | 描述 |
|---|---|
|
|
|
|
|
|
|
|
这些依赖项会收集到一个 |
|
|
借助 |
|
|
借助
注意
建议将 |
S2I 脚本示例
以下示例 S2I 脚本采用 Bash 编写。每个示例都假定其 tar 内容解包到 /tmp/s2i 目录中。
assemble 脚本:
#!/bin/bash
# restore build artifacts
if [ "$(ls /tmp/s2i/artifacts/ 2>/dev/null)" ]; then
mv /tmp/s2i/artifacts/* $HOME/.
fi
# move the application source
mv /tmp/s2i/src $HOME/src
# build application artifacts
pushd ${HOME}
make all
# install the artifacts
make install
popd
run 脚本:
#!/bin/bash
# run the application
/opt/application/run.sh
save-artifacts 脚本:
#!/bin/bash
pushd ${HOME}
if [ -d deps ]; then
# all deps contents to tar stream
tar cf - deps
fi
popd
usage 脚本:
#!/bin/bash
# inform the user how to use the image
cat <<EOF
This is a S2I sample builder image, to use it, install
https://github.com/openshift/source-to-image
EOF