|
| 1 | +# 通过GithubAction将内容部署到vps |
| 2 | + |
| 3 | +## 创建Action文件 |
| 4 | +在项目根目录创建 `.github/workflows/ci.yml` |
| 5 | + |
| 6 | +```yml |
| 7 | +# 标题 |
| 8 | +name: GitHub Actions 部署 vitepress 博客 |
| 9 | +# 触发时机:表示推送master分支时触发 |
| 10 | +on: |
| 11 | + push: |
| 12 | + branches: |
| 13 | + - master |
| 14 | +# 任务列表 |
| 15 | +jobs: |
| 16 | + build-and-deploy: |
| 17 | + # 运行环境 |
| 18 | + runs-on: ubuntu-latest |
| 19 | + # 步骤 |
| 20 | + steps: |
| 21 | + # 步骤1:安装Node.js |
| 22 | + - name: Setup Node.js |
| 23 | + uses: actions/setup-node@v4 |
| 24 | + with: |
| 25 | + node-version: 20 |
| 26 | + # 步骤2:检出代码 |
| 27 | + - name: Checkout |
| 28 | + uses: actions/checkout@v4 |
| 29 | + # 步骤3:安装yarn |
| 30 | + - name: Install yarn |
| 31 | + run: npm install -g yarn |
| 32 | + # 步骤4:安装依赖 |
| 33 | + - name: Install dependencies |
| 34 | + run: yarn install |
| 35 | + # 步骤5:构建VitePress |
| 36 | + - name: Build VitePress |
| 37 | + run: yarn run docs:build |
| 38 | + # 步骤6:部署到Github Pages |
| 39 | + - name: Deploy to Github Pages |
| 40 | + uses: peaceiris/actions-gh-pages@v3 |
| 41 | + with: |
| 42 | + github_token: ${{ secrets.VITEPRESS_BLOG_TOKEN }} # 自定义的action环境变量 |
| 43 | + publish_dir: .vitepress/dist # 指定部署目录 |
| 44 | + publish_branch: gh-pages # 指定部署分支 |
| 45 | + dotfiles: true # 允许使用.gitignore文件 |
| 46 | +``` |
| 47 | +
|
| 48 | +## 配置 Action 环境变量 |
| 49 | +
|
| 50 | +1. 右上角头像 => `Settings` => `Developer Settings` => `Personal access tokens` => `Tokens(classic)` => `Generate new token` |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +::: warning 注意 |
| 55 | +存储生成的 `token` ,请妥善保管,永远只出现一次。 |
| 56 | +::: |
| 57 | + |
| 58 | +## 仓库添加环境变量 |
| 59 | +1. 打开仓库主页 => `Settings` => `Secrets and variables` => `Actions` => `New repository secret` => 填入变量名和 `token` |
| 60 | + |
| 61 | +1. 推送本地代码,`github action` 检测到 `ci.yml` 文件,自动触发部署,将编译后的 `dist` 部署到 `gh-pages` 分支 |
| 62 | + |
| 63 | +## 配置github pages |
| 64 | + |
| 65 | +1. 打开仓库主页 => `Settings` => `Pages` => `Source` => 选择 `gh-pages` 分支 => `Save` |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +## 参考 |
| 73 | +1. [GitHub Actions 入门教程](https://www.ruanyifeng.com/blog/2019/09/getting-started-with-github-actions.html) |
| 74 | +1. [Github 管理个人访问令牌](https://docs.github.com/zh/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens) |
| 75 | +1. [通过 Github Action 将 GitHub 上的内容部署到 VPS](https://tourcoder.com/deploy-from-github-to-vps-via-github-action/) |
| 76 | +1. [使用 GitHub Actions 在代码更新时自动部署代码到 VPS](https://blog.csdn.net/m0_57236802/article/details/134216395) |
| 77 | +1. [vitepress项目使用github的action自动部署到github-pages中](https://blog.csdn.net/weixin_43972992/article/details/135123018) |
0 commit comments