轻松搞定!使用 GitHub Actions 同时将 Hexo 部署到 GitHub 和 Coding
2024-01-31 11:05:44
在软件开发过程中,我们经常需要将代码或项目部署到不同的平台或代码仓库,例如将 Hexo 构建的静态博客同时部署到 GitHub 和 Coding,实现代码备份和多平台展示。GitHub Actions 作为一个强大的自动化工具,可以帮助我们轻松实现这一目标。它能够自动化构建、测试和部署流程,提高效率并减少错误。
GitHub Actions 的优势在于其自动化、可定制性和易用性。通过配置 GitHub Actions 工作流,我们可以设置在特定事件(如代码提交或合并)触发部署流程,无需手动干预。GitHub Actions 还提供了丰富的配置选项,方便我们根据实际需求定制部署过程。
要实现同时部署到 GitHub 和 Coding,我们需要使用两个独立的部署操作,分别负责将 Hexo 站点部署到对应的平台。步骤如下:
1. 创建 GitHub Actions 工作流文件
首先,我们需要在项目的 .github/workflows/
目录下创建一个名为 deploy.yml
的 YAML 文件,用于定义工作流。
name: Deploy to GitHub and Coding
on:
push:
branches:
- main
jobs:
deploy-github:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: 'latest'
- name: Build and deploy
run: |
hexo generate
hexo deploy
deploy-coding:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: 'latest'
- name: Build and deploy
run: |
hexo generate
hexo deploy --config coding.yaml
在这个工作流文件中,我们定义了两个作业:deploy-github
和 deploy-coding
。deploy-github
负责将 Hexo 站点部署到 GitHub Pages,deploy-coding
负责部署到 Coding Pages。每个作业都指定了运行环境(runs-on: ubuntu-latest
)和一系列步骤。
2. 配置 Coding Pages 部署参数
对于 deploy-coding
作业,我们需要创建一个名为 coding.yaml
的 Hexo 部署配置文件,用于指定 Coding Pages 的部署设置,例如仓库名称、分支名称等。
# coding.yaml
deploy:
type: coding
repo: <your-coding-username>/<your-coding-repo-name>
branch: <your-coding-branch-name>
message: "Deploy to Coding Pages"
请将 <your-coding-username>
、<your-coding-repo-name>
和 <your-coding-branch-name>
替换成你的 Coding 账户信息和仓库信息。
3. 生成并配置部署密钥
为了使 GitHub Actions 能够访问 GitHub 和 Coding 仓库进行部署,我们需要生成部署密钥并将它们配置到 GitHub 仓库的 Secrets 中。
-
GitHub Pages:
- 在 GitHub 仓库的 “Settings” -> “Pages” -> “Deploy keys” 中生成一个新的部署密钥。
- 将生成的公钥添加到 GitHub 仓库的 Secrets 中,例如命名为
GITHUB_DEPLOY_KEY
。
-
Coding Pages:
- 在 Coding 仓库的 “设置” -> “Pages 服务” -> “部署密钥” 中生成一个新的部署密钥。
- 将生成的公钥添加到 GitHub 仓库的 Secrets 中,例如命名为
CODING_DEPLOY_KEY
。
4. 在工作流中使用部署密钥
修改 deploy.yml
文件,将部署密钥配置到环境变量中,以便在部署过程中使用。
jobs:
deploy-github:
...
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DEPLOY_KEY: ${{ secrets.GITHUB_DEPLOY_KEY }}
steps:
...
- name: Add deploy key
run: |
mkdir -p ~/.ssh
echo "$DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
...
deploy-coding:
...
env:
CODING_TOKEN: ${{ secrets.CODING_TOKEN }}
DEPLOY_KEY: ${{ secrets.CODING_DEPLOY_KEY }}
steps:
...
- name: Add deploy key
run: |
mkdir -p ~/.ssh
echo "$DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan e.coding.net >> ~/.ssh/known_hosts
...
5. 触发部署
完成上述配置后,当你将代码推送到 main
分支时,GitHub Actions 工作流就会自动触发,并将 Hexo 站点同时部署到 GitHub Pages 和 Coding Pages。
常见问题解答
-
问:为什么部署到 Coding Pages 失败?
答: 可能的原因有:Coding Pages 部署配置错误、Coding 部署密钥配置错误、Coding 仓库权限不足等。请检查coding.yaml
文件、Coding 部署密钥和 Coding 仓库权限设置。 -
问:如何自定义域名?
答: 你可以在 GitHub Pages 和 Coding Pages 的设置中分别配置自定义域名。 -
问:如何调试 GitHub Actions 工作流?
答: 你可以在 GitHub Actions 的 “Actions” 选项卡中查看工作流运行日志,并根据日志信息排查问题。 -
问:如何使用其他部署工具?
答: 你可以根据需要替换deploy.yml
文件中的部署命令,例如使用rsync
或scp
等工具进行部署。 -
问:如何部署到其他平台?
答: 你可以参考 GitHub Actions Marketplace 中的其他部署 Action,或者自己编写自定义的部署脚本。
通过使用 GitHub Actions,我们可以轻松实现 Hexo 博客的自动化部署,提高效率并减少错误。希望本文能够帮助你了解如何使用 GitHub Actions 同时部署到 GitHub 和 Coding,并将这一技术应用到你的项目中。