Skip to content

Instantly share code, notes, and snippets.

@dishao123
Created June 24, 2023 09:27
Show Gist options
  • Save dishao123/bd805ffca0cdcaf9d3ee191009168602 to your computer and use it in GitHub Desktop.
Save dishao123/bd805ffca0cdcaf9d3ee191009168602 to your computer and use it in GitHub Desktop.
利用 Github Action 自动化构建并部署 Hexo 博客
# 当有改动推送到main分支时,启动Action
name: Auto Deploy
on:
push:
branches:
- main #2020年10月后github新建仓库默认分支改为main,注意更改
paths-ignore: # 以下文件的变更不触发部署
- README.md
workflow_dispatch: # 手动触发部署,可以从工作流界面中手动触发
release:
types:
- published
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: 检查分支
uses: actions/checkout@v3
with:
ref: main #2020年10月后github新建仓库默认分支改为main,注意更改
- name: 安装 Node
uses: actions/setup-node@v3
with:
node-version: "18.x" #action使用的node版本,建议大版本和本地保持一致。可以在本地用node -v查询版本号。
- name: 安装 Hexo
run: |
export TZ='Asia/Shanghai'
npm install hexo-cli -g
- name: 缓存 Hexo
uses: actions/cache@v3
id: cache
with:
path: node_modules
key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}
- name: 安装依赖
if: steps.cache.outputs.cache-hit != 'true'
run: |
npm install --save
- name: 生成静态文件
run: |
hexo c
hexo g
- name: 部署 #此处master:master 指从本地的master分支提交到远程仓库的master分支,若远程仓库没有对应分支则新建一个。如有其他需要,可以根据自己的需求更改。
run: |
cd ./public
git init
git config --global user.name '${{ secrets.GITHUBUSERNAME }}'
git config --global user.email '${{ secrets.GITHUBEMAIL }}'
git add .
git commit -m "${{ github.event.head_commit.message }} $(date +"%Z %Y-%m-%d %A %H:%M:%S") Updated By Github Actions"
git push --force --quiet "https://${{ secrets.GITHUBUSERNAME }}:${{ secrets.GITHUBTOKEN }}@github.com/${{ secrets.GITHUBUSERNAME }}/${{ secrets.GITHUBUSERNAME }}.github.io.git" master:main
#git push --force --quiet "https://${{ secrets.TOKENUSER }}:${{ secrets.CODINGTOKEN }}@e.coding.net/${{ secrets.CODINGUSERNAME }}/${{ secrets.CODINGBLOGREPO }}.git" master:master #coding部署写法,需要的自行取消注释
#git push --force --quiet "https://${{ secrets.GITEEUSERNAME }}:${{ secrets.GITEETOKEN }}@gitee.com/${{ secrets.GITEEUSERNAME }}/${{ secrets.GITEEUSERNAME }}.git" master:master #gitee部署写法,需要的自行取消注释
#git push --force --quiet "https://${{ secrets.GITLABUSERNAME }}:${{ secrets.GITLABTOKEN }}@gitlab.com/${{ secrets.GITLABUSERNAME }}/${{ secrets.GITLABUSERNAME }}.gitlab.io.git" master:main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment