惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
N
News and Events Feed by Topic
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Engineering at Meta
Engineering at Meta
T
Tailwind CSS Blog
博客园_首页
Microsoft Azure Blog
Microsoft Azure Blog
Y
Y Combinator Blog
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
月光博客
月光博客
A
About on SuperTechFans
I
InfoQ
S
Securelist
Last Week in AI
Last Week in AI
S
Schneier on Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hacker News: Ask HN
Hacker News: Ask HN
Schneier on Security
Schneier on Security
Know Your Adversary
Know Your Adversary
腾讯CDC
大猫的无限游戏
大猫的无限游戏
S
Security @ Cisco Blogs
博客园 - 三生石上(FineUI控件)
Simon Willison's Weblog
Simon Willison's Weblog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tor Project blog
美团技术团队
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
罗磊的独立博客
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
S
Secure Thoughts
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Latest news
Latest news
Recent Announcements
Recent Announcements
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
L
LINUX DO - 热门话题
Security Latest
Security Latest
TaoSecurity Blog
TaoSecurity Blog
Cyberwarzone
Cyberwarzone
有赞技术团队
有赞技术团队

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19) 使用 Velero 备份 Kubernetes 集群 Kubernetes Cheat Sheet 开发 Tips(18) 如何构建一个 Java 工程 开发 Tips(17) KubeSpray 安装 Kubernetes 报错 ip in ansible_all_ipv4_addresses 基于 Kubernetes 和 Jenkins 搭建自动化测试系统 在 Kubernetes 上动态创建 Jenkins Slave 使用 Jenkins 进行服务拨测 开发 Tips(16) Kubernetes 签发 Ingress 证书及日常故障运维 Kubernetes 中 Deployment 的基本操作 Kubernetes 中的证书 如何使用 KubeBuilder 开发一个 Operator Kubernetes 1.6.0 安装问题汇总 镜像管理工具 -- Harbor 开发 Tips(15) Docker 如何拉取镜像 开发 Tips(14) 使用 Helm 安装 harbor 开发 Tips(13) 使用 S2I 构建云原生应用 在 Kubernetes 中使用 emptyDir、hostPath、localVolume 开发 Tips(12) 开发 Tips(11) 代码质量分析工具 SonarQube 使用 Kubeadm 安装 Kubernetes 集群 一起来学 Go --(4)常用函数 Kubernetes 中的 Ceph Kubernetes 之 Volumes Kubernetes 之 Labels、Selectors 开发 Tips(10) 开源正在重构商业模式 Kubernetes 之网络 Kubernetes 之 API 使用 Helm 和 Operator 快速部署 Prometheus Kubernetes 复杂有状态应用管理框架 -- Operator Kubernetes 的包管理器 -- Helm 一起来学 Go --(3)Go Modules 如何一步一步地优化博客方案 kubectl 实用指南 Kubernetes 中的基本概念 搭建远程 Kubernetes 开发环境 大公司和小公司的 ToB 思路 开发 Tips(9) Go 入门指南 一起来学 Go --(2)数据与逻辑结构 如何预防 Web 富文本中的 XSS 攻击 django-xss-cleaner 云工作时代 一起来学 Go --(1)背景与特点 SaaS 开发团队的不同阶段 你不知道的 Git 使用技巧 输出既服务 微服务设计 继续奔跑 开发 Tips(8) 从账户安全到二次验证 Django 性能之数据库查询优化 Django 性能之分库分表 敏捷开发之研发流程 打造一致性的团队 开发 Tips(7) Pytest 进阶学习之 Mock PaaS 部署之 buildpack Go 开发配置 领域输出才是 PaaS 的核心竞争力 Pytest 入门学习 开发 Tips(6) 如何使用 Jenkins、Docker、GitLab 搭建 Django 自动化部署流程
GitHub Actions 三步教你打造 ChatOps 系统
微信公众号 · 2021-01-06 · via 陈少文的网站

在上一篇文档 《使用 ChatOps 改进研发流程》中,通过 ChatOps 给 Pull Requests 提供预览链接提升了敏捷速度。本篇主要描述如何快速实现这个功能。

1. 第一步: 配置一个触发器

1.1 选择一个触发器

