通过GitHub Action将hexo部署到腾讯云cos
创建hexo博客
首先安装nodejs和npm,建议使用nodejs 12版本,然后安装git。
具体步骤请参考:nodejs安装、git安装
运行hexo安装命令,如果使用linux系统请切换为超级用户
进入新文件夹,打开终端,运行如下命令安装hexo
1 2 3
| hexo init <project_name> cd <project_name> npm install
|
你可以运行如下命令验证是否初始化成功
浏览器打开 localhost:4000 如果可以正常访问即代表搭建成功
初始化git仓库,上传到github
运行如下命令进行初始化
1 2 3 4 5
| git init git add . git commit -m "commit" git remote add origin <git repo> git push origin master
|
至此,代码已经成功上传到github仓库。
配置github action自动构建并上传到cos
配置腾讯云
- 创建一个新的cos仓库,选择公共读私有写即可,记录地区信息和完整的cos名
- 腾讯云创建一组新的access_key和access_secret,并记录
创建github secret
- 进入github仓库,点击
Settings-Secrets-Actions
- 点击
New repository secret依次添加如下变量
SECRETID:对应腾讯云SecretId
SECRETKEY:对应腾讯云SecretKey
创建github action
- 依次点击
Actions-New workflow
- 点击蓝色的
set up a workflow yourself ->
- 粘贴如下代码
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: Blog CI/CD on: push: branches: - main - master env: TZ: Asia/Shanghai jobs: blog-cicd: name: Hexo blog build & deploy runs-on: ubuntu-latest steps: - name: Checkout codes uses: actions/checkout@v2 - name: Setup node uses: actions/setup-node@v1 with: node-version: "18.x" - name: Install hexo dependencies && Generate files run: yarn install && yarn run build - name: Install coscmd run: sudo pip install coscmd - name: Configure coscmd env: SECRET_ID: ${{ secrets.SecretId }} SECRET_KEY: ${{ secrets.SecretKey }} BUCKET: abc-xxxxxxxxxxxxxxx REGION: ap-xxxxxx run: coscmd config -a $SECRET_ID -s $SECRET_KEY -b $BUCKET -r $REGION - name: Upload run: cd ./public && coscmd upload -r ./ /
|
腾讯云CDN绑定自定义域名
- 腾讯云COS开启静态网站托管
- 腾讯云CDN添加域名,源站选择COS-静态网站即可