背景
软件工程课我们的大作业中,前端的 CI/CD 由我负责。以前用 Travis CI 做这些,但是现在我想尝试下 GitHub Actions。
我的需求是,GitHub 上的 Vue.js 项目在编译、测试通过后,部署到我自己的服务器(https://interview.yusanshi.com)上。
经过
服务器端配置过程如下。
1 2 3 4 5 6 7 8 9 10 11 12 13
| sudo useradd -m github sudo chown -R github:github <Website Dir> sudo su github cd ~
ssh-keygen -t rsa -b 4096 -C "github@yusanshi.com" cat .ssh/id_rsa.pub > .ssh/authorized_keys chmod 600 .ssh/authorized_keys
cat .ssh/id_rsa
rm .ssh/id_rsa .ssh/id_rsa.pub
|
在 Settings - Secrets 中添加 Secrets SSH_PRIVATE_KEY
和 TARGET_DIR
(刚才记录的私钥和网站部署的目录)。

编写 GitHub Actions 脚本。
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
| name: Node.js CI/CD
on: push: branches: [ master ]
jobs: build:
runs-on: ubuntu-latest
steps: - uses: actions/checkout@v2 - name: Use Node.js uses: actions/setup-node@v1 with: node-version: '12.x' - run: npm install - run: npm run build - name: Deploy to server env: SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} TARGET_DIR: ${{ secrets.TARGET_DIR }} run: | SSH_PATH="$HOME/.ssh" mkdir "$SSH_PATH" echo "$SSH_PRIVATE_KEY" > "$SSH_PATH/id_rsa" chmod 600 "$SSH_PATH/id_rsa" ssh-keyscan yusanshi.com >> "$SSH_PATH/known_hosts" rsync -az --delete -e "ssh -i $SSH_PATH/id_rsa" ./dist/ "github@yusanshi.com:$TARGET_DIR"
|