
最近寫了幾篇文章,想對每次編寫的文檔做個版本控制。想到了Github, 将所有的文檔備份到Github。不想每次手動運作hugo 編譯和釋出腳本,于是就有了下面的GithubActions工作流。借助GitHubActions完成對部落格文章的自動化釋出部署。
版本庫
首先,我們在Github上面建立一個倉庫,用于存儲部落格的資料。靜态部落格推薦使用Hugo,非常簡單。使用Markdown文法,官方提供了很多不錯的主題模闆。
這裡我是用主幹main 分支存儲項目的代碼, 單獨建立一個分支存儲編譯後的靜态HTML資源檔案。我們切換到另外一個分支看下:
工作面闆
建立一個工作闆可以自定義狀态,簡單的将每次的部落格優化需求記錄在這裡。這裡的每一項任務都可以轉換為issue,送出代碼可以對應的issue關聯。
CI/CD內建與釋出
使用Github actions 作為建構。設定在main分支送出代碼即建構。
name: github pages
on:
push:
branches: [ main ]
簽出主幹分支代碼,删除
pages-git
分支(該分支存放靜态檔案,需要頻繁更新)。
jobs:
deploy:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
with:
submodules: true # Fetch Hugo themes (true OR recursive)
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod
- name: Delete branch
uses: dawidd6/action-delete-branch@v3
with:
github_token: ${{ secrets.GIT_TOKEN }}
branches: "pages-git"
hugo --minify
生成靜态檔案
public
目錄。
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: '0.74.2'
# extended: true
- name: Build
run: |
hugo --minify
ls public
tar zcf ${version}-public.tar.gz public
ls
删除目前目錄除
public
目錄外的所有檔案, 然後将public目錄檔案移動到目前目錄
- name: commit
run: |
rm -fr archetypes
rm -fr content
rm -fr demo
rm -fr static
rm -fr themes
rm -fr .DS_Store
rm -fr 1.1.0-public.tar.gz
rm -fr README.md
rm -fr config.toml
mv public/* ./
sleep 3
ls -l
rm -fr public
git config --global user.email [email protected]
git config --global user.name cccc
git add .
git commit -m "update" -a
送出目前的工作目錄到
pages-git
分支。(到此pages-git分支已經存儲了更新後的靜态檔案)
- name: Push changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GIT_TOKEN }}
branch: "pages-git"
github pages通路太慢了, 準備使用國内的gitee pages服務。在gitee建立一個倉庫。最後一部分代碼是将目前
pages-git
分支代碼鏡像到gitee 項目的
pages-git
分支。
- name: 'get code '
uses: actions/checkout@v2
with:
submodules: true # Fetch Hugo themes (true OR recursive)
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod
ref: "pages-git"
- name: 'Mirror to gitee'
uses: pixta-dev/repository-mirroring-action@v1
with:
target_repo_url:
[email protected]:devopsgo/devopsgo.git
ssh_private_key:
${{ secrets.GIT_PRIVATE_KEY }}
到此,代碼已經同步到了Gitee Pages。接下來觸發Gitee Pages 服務更新。(指定靜态目錄分支部署)
浏覽器通路
devopsgo.gitee.io
就可以通路了。到此釋出完成了。
工作流已經建立好了, 最後每次釋出博文的時候。使用vscode編寫markdown文檔,然後本地hugo serve 調試。完成後送出到主幹分支,随後GitHub Actions 就會運作CI/CD,自動釋出。
擴充參考:SSH釋出到雲主機
# - name: copy file via ssh password
# uses: appleboy/scp-action@master
# with:
# host: ${{ secrets.SSH_HOST }}
# username: ${{ secrets.SSH_USER }}
# password: ${{ secrets.SSH_PASSWD }}
# port: 22
# source: "./${{env.version}}-public.tar.gz"
# target: "/opt/"
# - name: executing remote ssh commands using password
# uses: appleboy/ssh-action@master
# with:
# host: ${{ secrets.SSH_HOST }}
# username: ${{ secrets.SSH_USER }}
# password: ${{ secrets.SSH_PASSWD }}
# port: 22
# script: |
# rm -fr /var/www/newdevops/*
# mv /opt/${{ env.version }}-public.tar.gz /var/www/newdevops/
# cd /var/www/newdevops/ && tar zxf ${{ env.version }}-public.tar.gz
# mv public/* ./ && rm -fr public
# chown nginx:nginx /var/www/ -R
# systemctl reload nginx
到此,整個工作流就完成了。望對你有所幫助!