在Gitlab CI中将通知推送到Rocket.Chat

时间:2018-08-10 14:06:26

标签: gitlab gitlab-ci rocket.chat

我正在尝试通过.gitlab-ci.yml文件设置到Rocket.Chat服务器的通知。我的测试和部署阶段正在运行,但通知阶段正在出错。我遵循了here的说明,但是我调整了通知脚本,使其可以与Rocket.Chat而不是Pushbullet一起使用。

这是我的.gitlab-ci.yml:

stages:
  - test
  - deploy
  - notify

test:
  stage: test
  image: homeassistant/amd64-homeassistant
  script:
    - hass --script check_config -c .

deploy:
  stage: deploy
  only:
    - master
  before_script:
    - 'which ssh-agent || ( apk update && apk add openssh-client )'
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
  script:
    - ssh $DEPLOY_USER@$DEPLOY_HOST "cd '$DEPLOY_PATH'; git pull; sudo systemctl restart home-assistant@homeassistant"

notify_success:
  stage: notify
  allow_failure: true
  only:
    - master
  script: 
    - curl -X POST -H 'Content-Type: application/json' --data '{"text":"New Hass config deployed successfully!"}' https://chat.bryantgeeks.com/hooks/$ROCKET_CHAT_TOKEN

notify_fail:
  stage: notify
  allow_failure: true
  only:
    - master
  when: on_failure
  script: 
    - curl -X POST -H 'Content-Type: application/json' --data '{"text":"New Hass config failed. Please check for errors!"}' https://chat.bryantgeeks.com/hooks/$ROCKET_CHAT_TOKEN

我在CI Lint中收到此错误:

  

状态:语法不正确

     

错误:jobs:notify_success:script配置应该为字符串或字符串数​​组

如果将通知脚本行更改为单引号('),则会在CI Lint中收到以下错误:

  

状态:语法不正确

     

错误:():在第33行的第7列中解析块映射时未找到预期的键

如果我尝试在脚本行(“)处使用双引号,则会出现以下错误:

  

状态:语法不正确

     

错误:():在分析第33行第5列的数据块集合时找不到预期的'-'指示器

我不确定该如何纠正该问题,还可以尝试其他方法或从何处着眼。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

YAML确实不喜欢字符串中的:。罪魁祸首是:

中的'Content-Type: application/json'

有时使用多行字符串格式会有所帮助,例如:

notify_success:
  stage: notify
  allow_failure: true
  only:
    - master
  script: |
    curl -X POST -H 'Content-Type: application/json' --data '{"text":"New Hass config deployed successfully!"}' https://chat.bryantgeeks.com/hooks/$ROCKET_CHAT_TOKEN