【问题标题】:How can I add multi-line bash EOD command to gitlab-ci.yml?如何将多行 bash EOD 命令添加到 gitlab-ci.yml?
【发布时间】:2018-12-06 07:48:15
【问题描述】:

这个问题被问了很多次,但大多数都很容易解决,尽管使用工具 expect 它并没有像我预期的那样工作:

/usr/bin/expect <<EOD
spawn npm adduser
expect {
  "Username:" {send "$USERNAME\r"; exp_continue}
  "Password:" {send "$PASSWORD\r"; exp_continue}
  "Email: (this IS public)" {send "$EMAIL\r"; exp_continue}
}
EOD

为了同样的目的,还有更简单的变体:

npm adduser <<!
$NPM_USERNAME
$NPM_PASSWORD
$NPM_EMAIL
!

.gitlab-ci.yml:这样会产生一个不好的行字符串,命令不起作用

npm_push:
  dependencies:
    - test
  script:
    - npm adduser <<!
      $NPM_USERNAME
      $NPM_PASSWORD
      $NPM_EMAIL
      !
    - npm config set registry https://$NPM_URL
    - npm push

如何以这种方式传递它,以便 gitlab-runner 在将它传递给 bash 时以多行方式执行该命令?

【问题讨论】:

    标签: bash yaml gitlab-ci


    【解决方案1】:

    终于找到了

    npm_publish:
      stage: deploy
      only:
        - master
      script:
        - apk update
        - apk add expect git alpine-sdk python python-dev
        - npm config set registry https://$NPM_URL
        - npm install publish
        - |
            /usr/bin/expect <<EOD
            spawn npm adduser
            expect {
                "Username:" {send "$NPM_USERNAME\r"; exp_continue}
                "Password:" {send "$NPM_PASSWORD\r"; exp_continue}
                "Email: (this IS public)" {send "$NPM_EMAIL\r"; exp_continue}
            }
            EOD
    

    【讨论】:

      【解决方案2】:

      此语法称为heredoc,以防有人想要进一步研究的正确名称。

      @holms 已经找到了他们的解决方案,但我想将此 link 添加到 YAML 中涵盖整个主题的多行条目的帮助器中。
      有几种方法可以在 YAML 中进行多行条目。此工具可帮助您找到符合您需求的工具。

      【讨论】:

        【解决方案3】:

        使用 YAML 块语法。

        npm_push:
          dependencies:
            - test
          script:
            - >
              npm adduser
              $NPM_USERNAME
              $NPM_PASSWORD
              $NPM_EMAIL
            - npm config set registry https://$NPM_URL
            - npm push
        

        https://docs.gitlab.com/ee/ci/yaml/#multiline-commands

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-22
          • 2020-04-05
          • 1970-01-01
          • 2020-07-06
          • 2022-07-14
          • 1970-01-01
          相关资源
          最近更新 更多