如何在gitlab-ci.yml中添加多行bash EOD命令?

时间:2018-06-27 12:05:01

标签: bash yaml gitlab-ci

这个问题被问了很多遍,但是大多数问题很容易解决,尽管使用工具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时以多行方式执行此命令?

3 个答案:

答案 0 :(得分:5)

终于找到了

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

答案 1 :(得分:0)

这种语法称为heredoc,以防万一有人想要适当的名称作进一步研究。

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

答案 2 :(得分:0)

使用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

相关问题