BING
斯是陋室,唯吾芳馨
原创

GitHub Actions 能干啥

共 3,522 字,需阅读 9 分钟2019/10/09 上午5,529 次阅读

我已经用上 Actions 了,在我看来它不仅仅是一个 CI 工具,它更像一个「 工作流控制器 」。

你可以把它打造为 CI、Bot、跳板机、文章发布器、Issues 统计器...任何你需要的工具或平台。

Actions 事件继承于之前的 API/Webhooks,自身提供了 Docker 环境,这意味着你拥有了无限编程能力的 “工作流定制权”。

比如,在实际的使用中,我的 angular-admin (web)、nodepress (nodejs) 两个项目的部署流程,

前者在 Actions 中的流程为:

测试(前提) -> 编译 -> 打包 -> 压缩 -> 通过 scp 将压缩包传给生产机 -> 执行生产机部署脚本 -> 完成部署

代码:

          
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
name: Deploy to server on: pull_request: types: [closed] branches: - master jobs: deploy: if: github.event.pull_request.merged == true name: Deploy to server runs-on: ubuntu-latest strategy: matrix: node-version: [10.x] steps: - name: Checkout uses: actions/checkout@master with: ref: master - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - name: Install Dependencies run: npm ci - name: Build run: npm run build - name: ls and pwd run: | pwd ls -la - name: Compress dist files run: tar -zcvf ./dist.tar.gz -C ./dist/angular-admin/ . - name: Copy file to server uses: appleboy/scp-action@master with: timeout: 3m overwrite: true host: ${{ secrets.HOST }} username: ${{ secrets.USER }} password: ${{ secrets.PASSWORD }} source: ./dist.tar.gz target: ${{ secrets.WWW_PATH }}/angular-admin/product/ - name: Exec deploy script with SSH uses: appleboy/ssh-action@master with: command_timeout: 3m host: ${{ secrets.HOST }} username: ${{ secrets.USER }} password: ${{ secrets.PASSWORD }} script: | sh ${{ secrets.WWW_PATH }}/angular-admin/.deploy/main.sh

后者在 Actions 中的流程为:

测试 -> 编译 -> 构建测试 -> 执行生产机部署脚本 -> 完成部署

代码:

          
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
name: Deploy to server on: pull_request: types: [closed] branches: - master jobs: deploy: if: github.event.pull_request.merged == true name: Deploy to server runs-on: ubuntu-latest steps: - name: Exec deploy script with SSH uses: appleboy/ssh-action@master with: command_timeout: 10m host: ${{ secrets.HOST }} username: ${{ secrets.USER }} password: ${{ secrets.PASSWORD }} script: sh ${{ secrets.WWW_PATH }}/nodepress/.deploy/main.sh

部署脚本:

          
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
#!/bin/bash WEB_PATH=$(dirname $0) # WEB_USER='root' # WEB_USERGROUP='root' echo "[deploy] Start deployment..." echo "[deploy] Fetch and rebuilding..." cd $WEB_PATH cd .. echo "[deploy] path:" $(pwd) echo "[deploy] pulling source code..." git fetch --all && git reset --hard origin/master && git pull git checkout master # echo "changing permissions..." # chown -R $WEB_USER:$WEB_USERGROUP $WEB_PATH # chmod -R 777 $WEB_PATH # sync && echo 3 | sudo tee /proc/sys/vm/drop_caches echo "[deploy] Install dependencies..." npm ci echo "[deploy] Stop service..." pm2 stop nodepress echo "[deploy] Remove old dist..." rm -r ./dist/* echo "[deploy] Building..." npm run build echo "[deploy] Restarting..." pm2 restart nodepress echo "[deploy] Finished."

这一切都在 Actions 中完成,除去目标生产服务所必须的环境,你不需要额外多做什么; 反而,你可以将 工作流、部署、测试、生产代码... 等一系列上下游相关的配置或文件约束在同一个项目,甚至同一个目录内, 使项目始终紧密耦合在同一个单元内,减少维护成本。

所以,我已经把 SRE 服务 下线并废弃了。

祝使用愉快。

署名 - 非商业性使用 4.0 国际 https://surmon.me/article/157
2 / 2 条看法
访客身份
在下有一拙见,不知...
  • 帅
    🇨🇳CNShenzhenMac OSChrome

    去他妈的能点心不能取消?我只是试试功能啊,有点流氓了😂

    • Surmon
      Surmon🇨🇳CNBeijingMac OSChrome

      回复

      (清空缓存后还可以刷心