Gitlab Runner为docker执行器折叠多行命令

4

gitlab-runner 使用 docker 执行器和我构建的镜像时,其中一个 gitlab ci 作业出现了错误。

以下是在 gitlab-ci.yml 中失败的作业。

image:
  name: 19950818/banu-terraform-ansible-cicd
.
.
.
create-ssh-key-pair:
  stage: create-ssh-key-pair
  script:
    - pwd
    - mkdir -p ~/.ssh

    # below lines gives the error
    - |
      # !/bin/bash
      FILE=~/.ssh/id_rsa
      if [ -f "$FILE" ]; then
        echo "$FILE exists."
      else 

        ssh-keygen -q -t rsa -N '' -f ~/.ssh/id_rsa 2>/dev/null <<< y >/dev/null
      fi

但是当执行器为shell时,这些行不会出现错误

这是镜像19950818/banu-terraform-ansible-cicdDockerfile

FROM centos:7

ENV VER "0.12.9"

RUN yum update -y && yum install wget -y && yum install unzip -y
RUN yum install epel-release -y && yum install ansible -y

RUN wget https://releases.hashicorp.com/terraform/${VER}/terraform_${VER}_linux_amd64.zip
RUN unzip terraform_${VER}_linux_amd64.zip
RUN mv terraform /usr/local/bin/  
RUN rm -rf terraform_${VER}_linux_amd64.zip

请问有人能告诉我发生了什么事情以及如何解决这个问题吗?

我的疑问是ssh-keygen -q -t rsa -N '' -f ~/.ssh/id_rsa 2>/dev/null <<< y >/dev/null这一行是否导致了错误。


2
如果有其他人在这里寻找如何扩展折叠的多行命令,我在这里找到了答案:使用set -xv;虽然不完美,但它确实可以帮助显示更多用于调试的行。 - ctwheels
2个回答

8

https://gitlab.com/gitlab-org/gitlab-runner/-/issues/166 - Aruy Aruy
1
请编辑您的回答,并提供相关细节,说明这段代码/链接如何帮助解决问题。 - Nowhere Man

1
# below lines gives the error
- |
  # !/bin/bash
  FILE=~/.ssh/id_rsa
  if [ -f "$FILE" ]; then
    echo "$FILE exists."
  else 

    ssh-keygen -q -t rsa -N '' -f ~/.ssh/id_rsa 2>/dev/null <<< y >/dev/null
  fi

尽管命令中有 # !/bin/bash,但该部分命令很可能被/bin/sh解析。脚本的各个部分将传递到容器的入口点,即/bin/sh -c,并且它将读取第一行作为注释。如果将其作为要运行的脚本传递,则至少需要删除空格,因此为#!/bin/bash,但我怀疑根据主机gitlab调用脚本并与其他要运行的脚本合并,它仍将被视为注释。
为什么会在/bin/sh下出现问题呢?<<< y是bash特定的语法。可以将其更改为:
echo y | ssh-keygen -q -t rsa -N '' -f ~/.ssh/id_rsa 2>/dev/null >/dev/null

如果您想从命令中查看错误消息以便于调试,则需要消除输出重定向。
echo y | ssh-keygen -q -t rsa -N '' -f ~/.ssh/id_rsa

如果您确实希望出于其他原因使用bash,则更改镜像的入口点:

image:
  name: 19950818/banu-terraform-ansible-cicd
  entrypoint: ["/bin/bash", "-c"]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接