如何在YAML文件中填充bash脚本

时间:2019-03-15 10:24:38

标签: bash yaml gitlab-ci

我正在尝试创建一个GitLab CI / CD作业,该作业从YAML文件中获取一个bash脚本,并检查语法是否正确。

我有一个名为.gitlab-ci.template.yml的YAML文件,其内容如下:

image: node:10.15.3-alpine

stages:
  - deploy

deploy:
  stage: deploy
  script:
    - apk_install
    - yarn_install
    - build
    - deploy

.bash: &bash |
  function apk_install() {
    apk add jq git curl grep
  }

  function yarn_install() {
     yarn
  }
  function build() {
    echo "Build"

    if [ -f ".gitlab-ci.template.yml" ]; then
       echo "Just a dumb test for more lines"
    fi 
  }

  function deploy() {
    echo "Deploy"
  }

before_script:
  - *bash

我想参加bash部分并进行测试。我安装了shyaml以便从YAML文件中获取值,就像这样:

FILE=`cat .gitlab-ci.template.yml | shyaml get-value before_script`

$FILE的内容为:

- "function apk_install() {\n apk add jq git curl grep\n}\n\nfunction yarn_install()\ \ {\n yarn\n}\nfunction build() {\n echo \"Build\"\n \n if [ -f \".gitlab-ci.template.yml\"\ \ ]; then\n echo \"Just a dumb test for more lines\"\n fi\n}\n\nfunction deploy()\ \ {\n echo \"Deploy\"\n}\n"

然后使用以下一些命令,我​​再次尝试获取有效文件:

echo $FILE | xargs -0 printf '%b\n' | sed 's/\\[[:space:]]\\[[:space:]]/ /g' | sed 's/\\"/"/g' | sed 's/\\[[:space:]]//g'

现在,我可以从开头和结尾删除一些字符。但我想知道会有更好/更简便的方法吗?

2 个答案:

答案 0 :(得分:1)

您是否考虑过使用yq?

这是一个将shell脚本从文件中删除的表达式:

yq -r '.[".bash"]' .gitlab-ci.template.yml 

结果是实际的脚本。现在,您只需要将其通过管道传送到bash短绒。我为您浏览了该站点,但找不到bash帖子(YMMV)中经常出现的bash语法解析器。我确实通过Google找到了ShellChecker,但是我没有进行全面评估(因此,请根据您的意愿使用)。

最终,您的代码可能看起来像这样:

yq -r '.[".bash"]' .gitlab-ci.template.yml | ShellChecker # or use your favorite bash linter

答案 1 :(得分:0)

仅注意到我应该从 public class PopUpFragment extends BottomSheetDialogFragment { @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState); dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(final DialogInterface dialog) { BottomSheetDialog d = (BottomSheetDialog) dialog; bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet); BottomSheetBehavior mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View view, int i) { switch (i){ case BottomSheetBehavior.STATE_HIDDEN: dialog.dismiss(); break; default: break; } } @Override public void onSlide(@NonNull View view, float v) { setScrim(v); } }); } }); // Do something with your dialog like setContentView() or whatever return dialog; } private void setScrim(float slideOffset) { // bottomSheet.getForeground().setAlpha((int) (slideOffset * 150)); } public static PopUpFragment newInstance() { PopUpFragment fragment = new PopUpFragment(); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.your_layou, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } } PopUpFragment bottomDialogFragment = PopUpFragment.newInstance(); bottomDialogFragment.show(getChildFragmentManager(),"PopUpFragment"); 来首次出现。所以我改变了:

before_script

收件人:

shyaml

将按原样获取脚本。