Gitlab CI是一个了不起的免费替代方案,可以让您流水运行管道。它在一个 docker 容器中运行,它派上用场,也给了我们超能力。

一旦你创建了一个管道,你希望一切都自动运行,并且只在它成功完成或失败时得到通知。

长话短说,让我们动手吧。

首先,我们需要一个管道来工作。这是一个运行在轻量级 Alpine docker 镜像上的超级简单的工作。

# .gitlab-ci.yml 

stages:
  - test
  - notification

test:
  image: alpine:latest
  stage: test
  script:
    - echo 🕵🏻 Checking...

进入全屏模式 退出全屏模式

下一步是配置我们的 Discord 频道以显示我们的通知。执行以下路径Your channel > Edit Channel > Integrations > Create Webhook,然后命名您的机器人并单击Copy Webhook URL。(Discord Webhook 指南)

在您的项目设置中转到 Gitlab CI 配置并添加一个新变量:settings > CI / CD > Variables,然后创建一个名为WEBHOOK_URL的新变量并将从 Discord 复制的 URL 粘贴为值。

现在我们将使用由DiscordHooks人创建的脚本。

那么我们的管道将如下所示:

# .gitlab-ci.yml 

stages:
  - test
  - notification

test:
  image: alpine:latest
  stage: test
  script:
    - echo 🕵🏻 Checking...

success_notification:
  image: alpine:latest
  stage: notification
  script:
    - apk add --update git curl
    - wget https://raw.githubusercontent.com/DiscordHooks/gitlab-ci-discord-webhook/master/send.sh
    - chmod +x send.sh
    - /bin/ash ./send.sh success $WEBHOOK_URL
  when: on_success

failure_notification:
  image: alpine:latest
  stage: notification
  script:
    - apk add --update git curl
    - wget https://raw.githubusercontent.com/DiscordHooks/gitlab-ci-discord-webhook/master/send.sh
    - chmod +x send.sh
    - /bin/ash ./send.sh failure $WEBHOOK_URL
  when: on_failure

进入全屏模式 退出全屏模式

现在只是放松和享受!在此处查看完整代码。

注意:不要缓存那些通知作业。

Logo

CI/CD社区为您提供最前沿的新闻资讯和知识内容

更多推荐