在 GitHub 中有三类 Workflows 触发器,定时、人工、自动触发器。我们需要选择一个自动触发器,用于触发执行逻辑。目前 GitHub Actions 支持的自动触发器有 check_run、check_suite、create、delete、deployment、deployment_status、fork、gollum、issue_comment、issues、label、milestone、page_build、project、project_card、project_column、public、pull_request、pull_request_review、pull_request_review_comment、pull_request_target、push、registry_package、release、status、watch、workflow_run。

在 workflows.yaml 文件中,on 关键字下添加触发器:

1
2
3
on:
  issue_comment:
    types: [created]

这里选择的是 issue_comment ,当有 Issues 或者 Pull Requests 被评论时,触发 Actions 的执行。

1.2 定义一个触发关键字

定义关键字是为了关联操作逻辑,主要是匹配过滤,将 /deploydeploy-script 关联起来,将 /clearclear-script 关联起来。

下面是根据指定 trigger 关键字 /deploy 进行触发的 workflows.yaml 片段。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
jobs:
  deploy-check:
    runs-on: ubuntu-latest
    steps:
      - name: acknowledge request to commenter
        id: check
        uses: khan/pull-request-comment-trigger@master
        with:
          trigger: "/deploy"
          reaction: rocket
        env:
          GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
    outputs:
      triggered: ${{ steps.check.outputs.triggered }}

  deploy-script:
    if: needs.deploy-check.outputs.triggered == 'true'
    runs-on: ubuntu-latest
    needs: deploy-check
    steps:
      - name: script
        run: |
                    echo "start"

2. 第二步: 执行自定义指令

这一步主要是执行一些相应的步骤,用来完成对基础设施的修改。比如,更新服务、部署应用、删除文件等,具有很高的自由度。

这里主要是需要部署新的服务,并提供一个对外可访问的地址。如果是静态项目的预览,直接使用 netlify 提供的预览服务更好。但是,更多的项目,还是需要远程到服务器上进行操作。

下面是一个以 root 用户远程到指定服务器,创建并暴露服务的示例。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
    - name: executing remote ssh commands
    id: deploy_console
    uses: appleboy/ssh-action@master
    with:
        host: ${{ secrets.IP }}
        username: root
        password: ${{ secrets.PASSWORD }}
        port: 22
        script: |
        kubectl run nginx --image=nginx
        kubectl expose deploy nginx --type=NodePort --port=80 --target-port=80        

可以根据具体需要,在 script 中填充自定义的脚本命令。

3. 第三步: 反馈信息

最后就是需要反馈信息,明确表明任务已经完成,并将完成的结果以某种方式告诉给用户。这里主要提供两种方式:评论回复和 Slack 通知。

3.1 评论回复

添加相关的 Job 可以实现回复指定的 Issues/Pull Requests 的功能。同时,在评论的内容中,我们也可以引用内置的变量,让回复内容更加详细,含义传达更加有效。

1
2
3
4
5
6
7
    - name: Create comment
    uses: peter-evans/create-or-update-comment@v1
    with:
        issue-number: ${{ github.event.issue.number }}
        body: |
        Congratulations! Deployment succeeded.
        reactions: heart, hooray, laugh        

3.2 Slack 通知

在之前的文档中,我已经分享过《如何配置 Slack 通知》 ,这里不再赘述。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
    - uses: 8398a7/action-slack@v3
    with:
        status: custom
        fields: workflow,job,commit,repo,ref,author,took
        custom_payload: |
        {
            username: 'action-slack',
            icon_emoji: ':octocat:',
            attachments: [{
            color: '${{ job.status }}' === 'success' ? 'good' : '${{ job.status }}' === 'failure' ? 'danger' : 'warning',
            text: `Congratulations! Deployment succeeded.`,
            }]
        }        
    env:
        SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

将上面的片段添加到 workflows 即可。

4. 一些 GitHub Actions 的使用技巧

4.1 切换代码到 Pull Requests 所在分支

在一个 Pull Requests 流程中,通常有两个分支,upstream 和 developer 。而 workflows 的 on 针对当前分支,在 upstream 仓库中,不能直接 Checkout 提交 Pull Requests 的 developer 仓库的分支。

