簡體   English   中英

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

[英]How can I add multi-line bash EOD command to gitlab-ci.yml?

這個問題被問過多次,但大多數都很容易解決,盡管使用工具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 時以多行方式執行此命令?

終於找到了

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

這種語法稱為heredoc ,以防有人想要進一步研究的正確名稱。

@holms 已經找到了他們的解決方案,但我想將此鏈接添加到涵蓋整個主題的 YAML 中多行條目的幫助程序。
有幾種方法可以在 YAML 中進行多行條目。 此工具可幫助您找到符合您需求的工具。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM