推粪上山
我们用尽所有力气,只为多推一点大粪上山
我已经用上 Actions 了,在我看来它不仅仅是一个 CI 工具,它更像一个「 工作流控制器 」。
你可以把它打造为 CI、Bot、跳板机、文章发布器、Issues 统计器...任何你需要的工具或平台。
Actions 事件继承于之前的 API/Webhooks,自身提供了 Docker 环境,这意味着你拥有了无限编程能力的 “工作流定制权”。
比如,在实际的使用中,我的 angular-admin (web)、nodepress (nodejs) 两个项目的部署流程,
前者在 Actions 中的流程为:
测试(前提) -> 编译 -> 打包 -> 压缩 -> 通过 scp 将压缩包传给生产机 -> 执行生产机部署脚本 -> 完成部署
代码:
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 中的流程为:
测试 -> 编译 -> 构建测试 -> 执行生产机部署脚本 -> 完成部署
代码:
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
部署脚本:
!/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 服务 下线并废弃了。
祝使用愉快。
去他妈的能点心不能取消?我只是试试功能啊,有点流氓了😂
回复:
(清空缓存后还可以刷心