下面这段片段,可以将代码切换到待合并的开发者分支上。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
- name: get pull request ref
  id: get_pull_request_ref
  uses: octokit/request-action@v2.x
  with:
    route: GET /repos/:repository/pulls/:issue_id
    repository: ${{ github.repository }}
    issue_id: ${{ github.event.issue.number }}
  env:
    GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
- uses: actions/checkout@v2
  with:
    repository: ${{ fromJson(steps.get_pull_request_ref.outputs.data).head.repo.full_name }}
    ref: ${{ fromJson(steps.get_pull_request_ref.outputs.data).head.ref }}

4.2 使用 Deployment 维护部署的状态

GitHub 提供了一个名为 Deployment 的对象,用来管理部署的事件和状态。下面是一个时序图:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
+---------+             +--------+            +-----------+        +-------------+
| Tooling |             | GitHub |            | 3rd Party |        | Your Server |
+---------+             +--------+            +-----------+        +-------------+
     |                      |                       |                     |
     |  Create Deployment   |                       |                     |
     |--------------------->|                       |                     |
     |                      |                       |                     |
     |  Deployment Created  |                       |                     |
     |<---------------------|                       |                     |
     |                      |                       |                     |
     |                      |   Deployment Event    |                     |
     |                      |---------------------->|                     |
     |                      |                       |     SSH+Deploys     |
     |                      |                       |-------------------->|
     |                      |                       |                     |
     |                      |   Deployment Status   |                     |
     |                      |<----------------------|                     |
     |                      |                       |                     |
     |                      |                       |   Deploy Completed  |
     |                      |                       |<--------------------|
     |                      |                       |                     |
     |                      |   Deployment Status   |                     |
     |                      |<----------------------|                     |
     |                      |                       |                     |

在页面上的效果如下图:

使用的方法,可以参考文末提供的链接。

我最终没有采用这种方式的原因是,在 Pull Requests 下,upstream 不存在 developer 的 PR 分支时,页面找不到 Deployment。好吧,我知道你没明白,不过没关系,推荐用这种方式管理 Deployment,可以先试试。

4.3 变量的调试方法

在 GitHub Actions 中内置了很多变量,使用第三方 Actions 也会得到很多变量。在调试过程中,如果对变量存在性或值内容存在疑惑,可以直接打印查看。

下面就是一个示例:

1
2
3
- name: echo
  run: |
        echo ${{ steps.get_pull_request_ref.outputs.data }}

最终打印的部分结果如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
    "url": "https://api.github.com/repos/***/console/pulls/7",
    "id": 548619025,
    "node_id": "MDExOlB1bGxSZXF1ZXN0NTQ4NjE5MDI1",
    "html_url": "https://github.com/***/console/pull/7",
    "diff_url": "https://github.com/***/console/pull/7.diff",
    "patch_url": "https://github.com/***/console/pull/7.patch",
    "issue_url": "https://api.github.com/repos/***/console/issues/7",
    "number": 7,
    "state": "open",
    "locked": false,
    "title": "Feat/workspace group",
    ...
}

在整个过程中,调试是非常耗时的,我们要掌握一定的方法,才能事半功倍。

4.4 Output 传递 Step 之间的变量

使用 Output 和 Env 都可以实现 Step 之间的变量传递,这里以 Output 为例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
- name: set SERVICE_PORT
  id: set_port
  run: |
        echo "::set-output name=SERVICE_PORT::$(sshpass  -p "${{ secrets.QING_PASSWORD }}" ssh -o StrictHostKeyChecking=no root@${{ secrets.EIP }} "kubectl get svc ${{ env.SERVICE_NAME }} -o json | jq .spec.ports[].nodePort")"
- name: Create comment
  uses: peter-evans/create-or-update-comment@v1
  with:
    issue-number: ${{ github.event.issue.number }}
    body: |
            Congratulations! Deployment succeeded. This is the [preview link](http://${{ secrets.EIP }}:${{ steps.set_port.outputs.SERVICE_PORT }}/) .
    reactions: heart, hooray, laugh

首先通过执行 sshpass 命令拿到服务的端口,设置为 output 变量。然后,在其他的 Step 中,通过 ${{ steps.set_port.outputs.SERVICE_PORT }} 的方式,引用跨 Step 的变量值。

5. 参考