欸我不是用主機也想改一下小功能

某天在滑手機的時候,突然看到某個好像很讚的hexo-plugin,就想把他加到這個小東西上面,可是怎麼辦,我的hexo資料都在主機裡面…

底下突然超級認真:

阿所以要怎麼辦到呢?

Repo 設定

  1. 首先創一個PrivateRepo(Github現在不用錢),底下稱為原始檔repo。
  2. 在主機裡面的原始檔資料夾中,把這個repo加到你的git remote,如果像我是用VScode的話可以這樣加入:

Imgur

如果是喜歡CommandLine就要這樣:

1
git remote add origin git@github.com:<名字>/<repo-name>.git

阿需要登入自己登入。

設定忽略檔

  1. 加入 .gitignore 檔案
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    .DS_Store
    Thumbs.db
    db.json
    *.log
    node_modules/
    public/
    .deploy*/
    package-lock.json
    github-deploy-key.pub
    github-deploy-key

然後要開始Github Action

開始進入我的tempo了你知道嗎?

  1. 產生金鑰給Github Action
1
2
# 不要輸入任何 passphrase!!!
ssh-keygen -f github-deploy-key
  1. 在原始檔 repo,設定 HEXO_DEPLOY_PRI ,貼上 deploy-with-action 的內容。

Imgur

  1. blog repo,設定 HEXO_DEPLOY_PUB ,貼上 github-deploy-key.pub 的內容。

Imgur

  1. 加入Github Action的.yml
    .github/workflows/main.yml
    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
    33
    34
    35
    36
    37
    38
    39
    40
    name: HEXO CI

    on:
    push:
    branches:
    - <尼der source branch名字>

    jobs:
    build:
    runs-on: ubuntu-latest
    strategy:
    matrix:
    node-version: ['16']

    steps:
    - uses: actions/checkout@v1

    - name: Use Node.js ${{ matrix.node-version }}
    uses: actions/setup-node@v1
    with:
    node-version: ${{ matrix.node-version }}

    - name: Configuration environment
    env:
    HEXO_DEPLOY_PRI: ${{secrets.HEXO_DEPLOY_PRI}}
    run: |
    mkdir -p ~/.ssh/
    echo "$HEXO_DEPLOY_PRI" | tr -d '\r' > ~/.ssh/id_rsa
    chmod 600 ~/.ssh/id_rsa
    ssh-keyscan github.com >> ~/.ssh/known_hosts
    git config --global init.defaultBranch <尼der blog branch名字>
    git config --global user.name "<尼der名字>"
    git config --global user.email "<尼der email>"
    - name: Install dependencies
    run: |
    npm i -g hexo-cli
    npm i
    - name: Deploy hexo
    run: |
    hexo clean && hexo generate && hexo deploy

阿如果Node.js有新的LTS,就改這邊: node-version: [‘16’]

改一下_config.yml

好懶喔自己看gif

_config.yml
1
2
3
4
deploy:
type: git
repo: git@github.com:yourname/yourname.github.io.git
branch: main

Imgur

阿然後咧?

就全部通通給他git addgit push上去,就可以等你的Github Action自己跑完囉!

寫在後面

其實是第一次接觸Github Action,雖然之前有想要摸摸看,但是一直沒有時間。

科技宅如我喜歡把東西帶著走,想在沙發上用筆電一邊逛Github,一邊摸狗,一邊懶懶的改blog的小功能,阿沒有把原始檔帶著走不就什麼都做不了!

阿既然要這樣搞,不如再把懶人如我,不想用個隨身碟把原始檔裝著四處hexo depoly,當然就是用Github Action讓他可以自己跑完所有的東西啊!

這次算是摸到一點CI/CD的邊邊,但是還遠遠不及更高深的實作,如果公司的E學園有機會上這些東西的話,我一定上爆。

阿所以…

我忘記我找到的plugin是什麼了…

參考

  1. Hexo + github actions 自動化部署
  2. GitHub Actions